Showing posts with label source code for reverse all words from string but not string through pointer. Show all posts
Showing posts with label source code for reverse all words from string but not string through pointer. Show all posts

Thursday, 2 August 2012

Reverse all words but not string using pointer

Q. Write a C program to accept a string from user and reverse all words but not string using pointer.


For example:


String: This Is A Good Blog
Result: sihT sI A dooG golB


Ans.

/*c program for reverse all words but not string using pointer*/
#include<stdio.h>
#include<conio.h>
int main()
{
 char str[30];
 char *p,*tmp;
 printf("Enter any string: ");
 gets(str);
 for(p=str; *p!='\0'; p++)
 {
  if(*(p+1)==' ' || *(p+1)=='\0')
  {
   for(tmp=p; *tmp!=' ' && tmp>=str; tmp--)
     printf("%c",*tmp);
  }
  printf(" ");
 }
 getch();
 return 0;
}


/************Output************/


Output of reverse all words but not string using pointer C program
Screen shot for reverse all words in string but string in same order C program