Tuesday, July 14, 2009

C programming problem?

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...?

C programming problem?
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.
Reply: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
Reply: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
Reply:all i can say is that your beautiful and fine and i think that i cant help you bye
Reply: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.
Reply: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!=' ' %26amp;%26amp; *ptr!='\0')


{


temp[j++]=*ptr++;


}


temp[j]='\0';


strcpy(splitdata[i++],temp);


}





while(i%26gt;0)


{


puts(splitdata[i--]);


}





- HOpe it works. i didnt tested,


No comments:

Post a Comment