473,409 Members | 2,056 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

Reversing order of words in a given string

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
2 12044
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: TXSherry | last post by:
Hi, I cannot seem to wrap my brain around preg_replace. Though I've read the help file backwords and forwards. :/ Hoping someone can give me a solution here. Problem: Given string 'str'...
8
by: der | last post by:
Hello all, I've a question about order of evaluations in expressions that have && and || operators in them. The question is: will the evalution go left-to-right, no matter what -- even if the...
4
by: Dr. David Kirkby | last post by:
I have a program that loops through and changes all the elements on an array n times, so my code looks like this: for (n=1; n < n_max; ++n) for(i=imax; i >= 0; --i) { for(j=0 ; j < jmax; ++j) {...
1
by: Jay Azurin | last post by:
Hello, I'm trying to create a memory dump parser tool for which I plan to use existing header files that contain structures of registers. My plan is to use a union and include the structures in...
3
by: Ryan Taylor | last post by:
Hello. I am trying to create a regular expression that will let me know if a string has the following criteria. Order does not matter in the string, but when building a regular expression it...
20
by: dmurray14 | last post by:
Hey guys, I'm a C++ newbie here - I've messed with VB, but I mostly stick to web languages, so I find C++ to be very confusing at times. Basically, I am trying to import a text file, but I want...
4
by: hello12 | last post by:
Hello, I am new to java and i was having a hard time figuring out on how to do certain string manipulations. I was asked to read in a text file and reverse the words. So far, I have put all...
8
by: arnuld | last post by:
i have created a solutions myself. it compiles without any trouble and runs but it prints some strange characters. i am not able to find where is the trouble. ...
3
by: steezli | last post by:
Hi, Brand new to VB.NET and I'm having a problem figuring out this program. I'll try and be descritive as possible. I have to create a Windows application that contains a single top-level...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.