![]() |
|
#1
|
|||
|
|||
Need help with reversing stringsI'm really kinda lost on how to make this program. It's one of my last problems,
I need a program that would take data a line at a time and reverse the words of the line. An example of the input and output would be... apples and oranges --> oranges and apples Any help would be much appreciated. Thanks |
|||
|
#2
|
||||
|
||||
Re: Need help with reversing stringsQuote:
Hey Dude, and welcome to GID What did you have in mind ? I can think of some solutions : First solution : 1. read each line with getline. 2. read the words into a doubly linke-list with head and tail, using strtok() 3. printing the lists from tail to head. Second solution : 1. read each line with getline ... 2. read the next token with strtok and call your function recursively for the rest of the line. 3. when you get to the end of the tokens, you finish the recursion and print the words. Third solution : 1. read each line with getline ... ... 2. read the words, using strtok() (or, you can have a pointer to char that you increment while generating the tokens on your own) into an array. Keeping count of the words in each line. 3. printing the array in the desired way. What were you thinking of doing ? Kobi. __________________
It's actually a one time thing (it just happens alot). |
|
#3
|
|||
|
|||
Re: Need help with reversing stringsI'm really kinda new to programming all together and haven't used getline or strtok before. Is there a good reference site where I can learn about these?
Thanks |
|
#4
|
||||
|
||||
Re: Need help with reversing stringsQuote:
try fgets instead of getline : fgets reference about strtok : strtok reference Best regards, Kobi. __________________
It's actually a one time thing (it just happens alot). |
|
#5
|
|||
|
|||
Re: Need help with reversing stringswhat about using a string array to store the letters and spaces, then just reversing the output of the array??
oh and by the way, are wetalking about taking data from a file, or from user input? |
|
#6
|
|||
|
|||
Re: Need help with reversing stringsuser input
how do I reverse the output of the array? |
|
#7
|
||||
|
||||
Re: Need help with reversing stringsDo you use C or c++?
Quote:
1. Read the string from the user, using getline or fgets. 2. Use strtok to split the string. 3. Store the splitted string into a string array, to be more simple. 4. Then add the strings in the string array, from the end, to a new string, which is the reverse of the original string. Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
|
#8
|
||||
|
||||
Re: Need help with reversing stringsQuote:
Set up a character (C) or string (C++) array to hold each word read. cin and scanf will read one word at a time from the keyboard. This is probably the ONLY place I see scanf as useful Read a word, put it in the array. When done reading, output the array from the end. __________________
During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence? |
|
#9
|
|||
|
|||
Re: Need help with reversing stringsI am using C, sorry about that.
|
|
#10
|
|||
|
|||
Re: Need help with reversing stringsStill struggling on how to have it assign a word to the string(Yes, i'm a beginner)
CPP / C++ / C Code:
Last edited by LuciWiz : 03-Dec-2005 at 08:21.
Reason: Please insert your C code between [c] & [/c] tags
|
Recent GIDBlog
Programming ebook direct download available by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Strings tripping me up once more | Elsydeon | C++ Forum | 5 | 04-Dec-2005 18:41 |
| need help - questions about strings | Benayoun | C Programming Language | 6 | 24-Jan-2005 03:15 |
| array of pointers to strings | mirizar | C++ Forum | 5 | 21-Jan-2005 11:24 |
| C++ style strings and STL | dexter | C++ Forum | 14 | 04-Jan-2005 08:46 |
| I am reviewing Arrays and need help converting some strings to arrays | jenmaz | C Programming Language | 22 | 23-Nov-2004 00:26 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The