• C

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

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.


Who is Participating?
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.

Have you written anything so far?
saad302Author Commented:
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...
Live Q & A: Securing Your Wi-Fi for Summer Travel

Traveling this summer? Join us on June 18, 2018 for a live stream to learn about the importance of Wi-Fi security and 3 easy measures you can start taking immediately to protect your private data while using public Wi-Fi. Follow us today to learn more!

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.

Avik DasguptaCommented:
  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.


saad302Author Commented:
Thank you Guys for your help...

Avik, Ankuratvb your tips were very helpfull.


>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.
Avik DasguptaCommented:
>>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 :)

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.