+ 2
Can anyone tell me what is wrong with this program?
<<TO REVERSE A STRING>> #include <iostream> using namespace std; void findreverse(char a[]) { char b[100]; int count =0; int k =0; for(int i =0; a[i]!='\0';i++) { count++; // length of a string } for(int i =count;i>=0;i--) { b[k]=a[i];k++; cout<<b[k]; } } int main() { char a[100]; cout<<"Enter the string"<<endl;cin.getline(a,100); findreverse(a); return 0; }
2 Answers
+ 8
#include <iostream>
using namespace std;
void findreverse(const char a[])
{
char b[100];
int count =0;
for(; a[count]!='\0';count++)
; // length of a string
for(int i = count - 1;i>=0;i--)
{
b[count - i - 1]=a[i];
}
b[count] = 0;
cout << b << endl;
}
int main()
{
char a[100];
cout<<"Enter the string"<<endl;
cin.getline(a,100);
findreverse(a);
return 0;
}
// correction with a little bit of optimization