Reverse word order to read it from right to left - C

This is a discussion on Reverse word order to read it from right to left - C ; I want to write a function to reverse the order of words to make it read from right to left, but it doesn't work. Can you help me and point out my mistakes. Thank you for your time. #include <string.h> ...

+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 14

Reverse word order to read it from right to left

  1. Default Reverse word order to read it from right to left

    I want to write a function to reverse the order of words to make it
    read from right to left, but it doesn't work. Can you help me and
    point out my mistakes. Thank you for your time.


    #include <string.h>
    #include <ctype.h>

    /* Reverse order of words in a sentence in a string to read it from
    right to
    * left. * For example:
    *
    * " hello world !! ! "
    *
    * becomes
    *
    * " ! !! world hello "
    *
    * (heading and tailing blanks should be reserved):
    */

    char *rttolt(char str[])
    {
    enum STATUS{SPC, NSPC, UNDEF};
    char *p1 = str, *p2, *p3, ch;
    int len = strlen(str);
    enum STATUS prestt;

    if (!str)
    return NULL;
    prestt = UNDEF;
    for (; len != 0; len--){
    if (!isspace(str[len - 1])){
    switch ((int)prestt){
    case SPC:
    for (p3 = &str[len]; isspace(*p3); p3++){
    ch = *p3;
    for (p2 = &str[len - 1]; p2 >= p1; p2--)
    *(p2 + 1) = *p2;
    *p2 = ch;
    p1++;
    }
    break;
    }
    prestt = NSPC;
    } else if (isspace(str[len - 1])){
    switch ((int)prestt){
    case NSPC:
    for (p3 = &str[len]; !isspace(*p3); p3++){
    ch = *p3;
    for (p2 = &str[len - 1]; p2 >= p1; p2--)
    *(p2 + 1) = *p2;
    *p2 = ch;
    p1++;
    }
    break;
    }
    prestt = SPC;
    }
    }
    return str;
    }

    #include <stdio.h>
    int main(void)
    {
    char a[] = "hello world !";

    printf("%s, %d\n", a, strlen(a));
    rttolt(a);
    printf("%s, %d\n", a, strlen(a));
    return 0;
    }


  2. Default Re: Reverse word order to read it from right to left

    "lovecreatesbea...@gmail.com" wrote:

    >I want to write a function to reverse the order of words to make it
    > read from right to left, but it doesn't work. Can you help me and
    > point out my mistakes. Thank you for your time.


    <snip>

    You forgot to explain the theory of operation. Should it work on n words
    of just two words? Do the words have to be the same size? You seem to have
    a fondness for switch statements that don't seem necessary. Some compilers
    may accept the definition you have for the array, a. They don't have to.

    Add a PNT macro and play around with where you call it. Change the data
    sample to something more general and without punctuation for the early
    attempts. I see non alphabetics creeping into the output.

    If all else fails, add some commentary and repost.

    Slight modifications embedded.

    #include <string.h>
    #include <ctype.h>

    /* Reverse order of words in a sentence in a string to read it from
    right to
    * left. * For example:
    *
    * " hello world !! ! "
    *
    * becomes
    *
    * " ! !! world hello "
    *
    * (heading and tailing blanks should be reserved):
    */

    #define PNT printf("debug %s\n", str)


    char *rttolt(char str[])
    {
    enum STATUS{SPC, NSPC, UNDEF};
    char *p1 = str, *p2, *p3, ch;
    int len = strlen(str);
    enum STATUS prestt;

    if (!str)
    return NULL;
    prestt = UNDEF;
    for (; len != 0; len--){
    if (!isspace(str[len - 1])){
    switch ((int)prestt){
    case SPC:
    for (p3 = &str[len]; isspace(*p3); p3++){
    ch = *p3;
    for (p2 = &str[len - 1]; p2 >= p1; p2--)
    *(p2 + 1) = *p2;
    *p2 = ch;
    p1++;
    //PNT;
    }
    //PNT;
    break;
    }
    prestt = NSPC;
    } else if (isspace(str[len - 1])){
    switch ((int)prestt){
    case NSPC:
    for (p3 = &str[len]; !isspace(*p3); p3++){
    ch = *p3;
    for (p2 = &str[len - 1]; p2 >= p1; p2--)
    *(p2 + 1) = *p2;
    *p2 = ch;
    p1++;
    PNT;
    }
    PNT;
    break;
    }
    prestt = SPC;
    }
    }
    return str;
    }

    #include <stdio.h>
    int main(void)
    {
    char a[] = "abc defgh";

    printf("%s, %d\n", a, strlen(a));
    rttolt(a);
    printf("%s, %d\n", a, strlen(a));
    getchar();
    return 0;
    }



  3. Default Re: Reverse word order to read it from right to left

    On Oct 2, 11:58 am, "lovecreatesbea...@gmail.com"
    <lovecreatesbea...@gmail.com> wrote:
    > I want to write a function to reverse the order of words to make it
    > read from right to left, but it doesn't work. Can you help me and
    > point out my mistakes. Thank you for your time.
    >
    > #include <string.h>
    > #include <ctype.h>
    >
    > /* Reverse order of words in a sentence in a string to read it from
    > right to
    > * left. * For example:
    > *
    > * " hello world !! ! "
    > *
    > * becomes
    > *
    > * " ! !! world hello "
    > *
    > * (heading and tailing blanks should be reserved):
    > */
    >
    > char *rttolt(char str[])
    > {
    > enum STATUS{SPC, NSPC, UNDEF};
    > char *p1 = str, *p2, *p3, ch;
    > int len = strlen(str);
    > enum STATUS prestt;
    >
    > if (!str)
    > return NULL;
    > prestt = UNDEF;
    > for (; len != 0; len--){
    > if (!isspace(str[len - 1])){
    > switch ((int)prestt){
    > case SPC:
    > for (p3 = &str[len]; isspace(*p3); p3++){
    > ch = *p3;
    > for (p2 = &str[len - 1]; p2 >= p1; p2--)
    > *(p2 + 1) = *p2;
    > *p2 = ch;
    > p1++;
    > }
    > break;
    > }
    > prestt = NSPC;
    > } else if (isspace(str[len - 1])){
    > switch ((int)prestt){
    > case NSPC:
    > for (p3 = &str[len]; !isspace(*p3); p3++){
    > ch = *p3;
    > for (p2 = &str[len - 1]; p2 >= p1; p2--)
    > *(p2 + 1) = *p2;
    > *p2 = ch;
    > p1++;
    > }
    > break;
    > }
    > prestt = SPC;
    > }
    > }
    > return str;
    >
    > }
    >
    > #include <stdio.h>
    > int main(void)
    > {
    > char a[] = "hello world !";
    >
    > printf("%s, %d\n", a, strlen(a));
    > rttolt(a);
    > printf("%s, %d\n", a, strlen(a));
    > return 0;
    >
    >


    A much cleaner, shorter, and more understandable algorithm:
    1) invert the entire sentence
    2) for each whitespace delimited substring in the sentence,
    invert the substring.
    --
    Fred Kleinschmidt


  4. Default Re: Reverse word order to read it from right to left

    On Oct 2, 11:58 am, "lovecreatesbea...@gmail.com"
    <lovecreatesbea...@gmail.com> wrote:
    > I want to write a function to reverse the order of words to make it
    > read from right to left, but it doesn't work. Can you help me and
    > point out my mistakes. Thank you for your time.

    [snip]
    Push the words on a lifo and pop them when you have read all the
    material.



  5. Default Re: Reverse word order to read it from right to left

    osmium wrote:
    > "lovecreatesbea...@gmail.com" wrote:
    >
    >> I want to write a function to reverse the order of words to make
    >> it read from right to left, but it doesn't work. Can you help me
    >> and point out my mistakes. Thank you for your time.

    >
    > <snip>
    >
    > You forgot to explain the theory of operation. Should it work on
    > n words of just two words? Do the words have to be the same size?
    > You seem to have a fondness for switch statements that don't seem
    > necessary. Some compilers may accept the definition you have for
    > the array, a. They don't have to.


    The OP could take a look at freverse.c, a test program for the
    ggets general purpose string input routine. See ggets.zip at:

    <http://cbfalconer.home.att.net/download/>

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>



    --
    Posted via a free Usenet account from http://www.teranews.com


  6. Default Re: Reverse word order to read it from right to left

    osmium wrote:
    > "lovecreatesbea...@gmail.com" wrote:
    >
    >> I want to write a function to reverse the order of words to make it
    >> read from right to left, but it doesn't work. Can you help me and
    >> point out my mistakes. Thank you for your time.

    >
    > <snip>
    >
    > You forgot to explain the theory of operation. Should it work on n words
    > of just two words?


    With the definition given, "reverse the order of words" implies all the
    words, unless otherwise specified.

    > Do the words have to be the same size?


    Without specification to that effect, that wouldn't be required.

    The biggest problem in the lack of definition of a word. Is
    "thirty-three" one word or two? In standard English it is two, but
    co-op is one word. What about non-alphabetic [single word] characters?
    Is each one individually reversed or are any combinations of
    non-alphabetic characters kept in the same order?

    --
    Thad

  7. Default Re: Reverse word order to read it from right to left

    "osmium" wrote:

    > "lovecreatesbea...@gmail.com" wrote:
    >
    >>I want to write a function to reverse the order of words to make it
    >> read from right to left, but it doesn't work. Can you help me and
    >> point out my mistakes. Thank you for your time.

    >
    > <snip>


    > You seem to have a fondness for switch statements that don't seem
    > necessary.


    I think you have in your mind the notion of a state machine and three
    states, (SPC, NSPC, UNDEF) but you misunderstand the switch statement. For
    example, here is an extract of one of your two *separate* switch statements.

    switch ((int)prestt)
    {
    case SPC:
    for (p3 = &str[len]; isspace(*p3); p3++)
    {
    ch = *p3;
    for (p2 = &str[len - 1]; p2 >= p1; p2--)
    *(p2 + 1) = *p2;
    *p2 = ch;
    p1++;
    }
    break;
    } // end of switch

    where presstt contains the state The break at the end looks like it makes
    sense, but look again. What effect can it possibly have? None.

    prestt is either SPC or it's not, a simple if would be sufficient. So my
    point was, either get rid of the switches or make them useful.

    I suggest you rethink your logic and recode. Note that the simplest test
    would be "a b" where the two words are a and b - get that working first.
    The method prrposed by Fred Kleinschmidt sounds good to me for a student
    exercies with no punctuation to handle other than the space.

    To continue on your present path I think you want something like

    switch(prestt)
    (
    case: SPC
    case NSPC:
    case:UNDEF:
    } // end switch

    Fill in the missing stuff.



  8. Default Re: Reverse word order to read it from right to left

    "osmium" <r124c4u102@comcast.net> writes:
    [...]
    > I think you have in your mind the notion of a state machine and three
    > states, (SPC, NSPC, UNDEF) but you misunderstand the switch statement. For
    > example, here is an extract of one of your two *separate* switch statements.
    >
    > switch ((int)prestt)
    > {
    > case SPC:
    > for (p3 = &str[len]; isspace(*p3); p3++)
    > {
    > ch = *p3;
    > for (p2 = &str[len - 1]; p2 >= p1; p2--)
    > *(p2 + 1) = *p2;
    > *p2 = ch;
    > p1++;
    > }
    > break;
    > } // end of switch
    >
    > where presstt contains the state The break at the end looks like it makes
    > sense, but look again. What effect can it possibly have? None.
    >
    > prestt is either SPC or it's not, a simple if would be sufficient. So my
    > point was, either get rid of the switches or make them useful.

    [...]

    Certainly the OP's switch statement is poorly written, in several
    ways. However, I habitually put a ``break;'' at the end of each case
    of a switch statement, even if it does nothing. I think of the
    ``break;'' as being almost part of the syntax of a switch statement.
    (Unless I want to fall through, but that's unusual, and if I do that I
    add a prominent comment.)

    For example:

    switch (ch) {
    case 'a': case 'A':
    /* blah blah */
    break;
    case 'b': case 'B':
    /* blah blah */
    break;
    default:
    /* blah blah */
    break;
    }

    --
    Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

  9. Default Re: Reverse word order to read it from right to left

    On Oct 4, 4:23 am, Keith Thompson <ks...@mib.org> wrote:
    > "osmium" <r124c4u...@comcast.net> writes:
    >
    > [...]
    >
    >
    >
    > > I think you have in your mind the notion of a state machine and three
    > > states, (SPC, NSPC, UNDEF) but you misunderstand the switch statement. For
    > > example, here is an extract of one of your two *separate* switch statements.

    >
    > > switch ((int)prestt)
    > > {
    > > case SPC:
    > > for (p3 = &str[len]; isspace(*p3); p3++)
    > > {
    > > ch = *p3;
    > > for (p2 = &str[len - 1]; p2 >= p1; p2--)
    > > *(p2 + 1) = *p2;
    > > *p2 = ch;
    > > p1++;
    > > }
    > > break;
    > > } // end of switch

    >
    > > where presstt contains the state The break at the end looks like it makes
    > > sense, but look again. What effect can it possibly have? None.

    >
    > > prestt is either SPC or it's not, a simple if would be sufficient. So my
    > > point was, either get rid of the switches or make them useful.

    >
    > [...]
    >
    > Certainly the OP's switch statement is poorly written, in several
    > ways. However, I habitually put a ``break;'' at the end of each case
    > of a switch statement, even if it does nothing. I think of the
    > ``break;'' as being almost part of the syntax of a switch statement.
    > (Unless I want to fall through, but that's unusual, and if I do that I
    > add a prominent comment.)
    >
    > For example:
    >
    > switch (ch) {
    > case 'a': case 'A':
    > /* blah blah */
    > break;
    > case 'b': case 'B':
    > /* blah blah */
    > break;
    > default:
    > /* blah blah */
    > break;
    > }
    >


    Thank you for the suggestion.

    Now I come up with my new code shown as follow. It has some
    improvement. It works with space separated sentence. The sad thing is
    that it doesn't work with input like:
    char a2[] = "a b c d";
    char a4[] = "a b c d";

    but it still works with input like:
    char a3[] = " a b c d ";
    char a[] = " hello code monkey ";

    I print out a test result diagram as following.



    /
    *******************************************************************************
    * Reverse order of words in a string to read it from right to left.
    For example
    * "hello code monkey" becomes "monkey code hello"

    ******************************************************************************/

    char *rttolt(char *p)
    {
    enum STATUS {SPC, NSPC, UNDEF} prestt = UNDEF;
    const char BLK = ' ';
    char *p1, *p2, *p3, ch;

    for (p1 = p; *p1; p1++)
    ;
    p1--;
    prestt = UNDEF;
    for (p2 = p; p2 != p1; p2++){
    if (*p2 != BLK){
    if (prestt == SPC){
    while (p2 > p){
    ch = *(p2 - 1);
    for (p3 = p2; p3 <= p1; p3++)
    *(p3 - 1) = *p3;
    *--p3 = ch;
    p2--, p1--;
    }
    }
    prestt = NSPC;
    } else if (*p2 == BLK){
    if (prestt == NSPC){
    while (p2 > p){
    ch = *(p2 - 1);
    for (p3 = p2; p3 <= p1; p3++)
    *(p3 - 1) = *p3;
    *--p3 = ch;
    p2--, p1--;
    }
    }
    prestt = SPC;
    }
    }
    return p;
    }


    #include <stdio.h>
    int main(void)
    {
    char a2[] = "a b c d";
    char a4[] = "a b c d";

    char a3[] = " a b c d ";
    char a[] = " hello code monkey ";

    printf(" [%s]\n", a);
    rttolt(a);
    printf(" [%s]\n", a);
    return 0;
    }



    Run: D:\working\c\a.out
    [ hello code monkey ]
    35: [hello code monkey ]
    51: [hell code monkey o ]
    51: [hel code monkey lo ]
    51: [he code monkey llo ]
    51: [h code monkey ello ]
    51: [ code monkey hello ]
    35: [ code monkey hello ]
    35: [code monkey hello ]
    51: [cod monkey e hello ]
    51: [co monkey de hello ]
    51: [c monkey ode hello ]
    51: [ monkey code hello ]
    35: [ monkey code hello ]
    35: [monkey code hello ]
    51: [monke y code hello ]
    51: [monk ey code hello ]
    51: [mon key code hello ]
    51: [mo nkey code hello ]
    51: [m onkey code hello ]
    51: [ monkey code hello ]
    [ monkey code hello ]

    Press the Enter key to return to Source Insight...


  10. Default Re: Reverse word order to read it from right to left

    lovecreatesbea...@gmail.com wrote:
    [snip suggestions from osmium and Keith Thompson]

    > Thank you for the suggestion.
    >
    > Now I come up with my new code shown as follow. It has some
    > improvement. It works with space separated sentence. The sad thing is
    > that it doesn't work with input like:
    > char a2[] = "a b c d";
    > char a4[] = "a b c d";
    >
    > but it still works with input like:
    > char a3[] = " a b c d ";
    > char a[] = " hello code monkey ";
    >
    > I print out a test result diagram as following.
    >
    > /********************************************************************
    > * Reverse order of words in a string to read it from right to left.
    > For example
    > * "hello code monkey" becomes "monkey code hello" */
    > /*********************************************************************/
    >
    > char *rttolt(char *p)
    > {
    > enum STATUS {SPC, NSPC, UNDEF} prestt = UNDEF;
    > const char BLK = ' ';
    > char *p1, *p2, *p3, ch;
    >
    > for (p1 = p; *p1; p1++)
    > ;
    > p1--;
    > prestt = UNDEF;
    > for (p2 = p; p2 != p1; p2++){
    > if (*p2 != BLK){
    > if (prestt == SPC){
    > while (p2 > p){
    > ch = *(p2 - 1);
    > for (p3 = p2; p3 <= p1; p3++)
    > *(p3 - 1) = *p3;
    > *--p3 = ch;
    > p2--, p1--;
    > }
    > }
    > prestt = NSPC;
    > } else if (*p2 == BLK){
    > if (prestt == NSPC){
    > while (p2 > p){
    > ch = *(p2 - 1);
    > for (p3 = p2; p3 <= p1; p3++)
    > *(p3 - 1) = *p3;
    > *--p3 = ch;
    > p2--, p1--;
    > }
    > }
    > prestt = SPC;
    > }
    > }
    > return p;
    > }
    >
    >
    > #include <stdio.h>
    > int main(void)
    > {
    > char a2[] = "a b c d";
    > char a4[] = "a b c d";
    >
    > char a3[] = " a b c d ";
    > char a[] = " hello code monkey ";
    >
    > printf(" [%s]\n", a);
    > rttolt(a);
    > printf(" [%s]\n", a);
    > return 0;
    > }


    <snip output>

    Why so complex. Here is a simpler version that, AFAICT, conforms to your
    specification.

    /* reverse the word order in 'src' to 'dest' */
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>

    void wrdrev(char *src, char *dest)
    {
    char *i, *j, *k;

    i = j = k = src + (strlen(src)-1);
    while (i >= src) {
    if (isspace((unsigned char)*i)) {
    *dest++ = *i--;
    k = j = i;
    }
    else {
    while (!isspace((unsigned char)*j) && j >= src) k = --j;
    for (j += 1; j <= i; j++) *dest++ = *j;
    i = j = k;
    }
    }
    *dest = '\0';
    return;
    }

    int main(void)
    {
    char a[] = " mad code monkey !! ";
    char b[] = " mad code monkey !! ";
    wrdrev(a, b);
    printf("a = %s\nb = %s\n", a, b);
    return 0;
    }

    $ ./wrdrev
    a = mad code monkey !!
    b = !! monkey code mad
    $


+ Reply to Thread
Page 1 of 2 1 2 LastLast

Similar Threads

  1. Reverse order of data in a particular dimension
    By Application Development in forum Fortran
    Replies: 2
    Last Post: 08-09-2007, 06:45 PM
  2. prints in reverse order
    By Application Development in forum Adobe Acrobat
    Replies: 0
    Last Post: 12-15-2006, 08:31 PM
  3. Any easy way to show a JList in reverse order?
    By Application Development in forum Java
    Replies: 2
    Last Post: 11-04-2006, 08:07 AM
  4. Join two lines in reverse order
    By Application Development in forum Editors
    Replies: 5
    Last Post: 07-01-2006, 06:12 AM
  5. MMX reverse byte order instr.
    By Application Development in forum ASM x86 ASM 370
    Replies: 5
    Last Post: 05-31-2006, 02:14 AM