The need:
I don't know what could be the need of this program but I found this question on many sites and thats why I wrote this program.
The code:
----------------------------------------------------------
#include<stdio.h> #include<string.h> void reverse(char *str,int start, int end) { char temp; int i,j; i=end-start; for(j=0;j<=i/2;j++) { temp=*(str+start+j); *(str+start+j)=*(str+start+i-j); *(str+start+i-j)=temp; } } rev_word(char *str) { int start=0,end=0,i=0; while(*(str+i)!='\0') { if(*(str+i)==' ') { end=i-1; reverse(str, start, end); i++; start=i; } else i++; } end=i-1; reverse(str, start, end); } int main() { char a[]="I dont know why useless programs are asked"; printf("before\n%s\n",a); rev_word(a); printf("after\n%s\n",a); return 0; }
----------------------------------------------------------
Approach:The approach is simple. make a note of start and end points (indices) of each word and reverse with the help of those points. The UDF reverse() does the job here.
No comments:
Post a Comment
feel free to ask your doubts... if any
you can post your doubts on
www.facebook.com/programsimply