Ask your own question, for FREE!
Mathematics 6 Online
OpenStudy (anonymous):

Can someone here help me with a simple C programming question?

OpenStudy (anonymous):

It has to count the number of words that contain the letter e in it.

OpenStudy (anonymous):

I've written most of the code.

OpenStudy (anonymous):

#include <stdio.h> #include <string.h> int main () { char str[] ="To understand just what it means for one...\ string to be “greater than” or “less than” \ another string, consider the process ofalphabetizing \ a series of last names. The reader would, no \ doubt, place “Jones” before “Smith,” because the first\ letter of “Jones” comes before the first letter of “Smith” \ in the alphabet. But the alphabet is more than just a list of 26 \ letters—it is an ordered list of characters. Each letter occurs \ in a specific position within the list. “Z” is more than merely\ a letter of the alphabet; “Z” is specifically the 26th letter of \ the alphabet. How does the computer know that one particular letter \ comes before another? All characters are represented inside the computer\ as numeric codes; when the computer compares two strings, it \ actually compares the numeric codes of the characters in the \ strings. "; char * pch; printf ("Splitting string \"%s\" into tokens:\n",str); pch = strtok (str," ,.-"); while (pch != NULL) { printf ("%s\n",pch); pch = strtok (NULL, " ,.-"); } return 0; }

OpenStudy (anonymous):

Won't those quotation marks in line 2 (as well as many more later) pose a problem?

OpenStudy (anonymous):

Which ones are you referring to?

OpenStudy (anonymous):

Around "greater than," "less than," "Jones," etc. The compiler will hit the first " and end the string.

OpenStudy (anonymous):

It shouldn't, but that's a good point. I'll just remove them.

OpenStudy (anonymous):

But would you know how to write a code that will count how many words will have the letter e in it?

OpenStudy (anonymous):

I don't know. I never used C, just Perl, so I could be wrong. I figured you'd need to encode them.

OpenStudy (anonymous):

I would break the string into an array, using space as a separator. Then, check each instance of the array to see if it contains an e. Sum those up and you're done!

OpenStudy (anonymous):

Im not so good at C, since I just started. What do you mean?

OpenStudy (anonymous):

Well, then we're on the same page. ;) You need to do your basics like initiating your variables (not sure if that's what it's called, but it looks like you've done it), then break your string (which you called str[]) into an array containing each word as an instance in that array. For there, cycle through the array (for, while ... however you want) and check each one to see if it contains an "e." I have no idea how to write the code, and that's just one way of doing it.

OpenStudy (anonymous):

The code you typed—does it run?

OpenStudy (anonymous):

Computer programming is about more than the code. That's the easy part. It's about analyzing a problem and determining a solution, of which there could be many. I often found it helpful to draft out my solution in words, then apply the code that does those actions. The way I solve this problem might be different than you would, or anyone else here for that matter. Best of luck!

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!