Reverse word order to read it from right to left

Discussion in 'C Programming' started by lovecreatesbea...@gmail.com, Oct 2, 2007.

  1. Guest

    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;
    }
     
    , Oct 2, 2007
    #1
    1. Advertising

  2. osmium Guest

    "" 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;
    }
     
    osmium, Oct 2, 2007
    #2
    1. Advertising

  3. Guest

    On Oct 2, 11:58 am, ""
    <> 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
     
    , Oct 2, 2007
    #3
  4. user923005 Guest

    On Oct 2, 11:58 am, ""
    <> 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.
     
    user923005, Oct 2, 2007
    #4
  5. CBFalconer Guest

    osmium wrote:
    > "" 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
     
    CBFalconer, Oct 3, 2007
    #5
  6. Thad Smith Guest

    osmium wrote:
    > "" 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
     
    Thad Smith, Oct 3, 2007
    #6
  7. osmium Guest

    "osmium" wrote:

    > "" 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.
     
    osmium, Oct 3, 2007
    #7
  8. "osmium" <> 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) <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"
     
    Keith Thompson, Oct 3, 2007
    #8
  9. Guest

    On Oct 4, 4:23 am, Keith Thompson <> wrote:
    > "osmium" <> 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...
     
    , Oct 3, 2007
    #9
  10. santosh Guest

    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
    $
     
    santosh, Oct 4, 2007
    #10
  11. santosh Guest

    santosh wrote:

    > 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" */
    >> /*********************************************************************/


    > <snip code & 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;
    > }


    Sorry to "lovecreatesbeauty" for presenting flawed code. The above function
    invokes undefined behaviour when the pointers 'i' and 'j' are decremented
    below the value of 'src.' The fix though is simple and I leave it as an
    exercise for the OP.

    <snip rest>
     
    santosh, Oct 4, 2007
    #11
  12. Guest

    On Oct 4, 1:21 pm, santosh <> wrote:
    > santosh wrote:

    [...]
    > > /* 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;
    > > }

    >
    > Sorry to "lovecreatesbeauty" for presenting flawed code. The above function
    > invokes undefined behaviour when the pointers 'i' and 'j' are decremented
    > below the value of 'src.' The fix though is simple and I leave it as an
    > exercise for the OP.


    Please come to my code first. I update my code, this version fixes the
    previous bug.

    The prototypes of our functions are different. you use two parameters
    and don't change the original argument.

    I use only one parameter and alter the original copy of argument. Is
    it bad style?


    /
    *******************************************************************************
    * Reverse order of space separated words, for example:
    * "hello code monkey" becomes "monkey code hello"

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

    char *wrdrvs(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;
    }
     
    , Oct 4, 2007
    #12
  13. Guest

    On Oct 4, 12:09 pm, santosh <> wrote:
    > wrote:

    [...]
    > /* 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--;


    i may point to one past the first array element

    > k = j = i;
    > }
    > else {
    > while (!isspace((unsigned char)*j) && j >= src) k = --j;


    so does j

    > for (j += 1; j <= i; j++) *dest++ = *j;
    > i = j = k;
    > }
    > }
    > *dest = '\0';
    > return;
    >
    > }
     
    , Oct 4, 2007
    #13
  14. santosh Guest

    wrote:

    > On Oct 4, 12:09 pm, santosh <> wrote:
    >> wrote:

    > [...]
    >> /* 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--;

    >
    > i may point to one past the first array element
    >
    >> k = j = i;
    >> }
    >> else {
    >> while (!isspace((unsigned char)*j) && j >= src) k = --j;

    >
    > so does j


    Yes, I acknowledged it in the other post. The fix is simple though.
     
    santosh, Oct 4, 2007
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dogbite
    Replies:
    4
    Views:
    694
    osmium
    Oct 10, 2003
  2. =?Utf-8?B?QmlzaG95?=
    Replies:
    0
    Views:
    998
    =?Utf-8?B?QmlzaG95?=
    Dec 28, 2006
  3. =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=

    Help on table align on left of page vs left hanging indent

    =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=, Jul 10, 2007, in forum: XML
    Replies:
    2
    Views:
    1,011
    =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=
    Jul 16, 2007
  4. karthikbalaguru
    Replies:
    9
    Views:
    672
    JosephKK
    Mar 22, 2009
  5. Oran
    Replies:
    2
    Views:
    550
Loading...

Share This Page