Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Reversing order of words in a given string

Reply
Thread Tools

Reversing order of words in a given string

 
 
Kelly B
Guest
Posts: n/a
 
      04-26-2007
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*?
 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      04-26-2007
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

 
Reply With Quote
 
 
 
 
Kelly B
Guest
Posts: n/a
 
      04-26-2007
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;
> }

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace stop words (remove words from a string) BerlinBrown Python 6 01-17-2008 02:37 PM
split camelcase string into array of words words pantagruel Javascript 8 07-22-2006 07:46 PM
Union reversing half words Jay Azurin C Programming 1 07-27-2004 12:27 AM
Reversing order of quicksort flipflop C Programming 3 05-28-2004 04:49 PM
Reversing the order of some loops. Dr. David Kirkby C Programming 4 10-26-2003 03:25 AM



Advertisments