Write a C program to reverse a given string with or without using string function.
In c programming it provide the various string handling function to manage string such as strlen, strcpy, strcmp etc. C programming also provide the string function to write a given string in reverse order by using strrev() function. In this string handling function of c programming it convert the string in its reverse order.
Syntax:
strrev(*string);
Example:
strrev(str);
In this program we will use the concept of swapping so get on temp. variable.
In c programming it provide the various string handling function to manage string such as strlen, strcpy, strcmp etc. C programming also provide the string function to write a given string in reverse order by using strrev() function. In this string handling function of c programming it convert the string in its reverse order.
Syntax:
strrev(*string);
Example:
strrev(str);
C Program to reverse a string using string function.
#include<stdio.h>
void main()
{
char str[20];
clrscr();
printf("Enter a string:: ");
scanf("%s",str);
printf("\nReverse string:: %s",strrev(str));
getch();
}
void main()
{
char str[20];
clrscr();
printf("Enter a string:: ");
scanf("%s",str);
printf("\nReverse string:: %s",strrev(str));
getch();
}
C program to reverse a given string without using string function.
To convert a string in its reverse order in c programming without using string function. first we find the length of string than after we will divide string length with 2 so we will swap first and last character of string with each other and than after 2nd character and 2nd last character and so on.In this program we will use the concept of swapping so get on temp. variable.
#include<stdio.h>
void main()
{
int i,len=0,l=0;
char str[20],temp;
clrscr();
printf("\nEnter any string:: ");
scanf("%s", str);
len=strlen(str);
l=len-1;
for(i=0;i<len/2;i++)
{
temp=str[i];
str[i]=str[l];
str[l]=temp;
l--;
}
printf("\nReverse string:: %s",str);
getch();
}
void main()
{
int i,len=0,l=0;
char str[20],temp;
clrscr();
printf("\nEnter any string:: ");
scanf("%s", str);
len=strlen(str);
l=len-1;
for(i=0;i<len/2;i++)
{
temp=str[i];
str[i]=str[l];
str[l]=temp;
l--;
}
printf("\nReverse string:: %s",str);
getch();
}
Here you can find c program to reverse string using pointers and recursion.
ReplyDeletec program to reverse a string