Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2023-02-08 03:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (40 votes). Check out past polls.

    Notices?