English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

write a program that takes a line at a time and reverses the words of the lines.

ex. input: i am confused
output: confused am i


ex.2 input: she is beautiful
output: beautiful is she
the data should contain one blank between each word.

any idea on how to solve this problem?
please give some ideas... how to solve this in c programming language...?

2007-02-25 16:44:34 · 6 answers · asked by Anonymous in Computers & Internet Programming & Design

6 answers

Once you have the sentence as a string, you could use sscanf() to read each word. A space in the format string will skip whitespace, and I recall that you could use a scanset, if necessary. However, if you can use gets(), then go right ahead.

As for reversing the word order, the most elegant way is to use a stack. However, rather than implement your own stack, I suggest using the function call stack, in conjunction with recursion.

Now, I'm not going to do your assignment for you, but the idea is write a function that reads a word from the string, prints it, and calls itself with a new string that has the word just printed removed. There are problems to be solved just with that. In addition, you'll need to figure out in which order these tasks should be performed.

It's not clear to me that this is the solution your instructor was going for, but it would certainly be the most elegant solution. You'll learn a lot if you try it.

2007-03-05 13:37:52 · answer #1 · answered by arbeit 4 · 0 0

First of all divide the sentences into 2d array. by separting the words at that time of occuring the space. And finally print the 2d array reversely. This is the logic. Mail me if you are in doubt.

gettingmyid_sri@yahoo.co.in
bsridharmca@gmail.com

2007-02-26 02:00:16 · answer #2 · answered by sridhar b 2 · 0 0

Use scanf to read in a string.
Use the strtok function to get the words out of the string.
Then printf them out in reverse order.

2007-02-26 00:55:46 · answer #3 · answered by data_disaster 2 · 0 0

write a module to reverse a given string
"i am confused" should become "desufnoc ma i"
Then split the string on spaces and pass each word back to the same module.
"desufnoc ma i" should become "confused am i"
Thats one way that should work

2007-02-26 00:54:34 · answer #4 · answered by Neil 5 · 1 0

DO not use scanf() as you cant read the space . So input like
U are fixed
will be stored as U only.
use gets() to read the characters.

char u_input[80];
gets(u_input)

now check for first space and put that data into one more string.

you may use pointers

int i=0,j;
char *ptr=u_input;
char temp[80];
char splitdata[80][80];


while(*ptr!='\0')
{
j=0;
temp[j]='\0';
while(*ptr!=' ' && *ptr!='\0')
{
temp[j++]=*ptr++;
}
temp[j]='\0';
strcpy(splitdata[i++],temp);
}

while(i>0)
{
puts(splitdata[i--]);
}

- HOpe it works. i didnt tested,

2007-02-26 01:43:32 · answer #5 · answered by PC 4 · 0 0

all i can say is that your beautiful and fine and i think that i cant help you bye

2007-03-05 20:28:27 · answer #6 · answered by $tuti_chulo 1 · 0 0

fedest.com, questions and answers