One solution can be in one traversal :- take an example:- char str[]="hello my friends"; int i=strlen(str),j=i; char *srt1=malloc(sizeof(char)*i); 1. start from end of string (while i>=0,i--) 2. whenever u find any space do a. strcat(srt1,&srt[i]); b. srt[i]='\0'; d. srt[j-i]='<space>'; //replacing nulls by space, can be done using strlen(srt1) also
Note:- I have not executed the code so modifications may be needed. :) On Thu, Jul 7, 2011 at 10:58 PM, Piyush Kapoor <[email protected]> wrote: > char a[20]; > int l,i,j,k; > int main() > { > char str[100]; > gets(str); > l=strlen(str); > for(i=l-1;i>=0;){ > k=19; > a[k]='\0'; > while(i>=0 && str[i]!=' '){ > a[--k]=str[i--]; > } > printf("%s",a+k); > while(i>=0 && str[i]==' ') i--; > printf(" "); > } > } > > On Thu, Jul 7, 2011 at 10:37 PM, Piyush Kapoor <[email protected]>wrote: > >> Pls Ignore my above post.. >> >> >> On Thu, Jul 7, 2011 at 10:36 PM, Piyush Kapoor <[email protected]>wrote: >> >>> char a[20]; >>> int l; >>> int main() >>> { >>> char str[100]; >>> scanf("%s",str); >>> l=strlen(str); >>> >>> >>> } >>> -- >>> *Regards,* >>> *Piyush Kapoor,* >>> *CSE-IT-BHU* >>> >>> >> >> >> -- >> *Regards,* >> *Piyush Kapoor,* >> *CSE-IT-BHU* >> >> > > > -- > *Regards,* > *Piyush Kapoor,* > *CSE-IT-BHU* > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- **Regards SAGAR PAREEK COMPUTER SCIENCE AND ENGINEERING NIT ALLAHABAD -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
