C program- To print reverse of a string using pointer

Posted: January 14, 2012 in Lab 1
#include<stdio.h>
#include<conio.h>
void strev(char *str1, char *str2);
void main()
{
char str1[100],str2[100];
printf("ENTER A STRING\n");
gets(str1);
strev(str1,str2);
printf("THE REVERSED STRING IS\n");
puts(str2);
}

void strev(char *str1, char *str2)
{
int i = 0, len = 0, r = 0;
while(*(str1+len)!='\0')
len++;
for(i=len-1;i>=0; i--)
{
*(str2+r) = *(str1+i);
r++;
}
*(str2+r) = '\0';
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s