theme

" जीतने वाले छोड़ते नहीं और छोड़ने वाले जीतते नही "

Wednesday, July 6, 2011

Write a C program to reverse a string.

There are a number of ways one can reverse strings. Here are a few of them. These should be enough to impress the interviewer! The methods span from recursive to non-recursive (iterative).

Also note that there is a similar question about reversing the words in a sentence, but still keeping the words in place. That is


I am a good boy


would become


boy good a am I



------------------------------
.............coding......................

#include
#include
#include

void main()
{
char sh[20],ch[20],hc[20];
int k,i,j,l=0;
clrscr();
printf("enter the string");
gets(ch);
l=strlen(ch);
for(i=l-1;i>=0;i--)
{
if(ch[i]==' ')
{
for(j=i;j<=l;j++)
{
printf("%c",ch[j]);
}
l=i;
}
}
for(k=0;ch[k]!=' ';k++)
      printf("%c",ch[k]);


getch();
}

..................................

No comments:

Post a Comment