On Wed, Jul 13, 2011 at 7:32 AM, Gene <gene.ress...@gmail.com> wrote:

> You can recognize a word W, recur to print the rest of the words in
> reverse, then print W:
>
> #include <stdio.h>
> #include <string.h>
>
> void print_words_in_reverse(char *s)
> {
>  char *e;
>  while (isspace(*s)) s++;
>  if (*s == '\0') return;
>  e = s + 1;
>  while (*e && !isspace(*e)) e++;
>  print_words_in_reverse(e);
>  printf("%.*s ", e - s, s);
> }
>
> int main(void)
> {
>  char line[16 * 1024];
>  fgets(line, sizeof line, stdin);
>  print_words_in_reverse(line);
>  return 0;
> }
>
>
>

Nice. Here is a similar version which, instead of printing, reverses the
string in place in O(n), if that's what's needed.

http://ideone.com/nHcQ3 <http://www.ideone.com/tA6rS>

--

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to