Question

Exercise 5.10. Write a program that reads strings from stdin and computes the integer mean of their lengths.code help. mine's logic is not working

in C language THANKS!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
#include <string.h>
int main()
{
char s[100];
int eachStringLength = 0,i;
int totalLength = 0;
int count = 0;
double mean = 0;
printf("Enter the string(bye to quit): ");
scanf("%s", &s);
while (strcmp(s, "bye") != 0){
eachStringLength = 0;
for(i=0; s[i]!='\0' ; i++){
eachStringLength++;
}
totalLength = totalLength + eachStringLength;
count++;
printf("Enter the string(bye to quit): ");
scanf("%s", &s);
}
mean = totalLength/(double)count;
printf("Mean of all strings length: %lf\n", mean);

return 0;
}

Output:

sh-4.2$  gcc -o main *.c                                                                                                                                                                                                                                               

sh-4.2$ main                                                                                                                                                                                                                                                           

Enter the string(bye to quit): suresh                                                                                                                                                                                                                                  

Enter the string(bye to quit): sekhar                                                                                                                                                                                                                                  

Enter the string(bye to quit): anshu                                                                                                                                                                                                                                   

Enter the string(bye to quit): revathi                                                                                                                                                                                                                                 

Enter the string(bye to quit): hi                                                                                                                                                                                                                                      

Enter the string(bye to quit): bye                                                                                                                                                                                                                                     

Mean of all strings length: 5.200000

Add a comment
Know the answer?
Add Answer to:
code help. mine's logic is not working in C language THANKS! Write a program that reads...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT