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;
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
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
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|