Home  >  Article  >  Backend Development  >  Write a C program to reverse a string without using library functions

Write a C program to reverse a string without using library functions

WBOY
WBOYforward
2023-08-26 08:45:09717browse

Write a C program to reverse a string without using library functions

Use strrev() function

  • This function is used to reverse a string.
  • The reversed string will be stored in the same string.

Syntax

strrev (string)

Before flipping a string without using a function, let us first see how to flip a string using the string function strrev() so that we can Easily spot the differences and get a clear understanding of the concepts −

Example

#include<stdio.h>
main (){
   char a[50] ;
   clrscr();
   printf (&ldquo;enter a string&rdquo;);
   gets (a);
   strrev (a);
   printf(&ldquo;reversed string = %s&rdquo;,a)
   getch ();
}

Output

enter a string Hello
reversed string = olleH

Not using strrev() function

Now Let’s look at a program to reverse a string without using strrev() function −

Example

#include <stdio.h>
#include <conio.h>
#include <string.h>
void main(){
   char string[20],temp;
   int i,length;
   printf("Enter String : ");
   scanf("%s",string);
   length=strlen(string)-1;
   for(i=0;i<strlen(string)/2;i++){
      temp=string[i];
      string[i]=string[length];
      string[length--]=temp;
   }
   printf("</p><p>Reverse string :%s",string);
   getch();
}

Output

Enter String : Tutorialspoint
Reverse string :tniopslairotuT

The above is the detailed content of Write a C program to reverse a string without using library functions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete