C program to reverse a string with or without string function strrev

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);

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();

}

c program to reverse a string by using string function


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();


}

C program to reverse a string wihout using string function

1 comments:

  1. Here you can find c program to reverse string using pointers and recursion.
    c program to reverse a string

    ReplyDelete