I need "a beginer level" c code to solve the following question? - urgent

Posted on 2004-04-16
Last Modified: 2010-04-15
Please help me to write "a beginer level" c code to solve the following question?

"Write a program that reads a line of text. The program should find the lenght of each word in the string and stores it in an array. The program should print the resulting array, and the index of the word that has the largest lenght, the program should also find and print the average word lenght?"

Please treat this question "urgent". by this Saturday (16 GMT) i need the answer?

Your help is highly appreciated.


Question by:saad302
  • 2
  • 2
  • 2
  • +2
LVL 17

Expert Comment

ID: 10846806

Have you written anything so far?
LVL 23

Accepted Solution

brettmjohnson earned 168 total points
ID: 10847462
Although we can't do your homework for you, I will provide you a few pointers:

Leverage the standard C library routines as much as possible:

fgets() can be used to read a line of text from standard input or a file.
strtok() can be used to break up the line of text into individual words.
strlen() can be used to determine the length of each word
realloc() can be used to allocate a resizable array of integer lengths (you don't know the number of words in advance)

You simply need to design a program that can leverage these tools to
to assemble the required word length statistics.  Your program should
leverage while() and for() loop constructs to iterate over each line of
input, iterate over each word in the line, display each value in the array,
scan the array for largest value, and sum the values to calculate the average

That should be enough to get you pointed in right direction.

Author Comment

ID: 10849753
Thank you brettmjohnson for your tips. it's not a homework as it's a tetorial to give me an idea how to deal with string, other questions i will have to do it my self (preparation for forther questions)?

Mokule, if you still can give help today, it will be appreciated...
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.


Assisted Solution

ankuratvb earned 166 total points
ID: 10849857
Here are a few pointers,

Store your line of text in a char array(for now you could use a statically allocated large array)

To count the no. of words which is the length of the integer array in which you will store the length of each word,

count the no. of single spaces that occur b/w characters.
i.e if there is a space,increment the no. of words by 1.Then,after this space,ignore all spaces till a non-space character occurs.
Then,check again for any other spaces separating words.

Then,using the no. of words,dynamically allocate your integer array.

Then,do a second pass over the string,processing in a similar manner as before,this time storing the number of non-space characters b/w consecutive spaces and storing in corresponding locations.

Then,the arithmetic is trivial.


Assisted Solution

Avik77 earned 166 total points
ID: 10850149
  I think tackling the problem will be easier if u create an array of structure, like:
struct word{
 char str[20]; // this will exceed normal size of words
 int count;
 int index;
struct word w[70]; // say max number of words

Now as referred by others, watch for delimeter chars like space, tabs, commas etc. Each word will begin after a delimeter is encountered and it also count the number of words as soon as it encounters a word. Accordingly provide the index field value i.e the string position where the word starts. Upon the encounter of another delimeter the word will be stored in the corresponding array's str field along with the counter value in the count field. Increment the array index and reset the counter till the '\0' character is encountered. Make sure u enter the last word and the first word properly. The first word may have no beginning delimeter and the last word may have no ending delimeter. Moreover u must also append a '\0' char to the word before storing it to the array. I hope there is no need to explain u about the average count now. Navigate the array and sum up the count values and average it .
for(i=0;i<max;i++){/* max obtained from the prev navigation of the string when the words were inserted */
maximum lies only to determine the maximum count value and print the corresponding index field.

Hope this helps.



Author Comment

ID: 10850798
Thank you Guys for your help...

Avik, Ankuratvb your tips were very helpfull.


Expert Comment

ID: 10852138

>I think tackling the problem will be easier if u create an array of structure, like:
>struct word{
>char str[20]; // this will exceed normal size of words
>int count;
>int index;
>struct word w[70]; // say max number of words

Do you need to store the words seperately in char arrays as well.
coz it doesnt say so in your original problem defn.
It says you just want the length of each word stored in an array.

In that case,the struct thing would waste memory.

Expert Comment

ID: 10853977
>>In that case,the struct thing would waste memory.

Well, I agree. But certainly it is a good approach to make the problem more general and flexible. The structure says everything. It keeps track of the words, their lengths and also their indexes. The problem I have tackled would lead to  only a single scan of the string storing all necessary information about the string in the proper fields of the structure.
It's all in a nutshell :)


Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question