The strrev() function is a built-in function in C and is defined in string.h header file. The strrev() function is used to reverse the given string.
Syntax:
strrev(str);
Example
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
printf("\nEnter the String : ");
gets(str);
printf("The given string is =%s\n",str);
printf("After reversing string is =%s",strrev(str));
return 0;
}
Output
Reverse String Without Using Library Function [ Strrev ]
#include<stdio.h>
#include<string.h>
int main() {
char str[100], temp;
int i, j = 0;
printf("\nEnter the string :");
gets(str);
i = 0;
j = strlen(str) - 1;
while (i < j) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}
printf("\nReverse string is :%s", str);
return (0);
}
#include<string.h>
int main() {
char str[100], temp;
int i, j = 0;
printf("\nEnter the string :");
gets(str);
i = 0;
j = strlen(str) - 1;
while (i < j) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}
printf("\nReverse string is :%s", str);
return (0);
}
or
#include <stdio.h>
#include <conio.h>
int main()
{
char str1[50], reversed[50];
int a, b, cnt = 0;
printf("\nEnter String:");
gets(str1);
//original string
printf("\n The given string is %s", str1);
// loop to calculate the length of the string
while (str1[cnt] != '\0') {
cnt++;
}
b = cnt - 1;
//assigning the value to characters of a new string
for (a = 0; a < cnt; a++) {
reversed[a] = str1[b];
b--;
}
printf("\n The reversed string is %s", reversed);
getch();
}
Output
No comments:
Post a Comment