Reverse String In C / Reversing String In C

Reverse String In C / Reversing String In C

Reversing string in C / Reverse String In C seems to be a daunting task for the newbies but after reading this post you can easily reverse string in C.

What Is Reverse String In C

Reverse string in c is basically taking input( i.e sentence ) from console and then display the sentence from last character to first character.

The following Program to reverse string in c accepts a string from the user and print the reverse string. For example consider the following input from the user :

Input String : The site name is comp-psyche.com
Output String : moc.ehcysp-pmoc si eman etis ehT

There are various ways to reverse string in c. You can either use string function to reverse string in c or you can reverse string in c without using c string function.
In the following program we will reverse string in c using string function streev()


#include<stdio.h>
#include<string.h>

int main()
{
 // Declaring variable str=string 
 char str[50];
 
 // Inputing string
 printf("Enter any string : ");
 gets(str);

 // Printing string and reversed string
 printf("String : %s\n",str);
 printf("Reversed string : %s",strrev(str));

 return 0;
}

REVERSE STRING IN C WITHOUT USING STRING FUNCTION

In the following program we will reverse string in c without using string function



int main()
{
 // Declaring variable str=string, revstr[50]=reverse string
 char str[50], revstr[50];
 
 // Declaring variable i=to iterate loop, l=length
 int i, j,l=0;
 
 // Inputing string
 printf("Enter any string : ");
 gets(str);

 // finding length
 while(str[l]!='\0')
 l++;
 
 // Reversing string
 j=0;
 for(i=l-1;i>=0;i--)
 {
  revstr[j]=str[i];
  j++;
 }
 revstr[j]='\0';
 
 // Printing string and reversed string
 printf("String : %s\n",str);
 printf("Reversed string : %s",revstr);

}

REVERSE STRING IN C USING RECURSION

In the following program we will reverse string in c using recursion


// Write a c program to reverse a string using recursion
#include<stdio.h>
void reverse (int index, char *str );

int main (void)

{

   char str[100];
   printf ("Enter any string : "); 
   gets(str);
 reverse (strlen(str) , str );
 return 0;

}

void reverse (int index, char *str )

{

  if (--index < 0 )

  {
       return ;
  }

  else
  {
        putchar ( *(str + index) ) ;  
        reverse (index, str) ;
  }

}

REVERSE STRING IN C USING POINTER

In the following program we will reverse string in c using pointer

// Write a C program to reverse a string using pointer
#include<stdio.h>
int main()
{
    
    // Declaring variable str = string and revstr = to store the revere string
    char str[50];
    char revstr[50];
    
    char *strptr = str; // strptr - holds the first position address of str
    char *revptr = revstr; // revptr - holds the first position address of revstr
    int i=-1;

    // Inputting string
    printf("Enter any string : ");
    gets(str);
   
    // strptr is pointed to the last position address
    while(*strptr)
    {
        strptr++;
        i++;
    }

    // string stored in str is copied to revstr
    while(i>=0)
{
       strptr--;
       *revptr = *strptr;
       revptr++;
       --i;
    }

    *revptr='\0';
  
    printf("Reverse of string is : %s",revstr);
  
    return 0;
}


Best C Compiler For Windows

Here I am providing you with three best c compiler for windows - CodeBlock, Dev C and Turbo C compiler. CodeBlock and Dev C are actually IDEs (Integrated Development Environment or Interactive Development Environment) whereas Turbo C is a basic compiler. CodeBlock which is provided here uses mingw compiler whereas Dev C uses gcc compiler. Though many suggest not to use Turbo C to learn c programming but as it is well documented and basic program runs without errors(pretty good). So I decided to keep it here.


BEST C COMPILER


C PROGRAMMING USING CODEBLOCKS

DOWNLOAD CODE BLOCKS


Code Blocks is an integrated development environment or IDE which uses mingw compiler or gcc compiler. Code Blocks provided here uses mingw compiler which is one of the best c compiler for windows to compile and run c program. This version of Code Block runs in Windows 7 / Windows 8. For windows XP it is not tested.

You can download free c compiler for windows from : Download Code Blocks For Free

HOW TO INSTALL CODE BLOCKS

Back To Best C Compiler

