Reverse a String using Pointer in C

String reverse means reverses the order of all the characters in a string. String are stored in Array of type char, with the end of the string marked by the null character.

 

#include<stdio.h>

#include<conio.h>

char strr[]=" i am rekha";                /*define & initializing a string of characters*/

char revv[20];    

void main()

{

char *s,*r;

clrscr();

s=strr;

r=revv;

/* we should move to the last position in the strr*/

while(*s!='\0')

{

s++;

}

/*Now ā€˜sā€™ is pointing to the ā€˜\0ā€™ character, so we need to decrement s to point to the previous character*/

s--;

while(s>=&strr[0]) /*copy char by char from strr to revv*/

{

*r=*s;

s--;

r++;

}

*r='\0';

printf("\n the string after reversing is %s",revv);

getch();

}

output..

Reverse String

About Us

Rekha SetiaRekha Setia is a passionate blogger of Extra Computer Notes. if you have any ideas or any request me @