Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

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.

Regards

0
saad302
Asked:
saad302
  • 2
  • 2
  • 2
  • +2
3 Solutions
 
mokuleCommented:

Have you written anything so far?
0
 
brettmjohnsonCommented:
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
length.

That should be enough to get you pointed in right direction.
0
 
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...
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
ankuratvbCommented:
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.

0
 
Avik DasguptaCommented:
Hi,
  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 .
sum=0;
for(i=0;i<max;i++){/* max obtained from the prev navigation of the string when the words were inserted */
 sum=+w[i].count;
}
avg=sum/(max+1);
maximum lies only to determine the maximum count value and print the corresponding index field.

Hope this helps.

Avik.

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

Avik, Ankuratvb your tips were very helpfull.

Regards
0
 
ankuratvbCommented:
Hi,

>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.
0
 
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 :)

Avik.
0
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.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now