C025 A C program to input a string and output the reversed string using pointer notation

By | October 10, 2013

Let’s identify variables needed for this program.

In this program, we need several variables to store string and count the number of characters in the given string.

First variables will be the one which will save the string entered by the user and reverse of it. So it will be S[50] and R[50]. In C language we do not have string as variable so we have to use character array for the same. Next is a pointer which will hold the address of S[50] and character pointers will be needed to point to the character arrays So we need *PTR1  and *PTR2. Next we will need a LEN variable to store Length of a characters in a string so in all Five variables.
The identified variables are S[50], R[50],*PTR1, *PTR2, LEN.

char *PTR1,*PTR2,S[50],R[50];
 int LEN=0;

Now, Selection of data type will be int data type due to the values expected for counting are decimals and they will be holding smaller values so int data type is sufficient for LEN. Selection of data type will be char data type due to string is a character array and we don’t have any string data type so char data type is selected for S[50], R[50] and *PTR1, *PTR2 pointers are always of same data type.

In this program, Give message to user to enter the required string and scan the enter character is the character array i.e. S[50]. The base address of the string S[50], R[50] is assigned to the character pointers *PTR1, *PTR2 respectively. In While loop we will use *PTR1 it will return true if any character is present in it or return false if any character is absent in it. Now pointer *PTR1 which will be used for moving from start to end of the string given within the loop pointer is incremented and so is LEN which will count the character in the string and used to reverse the string by adding it to the second pointer *PTR2 .

Main points to take care are all the strings are terminated by a NULL value at the end of the string and indexing of the string starts from zero so when we add the Length to the second pointer it is after the characters are finished, Hence we are assigning NULL to that position. Another point is that we have already moved the first pointer so assigned the address of S[50] to it again else no output will be sent to second string. So we are incrementing second pointer and decrementing first pointer.

void main()
 char *PTR1,*PTR2,S[50],R[50];
 int LEN=0;
 printf("\nTHE REVERSED STRING IS :- %s",R);

Finally print the Length and the reversed String at the end of the program.

C program code :

  1. #include<stdio.h>
  2. void main()
  3. {
  4.  char *PTR1,*PTR2,S[50],R[50];
  5.  int I,LEN=0;
  6.  clrscr();
  7.  printf("\nENTER THE STRING TO BE REVERSED :- ");
  8.  scanf("%s",&S);
  9.  PTR1=&S;
  10.  PTR2=&R;
  11.  while(*PTR1)
  12.      {
  13.       LEN++;
  14.       PTR1++;
  15.      }
  16.  printf("%d",LEN);
  17.  PTR1=&S;
  18.  PTR2=PTR2+LEN;
  19.  *PTR2=NULL;
  20.  PTR2--;
  21.  while(*PTR1)
  22.      {
  23.       *PTR2=*PTR1;
  24.       PTR1++;
  25.       PTR2--;
  26.      }
  27.  printf("\nTHE REVERSED STRING IS :- %s",R);
  28.  getch();
  29. }





Note:- To understand program for sequence in detail Please SEARCH numerically example: C001, C002, etc.

Leave a Reply