pTutorial

New way to learn C programming

C program to reverse string

This is a user-defined c program to reverse a string using strrev function from the string.h library and without using pre-defined function. For example entered string is "ptutorial" then after reversing result will be "lairotutp". You can see all the code below and also download this code.

C program to reveres string without using any pre-defined function



#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
 {
   char z[100], chr;
   int y, x = 0;
 
   printf("Please enter string :");
   gets(z);
 
   y = 0;
   x = strlen(z) - 1;
 
   while (y <= x) 
   {
      chr = z[y];
      z[y] = z[x];
      z[x] = chr;
      y++;
      x--;
   }
 
   printf("\nAfter reversing the string result will be :%s", z);
   getch();
}

Download C program to reverse String

Output of program

C program to reverse String

In this example, first create x and y two integer variables and assign "y=0" and x=length of (string -1) because array starts from zero.

Initialise while loop until y reach length of string and in between while loop swapping take place and at last increase the counter of x and y for accessing every character of entered string.

See Also

Multidimensional array in java   |   Php5 example pdf   |   Ajax difference

QUOTES OF THE DAY

The ballot is stronger than bullet.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER