Pages

  • Home
  • About & Contact
  • Archive
  • C Programming Examples

23 February 2012

C program to reverse a string without using string functions [ using pointers ]

The First program I have shown is the program to reverse the string using String function and the program below than this program is the program to reverse the string using pointers.For information on Strings and string function click here
C program to reverse a string without using string functions [ using pointers ]

C program to reverse the string using strrev().


#include <stdio.h>
#include <string.h>
void main(void)
{
   char string[25];
   printf("\nEnter a String to be reversed: ");
   gets(string);
   strrev(string);
   printf("\nreverse string is %s",string);
}

C program to reverse the string using pointer's
#include<stdio.h>

void rev(char *);
void main()
{
     char string[50];
     printf("Enter a string to be reversed:");
     gets(string);
     printf("Before reversing the string %s\t",string);
     rev(string);
     printf("\nReverse String is %s",string);
}

void rev(char *str)

{
     char *str1,temp;
     str1=str;
     while(*str1!='\0')
     str1++;                  //traversing pointer to point end of the string i.e '/0'
     str1--;                                  //now pointer will point to last character that is before '\0'
     while(str<str1)
     {
        temp=*str;                    // 1
        *str=*str1;                   // 2
        *str1=temp;                   //  3
        str++;
        str1--;
     }
}

Explanation: Assume the string to be KETAN.After rev() is called *str1 will point to last character i..e .
Now consider the while loop Statements 1,2,3 are used to swap the characters pointed by str and str1.
str1 points to last character (N) where as str points to 1st character (K).Then increment the str pointer and decrement the a pointer.So now str points to E and *str1 points to A. Again the same procedure will continue for the last time and we will get the reverse of string.

Similar Programs:

c program to copy one string to another without using strcpy [ using pointers ]


c program to find length of string without using strlen function [ using pointers]



c program to concatenate two strings without using strcat [ using pointers]

c program to compare two strings without using strcmp [ using pointers ]



Share your views regarding the given post via the comments section given below.
If you liked the post, please '+1' it & share it on other social networks.
Thank You
-Romil Shah

12 comments:

  1. Hey Can you please explain why there are str1++ and str1-- statements in lines 4 and 5 of the rev() ???

    ReplyDelete
    Replies
    1. we have 1 pointer to point to the start of string and the other at the end of string
      we just swap the values of first and last
      then increment the 1st pointer to point 2nd location and decrements the other pointer to point 2nd last element and then swap both

      the loop continues until midway

      //1//2//3
      are the statements used to swap the values present at the address in both pointers

      Delete
  2. hello thank you sir ji...

    ReplyDelete
  3. Anonymous03 June, 2013

    This comment has been removed by a blog administrator.

    ReplyDelete
  4. Anonymous11 June, 2013

    This comment has been removed by a blog administrator.

    ReplyDelete
  5. Can u give c program for reverse without using strrev() and without using pointer.....plz

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  6. #include
    #include
    int i=0,j=0;
    main()
    {
    char a[20];
    char b[20];

    printf("enter string: \n");
    scanf("%s",a);
    while(a[i]!='\0')
    {i++;}
    printf("length %d \n",i);
    do
    { b[j]=a[i-1];
    j++;
    i--;

    }while(i>0);
    b[j]='\0';
    puts(b);

    getch();
    }

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. header files :-> stdio.h and conio.h

      Delete
  7. This comment has been removed by the author.

    ReplyDelete
  8. can anyone explain full program step by step i cant understand after while loop
    please explain it

    ReplyDelete