c++ code not running | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

c++ code not running

//reverse a STRING #include<iostream> #include<cstring> void reverse(char *a); using namespace std; int main() { char a[100]="keshav"; reverse(a); } void reverse(char *a) {char temp; int c=strlen(a),i; for(i=0;i<c/2;i++) { temp=a[i]; a[i]=a[c-i]; a[c-i]=temp; } cout<<a<<endl; } it is not showing the output please help!

5th Sep 2019, 7:30 PM
Kesh∆v
Kesh∆v - avatar
1 Answer
+ 2
Change it to: for (i = 0; i < c/2; i++) { temp = a[i]; a[i] = a[c-i-1]; a[c-i-1] = temp; } Strlen() returns the number of characters in the array, but the position of the characters in the array is strlen()-1 since array indices begin at 0. You have to substract 1 or else the last character in the array (the null terminator) is shifted to the beginning of the array and you end up with an empty string.
5th Sep 2019, 8:15 PM
Cluck'n'Coder
Cluck'n'Coder - avatar