Reversing order of words in a given string

K

Kelly B

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*?
 
C

CBFalconer

Kelly said:
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.
 
K

Kelly B

Kelly said:
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, " ");
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top