Go Back   Computer Forums > General Computing > Programming
Join Computer forums Today

Thread Tools Search this Thread Display Modes
Old 10-07-2006, 12:12 PM   #1
Beta Member
Join Date: Oct 2006
Posts: 1
Default help!...someone pls...a newbie here.

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."

the output-

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

The text has 6 words, those are:

OMG ...how is that? all i know is "hello world"
i know i sound so stupid but please help!
and thanks in advance!

cp_newbie is offline   Reply With Quote
Old 10-07-2006, 01:13 PM   #2
Baseband Member
Griz803's Avatar
Join Date: Jun 2004
Posts: 89
Default Re: help!...someone pls...a newbie here.

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.


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.

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.
/*What does not kill us, makes us drink far too much caffeine*/
Griz803 is offline   Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting 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 Off

All times are GMT -5. The time now is 03:45 PM.

Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2016, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0