#include<stdio.h>
#include<string.h>
int main()
{
char str[30] = "plus2net";
printf("String after reverse : %s",strrev(str));
return 0;
}
The output of above code is here
ten2sulp
We can use string function strrev()
to reverse a string.
strlen(strrev);
#include<stdio.h>
#include<string.h>
int main()
{
char str[30] = "plus2net";
strcpy(str,strrev(str));
printf("String after reverse : %s",str);
return 0;
}
#include<stdio.h>
#include<string.h>
int main()
{
char str[30];
printf("Enter a string ");
scanf("%s",str);
strcpy(str,strrev(str));
printf("String after reverse : %s",str);
return 0;
}
#include<stdio.h>
#include<string.h>
int main()
{
char str[30]="plus2net";
int str_length;
str_length=strlen(str); // length of the string
for(int i=str_length; i>=0; i--){
printf("%c",str[i]); // printing the char at position i
}
return 0;
}
This example shows how to reverse multiple strings stored in an array.
#include <stdio.h>
#include <string.h>
void reverse_string(char str[]) {
int n = strlen(str);
for (int i = 0; i < n / 2; i++) {
char temp = str[i];
str[i] = str[n - i - 1];
str[n - i - 1] = temp;
}
}
int main() {
char str_arr[3][50] = {"plus2net", "hello", "world"};
for (int i = 0; i < 3; i++) {
reverse_string(str_arr[i]);
printf("Reversed string %d: %s\n", i + 1, str_arr[i]);
}
return 0;
}
Output:
Reversed string 1: ten2sulp
Reversed string 2: olleh
Reversed string 3: dlrow