Reverse Given String


This operation is used to reverse the given string.
In order to reverse a given string first we have to find the length of given string. Once length of the string is found we can start copying characters from the end of given string into new string to produce the reverse of given string.

Algorithm to Reverse Given String


Step 1: Length1 =strlen (S1)
Step 2: Length1 = Length1 - 1
Length2 = 0
Step 3: Repeat step 4 while Length1 >= 0
Step 4: S2 [Length2] =S1 [Length1]
Length2 = Length2 + 1
Length1 = Length1 -1
Step 5: S2 [Length2] = NULL
     

Program to Reverse Given String


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char *s1,*s2;
void srev(char *s1,char *s2);
clrscr();
puts("Enter string:");
gets(s1);
srev(s1,s2);
puts("Reverse of String is");
puts(s2);
getch();
}
void srev(char *s1,char *s2)
{
int length1=strlen(s1);
int length2=0;
length1=length1-1;
while(length1>=0)
{
s2[length2]=s1[length1];
length1=length1-1;
length2=length2+1;
}
s2[length2]='\0';
}

Download Projects


Download Programs