Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Here's a version in plain ANSI C. Yikes, that looks bad compared to the one liners. :-)

#include <stdio.h> #include <stdlib.h> #include <string.h> /* reverse a string in place, return str */ static char* reverse(char* str) { char* left = str; char* right = left + strlen(str) - 1; char tmp; while (left < right) { tmp = *left; *left++ = *right; *right-- = tmp; } return str; } static int reverseWords( const char* instr, /* in: string of words */ char* outstr) /* out: reversed words */ /* (caller must ensure big enough) */ { char* p; char* buf; *outstr = '\0'; if ((buf = (char*)malloc(strlen(instr)+1)) == NULL) { fprintf(stderr, "oops, out of memory\n"); return -1; } strcpy(buf, instr); reverse(buf); if ((p = strtok(buf, " \t")) == NULL) { free(buf); return 0; } strcpy(outstr, reverse(p)); outstr += strlen(p); while ((p = strtok(NULL, " \t")) != NULL) { *outstr++ = ' '; strcpy(outstr, reverse(p)); outstr += strlen(p); } free(buf); return 0; } int main() { char instr[256]; char outstr[256]; strcpy(instr, " one two \t three four "); reverseWords(instr, outstr); printf("in='%s' out='%s'\n", instr, outstr); return 0; }

Updated 17-Dec: Added improved version without the ugly malloc/strtok below:

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> static const char* get_next_word(const char* pstart, const char* p, si +ze_t* len) { const char* pend; while (p >= pstart && isspace(*p)) --p; if (p < pstart) return NULL; pend = p-- + 1; while (p >= pstart && !isspace(*p)) --p; *len = pend - ++p; return p; } static void reverseWords( const char* instr, /* in: string of words */ char* outstr) /* out: reversed words */ /* (caller must ensure big enough) */ { const char* p = instr + strlen(instr) - 1; size_t len; if ((p = get_next_word(instr, p, &len)) == NULL) { *outstr = '\0'; return; } memcpy(outstr, p, len); outstr += len; while ((p = get_next_word(instr, --p, &len)) != NULL) { *outstr++ = ' '; memcpy(outstr, p, len); outstr += len; } *outstr = '\0'; return; } int main() { char instr[256]; char outstr[256]; strcpy(instr, " one two \t three four "); reverseWords(instr, outstr); printf("in='%s' out='%s'\n", instr, outstr); return 0; }


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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (3)
    As of 2014-09-20 03:59 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (152 votes), past polls