Reversing order of words in a given string

Discussion in 'C Programming' started by Kelly B, Apr 26, 2007.

  1. Kelly B

    Kelly B Guest

    I tried to write a code which would reverse the order of words in a
    given string.
    I.e if
    Input String=The C Programming Language
    Output String=Language Programming C The

    Here is the code ..but it seems a bit "bloated" as i need an extra array
    of same size of the original string


    /* Begin Code 1*/

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #define MAX 50

    char *reverse_words(char *str1,char *str2)
    {
    char *token;
    char *rmv_blanks;
    size_t tot_len=0,curr_len=0,i=0,j;

    token=strtok(str1," ");
    while(token != NULL){
    tot_len+=strlen(token);
    curr_len=strlen(token);
    i++;

    for(j=MAX-tot_len-i;j<(MAX-tot_len-i)+curr_len;j++)
    str2[j]=*token++;

    token = strtok(NULL, " ");
    }

    rmv_blanks=str2;
    while(*(++rmv_blanks)==' ');
    return rmv_blanks;
    }

    int main(void)
    {
    char str1[MAX],str2[MAX];
    printf("Enter the string to be reversed word by word\n");

    if(fgets(str1,MAX,stdin)!=NULL){
    memset(str2,' ',sizeof(str2));
    str2[MAX-1]='\0';
    str1[strlen(str1)-1]='\0';

    printf("Original String :%s\n",str1);
    printf("Reversed String :%s\n",reverse_words(str1,str2));
    }
    else
    printf("Null String entered");
    return 0;
    }


    /*End Code 1*/

    I tried an alternative approach i.e :
    1.Reverse the entire string
    2.Reverse each string word by word

    However i cannot implement the second part correctly.The code follows here.

    /*Begin code 2*/

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #define MAX 50

    void reverse(char* begin, char* end)
    {
    char tmp;
    while(begin < end)
    {
    tmp = *begin;
    *begin = *end;
    *end = tmp;
    ++begin;
    --end;
    }
    return ;
    }


    char *reverse_words(char *str1)
    {
    char *token;
    reverse(str1,str1+strlen(str1)-1);

    token=strtok(str1," ");
    while(token != NULL)
    {
    reverse(token,token+strlen(token)-1);
    /*The problem seems to be here*/
    token = strtok(NULL, " ");
    }

    return str1;
    }


    int main(void)
    {
    char str1[MAX];
    printf("Enter the string to be reversed word by word\n");

    if(fgets(str1,MAX,stdin)!=NULL)
    {
    str1[strlen(str1)-1]='\0';
    printf("Original String :%s\n",str1);
    printf("Reversed String :%s\n",reverse_words(str1));
    }
    else
    printf("Null String entered");
    return 0;
    }
    /*end Code 2*/


    Can anyone please help me out ? I would also like to know if the first
    code is really *bloated*?
    Kelly B, Apr 26, 2007
    #1
    1. Advertising

  2. Kelly B

    CBFalconer Guest

    Kelly B wrote:
    >
    > I tried to write a code which would reverse the order of words in
    > a given string.
    > I.e if
    > Input String=The C Programming Language
    > Output String=Language Programming C The
    >
    > Here is the code ..but it seems a bit "bloated" as i need an extra
    > array of same size of the original string
    >

    .... snip ...
    >
    > Can anyone please help me out ? I would also like to know if the
    > first code is really *bloated*?


    Try writing two reversal in place routines. One use '\0' as the
    end marker, the other users isspace(c) as the marker. Now reverse
    the whole string, then reverse the words.

    --
    <http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
    <http://www.securityfocus.com/columnists/423>
    <http://www.aaxnet.com/editor/edit043.html>
    cbfalconer at maineline.net


    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Apr 26, 2007
    #2
    1. Advertising

  3. Kelly B

    Kelly B Guest

    Kelly B wrote:
    > I tried to write a code which would reverse the order of words in a
    > given string.
    > I.e if
    > Input String=The C Programming Language
    > Output String=Language Programming C The
    >



    > char *reverse_words(char *str1)
    > {
    > char *token;
    > reverse(str1,str1+strlen(str1)-1);
    >
    > token=strtok(str1," ");
    > while(token != NULL)
    > {

    *(token-1)=' ';
    /* I tried to modify it here i.e simply replaced the '\0'
    with ' '. i get the correct output now but it shows "stack around str1
    corrupted" .what exactly caused it?
    > reverse(token,token+strlen(token)-1);

    token = strtok(NULL, " ");
    > }
    >
    > return str1;
    > }
    Kelly B, Apr 26, 2007
    #3
    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. Dr. David Kirkby

    Reversing the order of some loops.

    Dr. David Kirkby, Oct 22, 2003, in forum: C Programming
    Replies:
    4
    Views:
    312
    Glen Herrmannsfeldt
    Oct 26, 2003
  2. flipflop

    Reversing order of quicksort

    flipflop, May 28, 2004, in forum: C Programming
    Replies:
    3
    Views:
    353
    Martin Ambuhl
    May 28, 2004
  3. flipflop

    Reversing order of quicksort

    flipflop, May 28, 2004, in forum: C Programming
    Replies:
    2
    Views:
    376
    Robert Wessel
    May 30, 2004
  4. Jay Azurin

    Union reversing half words

    Jay Azurin, Jul 26, 2004, in forum: C Programming
    Replies:
    1
    Views:
    305
    Barry Schwarz
    Jul 27, 2004
  5. Marcin Ciura
    Replies:
    27
    Views:
    573
    Terry Reedy
    Mar 22, 2007
Loading...

Share This Page