20/03/2016

C Program For Reverse a String Using Pointers

Problem:- Reverse a string using pointers c++ or C Program For Reverse A String Using Pointer or reverse a string using pointers c++ using while loop or c program to reverse a string using pointers with an explanation or Reverse a string using pointers or Reverse a string using pointers in C or How to Reverse String using Pointer in C or C Program to Reverse String Without Using Library Function or String Reverse Using pointers or Reverse all words and string using pointer or c program to reverse a string using pointer and loop or Reverse the String Using FOR Loop and Pointers in C or Write a C program to reverse a string using pointers or C program to reverse a string using pointers.

Explanation:- Reverse a string using pointers is very likely a Stack program it also follows the same rule of Stack FILO (First In Last Out) in Reversing a string using pointers we are doing the same when we are taking input from the user at the same time we are calculating the length of a String and with the help of length one by one we are transferring the character one by one from one string to another and until the length is not become zero in while loop we put the same condition.


Homework:- try to modify this program so the user can also take space as input. 

Solution:-

#include<stdio.h>
int main()
{
    char str[50];
    char revStr[50];

    char *strPtr = str;
    char *revPtr = revStr;

    int len = -1;

    printf("Enter The String Without Space:\n\n");
    scanf("%s",str);

    while(*strPtr)
  {
      strPtr++;
      len++;
    }

    while (len >= 0)
  {
      strPtr--;
      *revPtr = *strPtr;
      revPtr++;
      --len;
    }

    *revPtr='\0';
    printf("\n\nReverse String Is \n\n%s",revStr);
    return 0;
}

Output:-


C Program For Reverse a String Using Pointers

You May Also Like

Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

2 comments: