C Program to reverse string
Simple C program to reverse string: In the given example, we swap string elements like first element with last element, second element with second last element and so on. We do this n/2 times or half of array size.
/* C Program to reverse string */ #include<stdio.h> #include<string.h> int main() { char str[20]; int i,j,n; printf("Enter a string: "); gets(str); n=strlen(str); for(i=0;i<n/2;i++) { char temp=str[i]; str[i]=str[n-1-i]; str[n-1-i]=temp; } printf("Reverse string is : %s",str); return 0; } /* End of program */
Another C Program to reverse string using pointer
/* C Program to reverse strin */ #include<stdio.h> #include<string.h> int main() { char str[20]; char *temp=str; printf("Enter a string: "); gets(str); while(*temp!='\0') { temp++; } temp--; printf("Reverse String is: "); while(temp>=str) { printf("%c",*temp); temp--; } return 0; } /* End of program */