So I recently started learning C++ and stumbled upon this issue.
I don't understand why can I not reverse the string using the following piece of code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include<iostream>
#include<string.h>
usingnamespace std;
int main(){
char stri[40], stra[40];
int a, i, j;
cout<<"Enter String:- ";
cin.getline(stri,40);
a=strlen(stri);
for(i=0,j=a;i<a;i++,j--){
stra[i]=stri[j];
}
cout.write(stra,40);
return 0;
}
I know there are many other methods to do the same but why won't this method work?