Does anybody know much about C Programming?
#1
Senior Member
Posts like a Turbo
Thread Starter
Join Date: Apr 2003
Location: Arkansas
Posts: 409
Likes: 0
Received 0 Likes
on
0 Posts
![parallelcircuits is on a distinguished road](https://www.gmforum.com/images/reputation/reputation_pos.gif)
![Default](https://www.gmforum.com/images/icons/icon1.gif)
Is there any programmers here? If so maybe I can get some help.
I am trying to read a unknown length of text separated by white space. Separate the words and place them in a binary tree. I can get it to work if I set up a exit string but the teacher will not accept it that way.
I am trying to use scanf("%*", input); where input is a char input[50];
It will read and separate the text but will not exit because it scanf still wants a string.
Does this make any sence to anybody?
Any help would be great.
Here is the main part of the program
Here is what I am trying to do.
http://csce.uark.edu/~bss03/ceng1121l/bintree/
I am trying to read a unknown length of text separated by white space. Separate the words and place them in a binary tree. I can get it to work if I set up a exit string but the teacher will not accept it that way.
I am trying to use scanf("%*", input); where input is a char input[50];
It will read and separate the text but will not exit because it scanf still wants a string.
Does this make any sence to anybody?
Any help would be great.
Here is the main part of the program
Code:
while(1) { curr = (node *)malloc(sizeof(node)); curr->left = curr->right = NULL; scanf("%*", input); printf("%* ", input); if(isspace((int)input[0]) && isspace((int)input[1]) == 0) { break; } strcpy ( curr->word, input); insert(&output, curr); }
http://csce.uark.edu/~bss03/ceng1121l/bintree/
#3
Senior Member
Certified GM nut
![Default](https://www.gmforum.com/images/icons/icon1.gif)
sorry.....not proficient in C. when i had a problem with a different language i tried here http://www.tek-tips.com/, but it doesn't seem like they have a forum for C.
look around!
good luck
look around!
good luck
#4
Senior Member
Posts like a Supercharger
Join Date: Mar 2004
Location: Columbia, MO
Posts: 175
Likes: 0
Received 0 Likes
on
0 Posts
![enmityst is on a distinguished road](https://www.gmforum.com/images/reputation/reputation_pos.gif)
![Default](https://www.gmforum.com/images/icons/icon1.gif)
Originally Posted by parallelcircuits
Is there any programmers here? If so maybe I can get some help.
I am trying to read a unknown length of text separated by white space. Separate the words and place them in a binary tree. I can get it to work if I set up a exit string but the teacher will not accept it that way.
I am trying to use scanf("%*", input); where input is a char input[50];
It will read and separate the text but will not exit because it scanf still wants a string.
I am trying to read a unknown length of text separated by white space. Separate the words and place them in a binary tree. I can get it to work if I set up a exit string but the teacher will not accept it that way.
I am trying to use scanf("%*", input); where input is a char input[50];
It will read and separate the text but will not exit because it scanf still wants a string.
![Smile](https://www.gmforum.com/gm/images/smilies/smile.gif)
I'm a little confused as to why you're using scanf() here -- scanf() reads data from stdin (keyboard), parses it according to your delimiters, and stores it in the variable specified -- the way you have it set up, the program will enter the while loop, initialize a node, and then wait for keyboard input. If that'* what it'* supposed to do, sorry, I just can't really tell without seeing the rest of the program
![Smile](https://www.gmforum.com/gm/images/smilies/smile.gif)
Looking over the assignment, I was under the impression that the input was just going to be *one* string, words separated by whitespace, and a null terminator -- i.e.,
"now is the time for all good men", etc, and that you would have to parse that one large string into many small strings ("now", "is", etc), then store those small strings in the appropriate place (node->word), rather than using the keyboard to enter them one at a time.
If that is indeed the case, strtok() is the function you want to use: (excuse the quick 'n dirty programming -- I haven't written any in C in almost 5 years)
Code:
char string[]="this is an example string"; char *token; // may want to do a malloc() here to be safe token = strtok(string, " ") // this will pull the first token out of the string ("this"). the " " is your list of delimiters, which in your case is just a whitespace. so token will point to the first group of non-whitespace characters in the string -- i.e., the first word. while (token != NULL) { // strtok returns a null pointer when it hits the end of the string node->word = *token; // or however your pointers and everything are set up token = strtok(NULL, " "); // strange thing about strtok -- it remembers what string it'* looking at and where it is. so after the first call, all you have to pass is a null pointer and your delimiter string, and it will remember where it'* at and return the pointer to the next token. }
![Smile](https://www.gmforum.com/gm/images/smilies/smile.gif)
-b
#5
Senior Member
Posts like a Supercharger
Join Date: Mar 2004
Location: Columbia, MO
Posts: 175
Likes: 0
Received 0 Likes
on
0 Posts
![enmityst is on a distinguished road](https://www.gmforum.com/images/reputation/reputation_pos.gif)
![Default](https://www.gmforum.com/images/icons/icon1.gif)
Oh, one thing I forgot to mention -- if there are any other functions that do anything to that string, you may want to copy the string to a buffer for strtok. strtok *will* modify the string passed to it, so if you have other functions depending on it, it may jack things up a bit.
For this same reason it'* important that the string that gets passed to strtok isn't initialized as const.
-b
For this same reason it'* important that the string that gets passed to strtok isn't initialized as const.
-b
Thread
Thread Starter
Forum
Replies
Last Post
jachin
Detailing & Appearance
5
12-31-2003 01:26 PM