Sunday, August 2, 2009

How to write reverse a string program in C ?

/*without using standard functions*/





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





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





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





main()





{





char s[40],c,r[40];





int i,j,p;





clrscr();





printf("Enter the string:");





gets(s);





for(i=0;s[i]!='';i++);





j=i;





printf("The original string is:%s",s);





printf("nThe reverse string is:");





for(p=j-1,i=0;p%26gt;=0;p--,i++)





r[i]=s[p];





r[i]='';





printf("The reverse string is:%s",r);





getch();





}

How to write reverse a string program in C ?
u have a predefined function in string.h library function.


the function is strrev(*char)
Reply:#include %26lt;stdio.h%26gt;


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





int main(void)


{


char str1[] = "Pointers are fun and hard to use";


char str2[80], *p1, *p2;





/* make p point to end of str1 */


p1 = str1 + strlen(str1) - 1;





p2 = str2;





while(p1 %26gt;= str1)


*p2++ = *p1--;





/* null terminate str2 */


*p2 = '\0';





printf("%s %s", str1, str2);





return 0;


}
Reply:Check this out


http://discuss.fogcreek.com/techintervie...


http://en.allexperts.com/q/C-1587/C-prog...





both of them are working.
Reply:i think you should use string constant " ' " symbole





OMAR QURESHI


momarqureshi@yahoo.com
Reply:void main()


{


char string1[ ]="this string needs to be revresed";


char string2[35];





/* now calculate length of the string. You can also use strlen() function if allowed */


int i,len;


for(len=0;string1[len]!=NULL;len++);





for(i=0;string1[i]!=NULL;i++)


string2[i]=string1[len-i-1];


string1[len]=NULL; /*terminate the reversed string*/





printf("The reversed string is:\n%s",string2);





}


No comments:

Post a Comment