+ 1

Find the Error : Reverse the word in its position

I want to reverse the word in its position but output is not correct can anyone help me to find the error in my code. ex: samir singh : rimas hgnis ************MY CODE IS****** #include<stdio.h> #include<string.h> int main() { char str[]="Samir Singh"; int i,j,start,end=0; int l=strlen(str); for(i=0; str[i]!='\0'; i++) { if(str[i]==' ' || i==l) { if(str[i]==' ') start=i-1; else start=l-1; for(j=start; j>=end; j--) { printf("%c",str[j]); } end=start+2; printf(" "); } } return 0; }

26th Dec 2020, 2:52 AM
Samir Singh
Samir Singh - avatar
1 Odpowiedź
0
You can use this function. Just pass the string you want to reverse as argument, and it will reverse the string. void reverse( char* str ) { char swap; size_t left = 0, // left index right = strlen( str ) - 1; // right index while( left != right ) { swap = str[ left ]; str[ left ] = str[ right ]; str[ right ] = swap; left++; right--; } }
26th Dec 2020, 4:47 AM
Ipang