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.

[codesyntax lang=”c”]

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

[/codesyntax]

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.

[codesyntax lang=”c”]

#include<stdio.h>
void main()
{
 char *PTR1,*PTR2,S[50],R[50];
 int LEN=0;
 clrscr();
 printf("\nENTER THE STRING TO BE REVERSED :- ");
 scanf("%s",&S);
 PTR1=&S;
 PTR2=&R;
 while(*PTR1)
     {
      LEN++;
      PTR1++;
     }
 printf("%d",LEN);
 PTR1=&S;
 PTR2=PTR2+LEN;
 *PTR2=NULL;
 PTR2--;
 while(*PTR1)
     {
      *PTR2=*PTR1;
      PTR1++;
      PTR2--;
     }
 printf("\nTHE REVERSED STRING IS :- %s",R);
 getch();
}

[/codesyntax]

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

C program code :

[codesyntax lang=”c” lines=”normal”]

#include<stdio.h>
void main()
{
 char *PTR1,*PTR2,S[50],R[50];
 int I,LEN=0;
 clrscr();
 printf("\nENTER THE STRING TO BE REVERSED :- ");
 scanf("%s",&S);
 PTR1=&S;
 PTR2=&R;
 while(*PTR1)
     {
      LEN++;
      PTR1++;
     }
 printf("%d",LEN);
 PTR1=&S;
 PTR2=PTR2+LEN;
 *PTR2=NULL;
 PTR2--;
 while(*PTR1)
     {
      *PTR2=*PTR1;
      PTR1++;
      PTR2--;
     }
 printf("\nTHE REVERSED STRING IS :- %s",R);
 getch();
}

[/codesyntax]

SCREEN SHOTS:-

C_program_String_Reverse_Pointer

C_program_String_Reverse_Pointer_Output

 

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

Leave a Reply