Solved

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

Posted on 2004-04-16
10
222 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
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…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now