Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: Five Ways to Reverse a String of Words (ANSI C version)

by oiskuu (Hermit)
on Mar 06, 2015 at 15:13 UTC ( #1119048=note: print w/replies, xml ) Need Help??


in reply to Re: Five Ways to Reverse a String of Words (ANSI C version)
in thread Five Ways to Reverse a String of Words (C#, Perl 5, Perl 6, Ruby, Haskell)

A somewhat condensed, C99 version:

#include <stdio.h> #include <ctype.h> #include <string.h> char *str_rwords(char *s) { int n = strlen(s); char t[n], *e = t + n, *d = e, *s0; do { for (s0 = s; *s && isalpha(*s); s++); if ((n = s - s0)) { if (d != e) *--d = ' '; d = memcpy(d - n, s0, n); } } while (*s++); return strndup(d, e - d); } int main(void) { puts(str_rwords(" one two \t three four ")); }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1119048]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2021-01-16 02:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?