• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Once again... Need help with C programming

saahmed

Golden Member
Okay I have some of the program completed. Ive run into some trouble. I think I have some of the psuedocode in my head, but dont really know if it is the correct way to go about doing it and really just want some advice. What I have put in the function parseString is incorrect, and I dont even know if I need to get into those functions (isalpha etc.) but thats the only way I can think of. Any input would be apprectiated. I think what needs to be done by the program is explained well in the comments.
thanks.
EDIT: sorry, forgot to mention, so far in the parseString function I have only been trying to get the count of characters.

Also, the purpose of the program is to take an input string from the user. The users string ends when they enter a '^'. Then the program should count the number of characters, words, and lines in the string.

So far I have created the function that will take the users input and put it in a string. I have the main function completed as well. I just need help with the function that will add up the characters, lines, and words.

Problems I have noted so far:
1. when the while loop is put in the parseString function the program will run but it will not do anything after the users input.

2. The if statements are non-functional. The isalnum and isspace functions are supposed to return a true value if it is an alnum or space and 0 if not. When I run the program I get an error saying that the left operand must be a 1-value.

EDIT:Changed code a little.

EDIT: I think I thought of a much better way to do this. Use pointers to iterate through the string.
to count characters- count up elements until '^'.
for words- use two pointers, one will denote the end of a word and one will denote the beginning of a new word. And just add up the number of words.
for lines- count up to every '\n' just add those up. Is that possible?

Is this a good way to do it? Im gonna try and work on it but I think ill need a little help, not very good with pointers yet.
 
Maybe I'm not understanding the problem correctly, but shouldn't charCount be incremented with every loop? Why are you only counting alpha (alphanumeric?) and spaces?

If you run into a space, word++. If you run into a \n, line++. Always charCount++. Right?
 
Originally posted by: notfred
Thanks for letting us know what you're trying to do.


Sorry, are you being sarcastic? cant tell. I thought I described it well in the comments, but I guess I will add it to the post.
 
Originally posted by: igowerf
Maybe I'm not understanding the problem correctly, but shouldn't charCount be incremented with every loop? Why are you only counting alpha (alphanumeric?) and spaces?

If you run into a space, word++. If you run into a \n, line++. Always charCount++. Right?

So far I have just been trying to get a count of the characters in the string. So just adding up all the alphanumeric and spaces.
 
given a string you can iterate through it using an index or a pointer, first thing to do is realize using getchar() in parseString is not either.

strlen() will help if you use an index, ptr[0] != 0 will help for pointers.

> for lines- count up to every '\n' just add those up. Is that possible?

Seeing if the current character is "\n" while iterating is the right approach but consider:
"hello" = 1
"hello\n" = 1
"hello\nthere" = 2
"hello\nthere\n" = 2
...so your logic needs to be a little more complicated.
 
in getString you stop at ^ but don't store it.

in parseString you assume you stored the '^' instead of stopping when you reach the string terminator.
 
Im not sure how to do the word count. The assignment says that a word is a group of consecutive non-whitespace characters. The whitespace characters include '\t', '\n' etc. So I cant simply iterate through the string and stop at ' '. I know of the function isspace but the book doesnt really do a good job of helping me implement it. I dont know if I even have to use that function. Any ideas/ hints?

EDIT: actually I think I can just do the same while loop, but instead of !='\0', use !='\n'||'\t' etc. But how can I have it go to one whitespace character stop and then go the the next non-whitespace character and do this over and over, then add up the words?

I know i'll need two pointers. One to store location of the end of a word and then one at beginning of next word.
 
flags.

think of how a human counts words. you see some letters so a word has started, then you see some whitespace, so the word is done and the next time you see letters it's a new word, right?
 
Originally posted by: DaveSimmons
flags.

think of how a human counts words. you see some letters so a word has started, then you see some whitespace, so the word is done and the next time you see letters it's a new word, right?

Use pointers and then store the each spot that is a whitespace character? And then count in between those 'flags'?

One pointer that points to characters with whitespace before them, and one that points to characters with whitespace after?

EDIT:

would something like this maybe work?
if(string[count-1]==' '||'\n'||'\t'){
beginWord=count;
i++;
}
if(string[count+1]==' '||'\n'||'\t'){
endWord[t]=count;
t++;
}
 
Boolean / true-false flags.

int word_started = 0 ;

loop
{

if (some test)
{
word_started = 1 ;
word_count ++ ;
}

if (some other test )
word_started = 0 ; // this word is over now, next iteration start looking for a new one

} // end loop
 
I think I have an almost complete program. But it still isnt working properly. not getting any values for lines and words. Probably some stupid error.
Its in the OP.
 
Originally posted by: DaveSimmons
tell me, what is the value of count when you start your second while loop?


Thats what I started thinking just after I posted that last post. I fixed it. I always make some stupid mistake and after I post about it on here I tend to figure it out. Its weird. I guess it just helps me to talk it out on here.
 
Back
Top