This is the program to reverse the given string and display. The program internally uses the logic of reversing the word.
Logic: The approach here is to reverse the string using the pointers. Reversing the string includes the reversing the each and every words in it. After accepting a string from user, it calls a function “strev” with two string pointer arguments, the source and destination. It has an iterative loop, which traces from the EOL through the beginning. Each time it copies the current letter to the destination. Finally it displays the resultant string.
The earlier program implements the same by direct method, i.e. without pointers.
#include<stdio.h>
#include<conio.h>
void strev(char *str1, char *str2);
void main()
{
char *str1, *str2;
clrscr();
printf("nnt ENTER A STRING...: ");
gets(str1);
strev(str1,str2);
printf("nt THE REVERSED STRING IS...: ");
puts(str2);
getch();
}
void strev(char *str1, char *str2)
{
int i = 0, len = 0, r = 0;
while(*(str1+len)!=' ')
len++;
for(i=len-1; i>=0; i--)
{
*(str2+r) = *(str1+i);
r++;
}
*(str2+r) = ' ';
}
Description :
This is the one stop educational site for all Electronic and Computer students. If you want to learn something new then we are here to help. We work on Microcontroller projects, Basic Electronics, Digital electronics, Computer projects and also in basic c/c++ programs.
#Home #Sitemap #Submit #Terms of Use
Copyright©2011 electrofriends.com All Rights Reserved
Contact:info@electrofriends.com | Powered by Dhyeya
January 24th, 2009 at 10:48 am
i need codes for yours projects
so please send me full details
don’t forget me
thank uuuuuuuuuuuuuuuuuuuuu
May 2nd, 2011 at 11:18 am
char *s1;
gets(s1);
this is not correct as s1 is not initialized. its giving segmentation fault.
August 1st, 2011 at 11:41 am
void main(void)
{
uchar *str “ABCDE”;
uchar len;
uchar newidex = 0;
len=0;
while( *str != ” ) len++;
str = revstr(str, len, newidex);
PRINT(str);
}
uint* revstr(uchar *str, uchar len, uchar &index)
{
uchar *b;
if(len==0)
{
return b;
}
else
{
b[index++] = str[len];
revstr( str, len-1, index );
}
}
August 1st, 2011 at 12:00 pm
correction in above code is:
while( *(str+len) != ” ) len++;