Home > Backend Development > C++ > body text

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

WBOY
Release: 2023-08-26 08:45:09
forward
726 people have browsed it

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)
Copy after login

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 ();
}
Copy after login

Output

enter a string Hello
reversed string = olleH
Copy after login

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();
}
Copy after login

Output

Enter String : Tutorialspoint
Reverse string :tniopslairotuT
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template