help!...someone pls...a newbie here.

cp_newbie

Beta member
Messages
1
i do not know much bout C prog. so i need help...my teacher gave us this problem:

"Write a prog. that'll read a line of text (sentence), and count the number of words in it, then displaying the words."

Example:
the output-

Enter a sentence:
Roses are red, violets are blue.

The text has 6 words, those are:
Roses
are
red
violets
are
blue

OMG :eek: ...how is that? all i know is "hello world"
i know i sound so stupid but please help!
and thanks in advance! :)
 
Okay, calm down a bit. First, I won't write the code for you. Second, I *WILL* try and get you started. You said C language, right? Okay, one of the most difficult things for new C and C++ programmers is to figure out what sort of library functions (ANSI, preferrably) will do certain things that apply to their program's functionality. The URL below will help out here quite a bit.

http://www.cppreference.com/

I know it *SAYS* C++, but it includes all the C stuff too.

Next big hangup is knowing what the logic of the thing would be. Here is a bit of a boost in that direction. When you don't know what the program should do, you slow down and break down the steps you'd naturally use to do the task yourself. The only thing here is that you have to remember that a computer is a machine and machines are stupid until a smart human "explains" it all to them in a program. So, gear the steps to be small and easily "understood" by the average, oh, say wild metamorphic rock:-D

I'll try and show how by working through the logic of your problem to a certain point. Let's start with reading the line of text. Okay, I can read English, computers don't though. So the concept of "word" to them is simply not a defined thing they recognize. They do comparisons though, of characters. And the English language (as most natural languages) uses a space between each word as a delimiter (if you've never heard that word, look it up). Maybe we can read a whole line of text into a group of characters called an array. Once we have a copy of the line, we can search for the number of spaces to find the groups of characters before the end of the line. We can also break them apart this way. Hey, problem almost solved. Psuedocode ( if you don't know what that is look it up) for this appears below.

Code:
creat Array, TempArray
Init Array, TempArray
Array = Line input  (Set an array equal to the one you get from the keyboard)
For each element of Array
                    if Element = letter then
                                     {
                                       copy letter to TempArray
                                     }
                    if Element = space then 
                                     {
                                       increment space counter
                                       print TempArray
                                       Init TempArray
                                     }
Next Element

When you figure out how to do this, you should be able to get the rest easily enough I think. Good luck and I hope this helps you out.
 
Back
Top Bottom