Solved

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

Posted on 2004-04-16
10
229 Views
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.

Regards

0
Comment
Question by:saad302
  • 2
  • 2
  • 2
  • +2
10 Comments
 
LVL 17

Expert Comment

by:mokule
ID: 10846806

Have you written anything so far?
0
 
LVL 23

Accepted Solution

by:
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
length.

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

Author Comment

by:saad302
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...
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 9

Assisted Solution

by:ankuratvb
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.

0
 
LVL 2

Assisted Solution

by:Avik77
Avik77 earned 166 total points
ID: 10850149
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
 

Author Comment

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

Avik, Ankuratvb your tips were very helpfull.

Regards
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10852138
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
 
LVL 2

Expert Comment

by:Avik77
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 :)

Avik.
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

803 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