GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Dec-2005, 15:58
Infuriate Infuriate is offline
New Member
 
Join Date: Dec 2005
Posts: 9
Infuriate is on a distinguished road
Unhappy

Need help with reversing strings


I'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  
Old 01-Dec-2005, 16:08
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about

Re: Need help with reversing strings


Quote:
Originally Posted by Infuriate
I'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

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  
Old 01-Dec-2005, 16:16
Infuriate Infuriate is offline
New Member
 
Join Date: Dec 2005
Posts: 9
Infuriate is on a distinguished road

Re: Need help with reversing strings


I'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  
Old 01-Dec-2005, 16:40
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about

Re: Need help with reversing strings


Quote:
Originally Posted by Infuriate
I'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

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  
Old 01-Dec-2005, 17:22
Blstretch Blstretch is offline
New Member
 
Join Date: Oct 2005
Posts: 21
Blstretch is on a distinguished road

Re: Need help with reversing strings


what 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  
Old 01-Dec-2005, 17:44
Infuriate Infuriate is offline
New Member
 
Join Date: Dec 2005
Posts: 9
Infuriate is on a distinguished road

Re: Need help with reversing strings


user input

how do I reverse the output of the array?
  #7  
Old 01-Dec-2005, 17:50
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: Need help with reversing strings


Do you use C or c++?

Quote:
Originally Posted by Infuriate
how do I reverse the output of the array?
As Kobi Already said,
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  
Old 01-Dec-2005, 18:27
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Need help with reversing strings


Quote:
Originally Posted by Infuriate
I'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?
You don't need either of these functions. You also didn't mention if you are writing C or C++. Giving us the details is important.

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  
Old 01-Dec-2005, 18:28
Infuriate Infuriate is offline
New Member
 
Join Date: Dec 2005
Posts: 9
Infuriate is on a distinguished road

Re: Need help with reversing strings


I am using C, sorry about that.
  #10  
Old 01-Dec-2005, 18:48
Infuriate Infuriate is offline
New Member
 
Join Date: Dec 2005
Posts: 9
Infuriate is on a distinguished road

Re: Need help with reversing strings


Still struggling on how to have it assign a word to the string(Yes, i'm a beginner)

CPP / C++ / C Code:
#include <stdio.h>
#include <conio.h>

int
main(void)

{
          char x[50];
          
          printf("=> ");
          scanf("%s", &x);
          
          printf("%s", x);
          
          getch();
          return(0);
}
Last edited by LuciWiz : 03-Dec-2005 at 08:21. Reason: Please insert your C code between [c] & [/c] tags
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 13:42.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.