C Program To Reverse a String Using Pointers

C Program To Reverse a String Using Pointers – If you are looking for a program for reversing a string in C program, this article will help you to learn how to reverse a string in C using pointers. Just go through this reverse string tutorial you will be able to write a  C program to reverse a string.

C Programming Tutorials

CodingCompiler.com presenting series of C programming tutorials for beginners, experienced to advanced learners. These programming tutorials help C learners, students, B.Tech graduates to enhance their C programming skills and hands-on experience.

C Program To Reverse a String with Using Function

C Program To Reverse a String without Using Function

C Program To Reverse a String Using Recursion

This is the sixth C programming example in the series, it helps newbies in enhancing C programming and land on their dream job. All the best guys in learning c programming with coding compiler website.

C Program to Reverse a String Using Pointers

Learn how to reverse a string in C programming in this tutorial. Reversing a string in C programming language can be done using various techniques, but here in this program, we show how to reverse a string using pointers.

C Program To Reverse a String Source Code

Copy paste the below source code or write your own logic into C compilers and run the program to see the result.

/* C PROGRAM TO REVERSE a STRING - REVERSESTRINGRECURSION.C */

#include<stdio.h>
 
int string_length(char*); //Pointers
void reverse(char*);
 
main() 
{
   char string[100];
 
   printf("Enter a string\n");
   gets(string);  //Input taking from user
 
   reverse(string);
 
   printf("Reverse of entered string is \"%s\".\n", string);
 
   return 0;
}
//Pointers Logic to reverse a string 
void reverse(char *string) 
{
   int length, c;
   char *begin, *end, temp;
 
   length = string_length(string);
   begin  = string;
   end    = string;
 
   for (c = 0; c < length - 1; c++)
      end++;
 
   for (c = 0; c < length/2; c++)
   {        
      temp   = *end;
      *end   = *begin;
      *begin = temp;
 
      begin++;
      end--;
   }
}
 
int string_length(char *pointer)
{
   int c = 0;
 
   while( *(pointer + c) != '\0' )
      c++;
 
   return c;
}

C Program To Reverse a String Output

After you compile and run the above program, your C compiler asks you to enter a string to reverse. After you enter a string, the recursive function will reverse the string and show output like below expected output.

Enter a string

programming

Reverse of entered string is

gnimmargorp

Read Other C Programming Tutorials:

C Program To Swap Two Numbers Using Two Variables

C Program To Swap Two Numbers Using Three Variables

C Program For Prime Numbers – Check  a Number is Prime or Not

C Program To Reverse a String with Using Function

C Program to Reverse a String without Using Function

C Program to Reverse a Sting Using Recursion

Leave a Comment