How to reverse a string in C?
#include <stdio.h>
#include <string.h>
int main(void) {
char s[] = "Hello";
int i = 0, j = strlen(s) - 1;
while (i < j) {
char t = s[i];
s[i] = s[j];
s[j] = t;
i++;
j--;
}
printf("%s\n", s);
return 0;
}
Output:
Output of the above program is
olleH
More ways are in Reverse a String in C.
Learn more about the similar topics:
| Tutorials |
|---|
| C Arrays |
| C Two Dimensional Arrays |
| C Passing Arrays to Functions |
| C Strings |
| C String Input and Output |
| C String Functions |
| C strlen() Function |
| Interview Questions & Answers |
|---|
| What is an array in C? |
| How is a string stored in C? |
| How to reverse a string in C? |
| What is the difference between memcpy() and strcpy() in C? |