Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > C Help

C Help
Thread Tools
Junior Member
Join Date: Sep 2002
Location: Evanston, Illinois
Status: Offline
Reply With Quote
Nov 19, 2003, 01:04 PM
 
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
Reply With Quote
Nov 19, 2003, 01:14 PM
 
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
Reply With Quote
Nov 19, 2003, 01:55 PM
 
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
Reply With Quote
Nov 19, 2003, 02:01 PM
 
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
Reply With Quote
Nov 19, 2003, 02:17 PM
 
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
Reply With Quote
Nov 19, 2003, 02:23 PM
 
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
Reply With Quote
Nov 19, 2003, 04:15 PM
 
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
Reply With Quote
Nov 19, 2003, 04:26 PM
 
Glad I could help!
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 02:26 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2