Saturday, May 22, 2010

How to write C program for finding reverse of a string?

# include %26lt;conio.h%26gt;


# include %26lt;stdio.h%26gt;


# include %26lt;string.h%26gt;





void main()


{


char *str,*rev;


int n,i,cnt=0;


n=strlen(str);





printf ("Enter any string : ");


gets(str);





for (i=n; i%26lt;=0 ;i--)


{


rev[cnt]=str[i];


cnt++;


}


rev[cnt]='\0';





printf ("\n\nReverse string : %s",rev);


getch();


}

How to write C program for finding reverse of a string?
this sounds like homework, so I'll just give you an idea.





put the string in an array.





ie "string"





s[0] = 's'


s[1] = 't'


s[2] = 'r'


s[3] = 'i'


s[4] = 'n'


s[5] = 'g'





make a variable, say x, the size of the array. output the string backwards, starting from x while x %26gt;= 0.





5 - g


4 - n


3 - i


2 - r


1 - t


0 - s


No comments:

Post a Comment