In C programming, a string is a one-dimensional array of characters. As a result, one can reverse a string using a for loop that decrements the variable over which the loop is iterated.
An example of such a code is:
#include <stdio.h>
#include <string.h>
int main(){
char str[100], rev[100];
int i, j, len;
printf("Enter a string: ");
scanf(“%s”, str); // read string from user
len = strlen(str); // calculate length of string
j = 0;
for(i = len - 1; i >= 0; i--){
rev[j++] = str[i];
}
rev[j] = '\0';
printf("Reverse of the string: %s", rev);
return 0;
}
// Output = Enter string: hello
Reverse of the string: olleh
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In C programming, a string is a one-dimensional array of characters. As a result, one can reverse a string using a for loop that decrements the variable over which the loop is iterated.
An example of such a code is: