Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Reverse words in a sentense keeping space positions same (http://www.go4expert.com/forums/reverse-words-sentense-keeping-space-t22300/)

biswajit.dash 3Jun2010 16:46

Reverse words in a sentense keeping space positions same
 
Hi,

I have been try this for 2-3 hr.

what my intent is to hav program while will reverse the words in a sentence without changing the space positions.

ex:

input: char str[]="My name is Biswajit""
output : "Biswajit is name My"

I m successful in reversing the words without keeping the space positions same, i m not successful in other way.

Appreciate any thoughts.

Thx
Biswajit

xpi0t0s 4Jun2010 14:54

Re: Reverse words in a sentense keeping space positions same
 
Scan right to left checking for spaces and counting the length of the word you're currently looking at.
Then use strncpy to copy a fixed number of characters to the end of the output string.
Remember to add spaces too.

Another way to do it is to create a substring reverser that will reverse the specified characters within a string (for example, if you gave it "abcde", start position 1, number of characters 3, it would change the string to "adcbe"). Then reverse the whole string, and use the substring reverser to reverse each individual word.

CrazyGeek 6Jun2010 21:09

Re: Reverse words in a sentense keeping space positions same
 
I think It's simpler if you use the function gotoxy(x,y);
At first call gotoxy(x,y) to move text pointer to a position that the distance to home line is long enough to show your string.
Then scan through all characters of your string and print one by one, if the charater is " " ( means a space), call gotoxy(x,y) to the appropriate position.( the text pointer must be back everytime you catch a space). I know you can understand my idea. OK thanks!

xpi0t0s 7Jun2010 13:09

Re: Reverse words in a sentense keeping space positions same
 
Good idea, except that gotoxy moves the cursor on the screen and has absolutely bu99er all to do with string manipulation.


All times are GMT +5.5. The time now is 00:03.