To invert a string, we need to exchange the last character with the first character, the second character with the last second character, etc., until we reach the middle of the string.
#include <stdio.h> #include <string.h> int main(void) { //variable char str[100], tmp; int i, len, mid; //input printf("Enter a string: "); gets(str); //find number of characters len = strlen(str); mid = len/2; //reverse for (i = 0; i < mid; i++) { tmp = str[len - 1 - i]; str[len - 1 - i] = str[i]; str[i] = tmp; } //output printf("Reversed string: %s\n", str); printf("End of code\n"); return 0; }
output:
Enter a string: Hello World Reversed string: dlroW olleH End of code