Now that you have downloaded CodeBlocks the next thing which you should know is how to install CodeBlocks. So follow these steps to install it :
  • Open the folder where you have downloaded CodeBlocks.
  • Run the exe file. A window pops up.

    Best C Compiler - How to install CodeBlocks : First step
    Best c compilers :  First Step

  • Click on next. Another window of License Agreement pops up.
    Best C Compiler - How to install CodeBlocks : License Agreement
    Best c compilers :  License Agreement
  • Click on I Agree. A window of Choose Components pops up.

    Best C Compiler - How to install CodeBlocks : Choose Components
    Best c compilers :  Choose Components
  • Click on Next. Install windows pops up.

    Best C Compiler - How to install CodeBlocks : Install Location
    Best c compilers :  Choose Install Location
  • Choose the location where you want CodeBlocks to be installed and then click on Install.
  • After Installation a windows pops up saying if you want to run CodeBlocks. Click on yes to run it.
  • A window of Installation Completion pops up.
    Best C Compiler - How to install CodeBlocks : Installation Completion
    Best c compilers :  Installation complete

  • Click on Next. Another windows pops up.

    Best C Compiler - How to install CodeBlocks : Finish Window
    Best c compilers :  Finishing the setup
  • Click on Finish. 

HOW TO WRITE C PROGRAM

Back To C Compiler

Now that you have finished Installing Code Blocks. Lets write a simple c program.

#include<stdio.h>
int main()
{
      printf("Congratulation.. You successfully run your first Program");
      return 0;
}

Note : If you want to know what the syntax of the c program then you can read it from : Basic Component Of C Program

Now the question arises where should you write this code. Follow these steps to write your first C Program: 
  • Click on File - New - File...

    Best C Compiler - How to write C Program : File-New-File...
    Best c compilers :  How to write c program

  • Select C/C++ Source and click on Go.

    Best C Compiler - How to write C Program : C/C++ Source
    Best c compilers :  How to write c program

  • A window for Language Selection pops up. Select C and click on Next.

    Best C Compiler - How to write C Program : Language Selection
    Best c compilers :  How to write c program

  • Enter the File Location with File Name i.e where you want to save your file and click on Finish.

    Best C Compiler - How to write C Program : File Location with File Name
    Best c compilers :  How to write c program

HOW TO COMPILE AND RUN C PROGRAM

Back To Best C Compiler

Congratulation You successfully wrote your first c program. Now the next step is to Compile and Run it. To Compile and Run your first c program follow these steps :
  • To Compile c program : Click on Build - Compile Current File. You can also use shortcut key - Ctrl + Shift + F9
  • To Run c program : Click on Build - Run. You can also use shortcut key - Ctrl + F10
  • To Compile and run c program at the same time : Click on Build - Build and Run. you can achieve the same task by pressing F9

DOWNLOAD DEV C

Back To Best C Compiler

Dev C is another integrated development environment or IDE. Dev C provided here uses gcc compiler which is one of the best c compiler for windows with good graphic and user friendly environment. You can easily write, compile and run a c program using Dev C. This version of Code Block runs in Windows 7 / Windows 8. For windows XP it is not tested.

You can download free c compiler for windows from : Download Dev C For Free

HOW TO INSTALL C COMPILER DEV C

Back To Best C Compiler

Now that you have downloaded Dev C. Lets install it. Follow these step to install Dev C.
  • Open the folder where you have downloaded Dev C..
  • Run the exe file. Installer Language windows pops up. Select language according to your preference and then click OK.
    Best C Compiler - How to install Dev c/c++ : Installation language
    Best c compilers :  Installer Language

  • Another window of License Agreement pops up. Click I Agree.

    Best C Compiler - How to install Dev C/C++ : License Agreement
    Best c compilers : License Agreement


  • Choose Components windows Appears. Click Next.

    Best C Compiler - How to install Dev C/C++ : Choose component
    Best c compilers : Choose Components

  • Install windows pops up. Choose the location where you want Dev-C++ to be installed and then click on Install.

    Best C Compiler - How to install Dev C/C++ : Choose Install Location
    Best c compilers : Choose Install Location

  • Installation Completion windows pops up. Click on Finish.

    Best C Compiler - How to install Dev C/C++ : Installation Completion
    Best c compilers : Finishing the setup

HOW TO WRITE C PROGRAM

Back To Best C Compiler

Now that you have finished Installing Dev C. Lets write a simple c program.

#include<stdio.h>
int main()
{
      printf("Congratulation.. You successfully run your first Program");
      return 0;
}

Note : If you want to know about the syntax of c program  then you can read it from : Basic Component Of C Program

Now where should you write the above code. Follow these steps to write your first c program :
  • Click on File - New - Source File
  • An editor opens up. Write your c program and then save it with ".c" extension.
  • To save c program : Click on File - Save As...
  • Give a name to your file and save it as C source files.

    Best C Complier - Saving c program in Dev C/C++
    Best c compilers : How to write c program in Dev C/C++

HOW TO COMPILE AND RUN C PROGRAM

Back To Best C Compiler

Lets Compile and Run your first c program. Follow these steps to compile and run c program :
  • To compile c program : Click on Execute - Compile or F9
  • To Run c program : Click on Execute - Run or F10
  • To Compile and Run : Click on Execute - Compile & Run or F11