By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,581 Members | 1,966 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,581 IT Pros & Developers. It's quick & easy.

Reversing order of words in a given string

P: n/a
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*?
Apr 26 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

Apr 26 '07 #2

P: n/a
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;
}
Apr 26 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.