 |
 |
C Help
|
 |
|
 |
|
Junior Member
Join Date: Sep 2002
Location: Evanston, Illinois
Status:
Offline
|
|
Hey everyone,
I have a question.
I want to use only C (no C++)
to have the user enter a string of text and then separate every word in the test into different strings.
How would I do this?
Like
User enters "I would like some help" as one string
then
string - I
string - would
string - like
and so on
I think this would require placing a null at each space and then using pointers to go past to the next string.
Basically i want to be able to re-arrange the input string words into a different string.
In other words, make it output
"Would I like help some" or "help some would like I"
|
|
That's just my opinion. What do I know, I'm just a college student.
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Nov 2003
Location: Minnesota
Status:
Offline
|
|
Hmmm, you could replace the spaces with '\0's and use some clever pointer aritmetic, but that would be pretty ugly.
Why not just do the following?
1) Count the number of words in the string (using the spaces).
2) Create a char* array that has as many entries as words.
3) Loop through the original string and copy each word, one at a time to the array.
It's much easier to write, follow, debug and re-use.
|
|
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Sep 2002
Location: Evanston, Illinois
Status:
Offline
|
|
Thank Turias
however, the input is going to vary in length and in number of words, so i need to figure out the fun point math.
I am very new to C.
So i might just try what you said and see if will work for it.
Thanks.
(Last edited by ZeroKoolNess1; Nov 19, 2003 at 02:01 PM.
)
|
|
That's just my opinion. What do I know, I'm just a college student.
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Nov 2003
Location: Minnesota
Status:
Offline
|
|
Originally posted by ZeroKoolNess1:
Thank Turias
however, the input is going to vary in length and in number of words, so i need to figure out the fun point math.
A varying input length with a varying number of words shouldn't matter.
As far as input is concerned, you have two options:
1) Put the input into a big-ass string (thus, there will be a maximum length).
2) Read the input in one character at a time.
Do you have any other concerns? I'm confused as to what other problems you forsee.
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Feb 2001
Location: Deer Crossing, CT
Status:
Offline
|
|
Your best bet would be to use the strtok() (string tokenizer) function. You could send your input through a while loop and add each token to an array or linked list or whatever.
Try doing a "man strtok" to get the lowdown.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Nov 2003
Location: Minnesota
Status:
Offline
|
|
Heheh. I like how the man page says:
BUGS
Never use these functions.
It then lists a number of reasons why you shouldn't.
Plus, since I assume that ZeroKool is an intro student, I would argue that it would be more useful to stay away from functions like these and instead learn how to do it manually.
|
|
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Sep 2002
Location: Evanston, Illinois
Status:
Offline
|
|
Originally posted by Turias:
A varying input length with a varying number of words shouldn't matter.
As far as input is concerned, you have two options:
1) Put the input into a big-ass string (thus, there will be a maximum length).
2) Read the input in one character at a time.
Do you have any other concerns? I'm confused as to what other problems you forsee.
Sorry about thinking there would be problems.
Your ideas work (but i bet you already knew that) and it got it to work.
Thanks you very much, i knew i would find good help here.
Again, thank you
|
|
That's just my opinion. What do I know, I'm just a college student.
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Nov 2003
Location: Minnesota
Status:
Offline
|
|
|
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|