- Get link
- X
- Other Apps
Reversing a string
If s1="HELLO", then reverse of s1="OLLEH".To reverse a string we just need to swap the first character with the last,second character with the second last character.
Write a program to reverse the given string
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[100],reverse_str[100],temp;
int i=0,j=0;
clrscr();
printf("\n Enter the string:");
gets(str);
j=strlen(str)-1;
while(i<j)
{
temp=str[j];
str[j]=str[i];
str[i]=temp;
i++;
j--;
}
printf("\n The reversed string is:");
puts(str);
getch();
}
Output
Enter the string:HELLO
The reversed strin is:OLLEH
If s1="HELLO", then reverse of s1="OLLEH".To reverse a string we just need to swap the first character with the last,second character with the second last character.
Write a program to reverse the given string
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[100],reverse_str[100],temp;
int i=0,j=0;
clrscr();
printf("\n Enter the string:");
gets(str);
j=strlen(str)-1;
while(i<j)
{
temp=str[j];
str[j]=str[i];
str[i]=temp;
i++;
j--;
}
printf("\n The reversed string is:");
puts(str);
getch();
}
Output
Enter the string:HELLO
The reversed strin is:OLLEH
Comments
Post a Comment