Hi guys. Can anyone help me to make that project.
Write a program that calculates statistics on word length for a sentence. The sentence isterminated by a ’.’ For each found length of a word the number of words of that length
is printed. Only those lengths that are found in the input are printed. Only letters from
a-z or A-Z are allowed to form words. Words are separated by a space. No punctuation
characters other than ’.’ are allowed. If any other input character is recognized or the
input is longer than 80 characters the program displays ”NOT VALID” (see Hints). Note,
that in the case that no word is present in the input, nothing is printed.
% u6_stats
Enter a sentence: Bolt was expected to use the super bark.
Length 2: 1
Length 3: 3
Length 4: 2
Length 5: 1
Length 8: 1
% u6_stats
Enter a sentence: Something wasn’t right.
NOT VALID
Thats what i've already done.
Code:
#include <stdio.h>#include <conio.h>
void main() {
char s[100];
int numOfWords, lengthOfWord = 0;
int i = 0, j = 0;
printf("Enter the text : ");
fgets(s, sizeof(s), stdin);
numOfWords = 1;
while(s[i]!='\0'){
if(s[i] == ' '){
numOfWords++;
}
i++;
}
//printf("%d", numOfWords);
i = 0;
int help[numOfWords];
int l;
for(l = 0; l < numOfWords ; l++) help[l] = 0;
while(s[i]!='\0') {
if(s[i] != ' ' && s[i] !='\n' && s[i]!='.' ){
lengthOfWord++;
i++;
}else{
help[j] = lengthOfWord;
j++;
lengthOfWord = 0;
i++;
}
}
int repeat[80];
for(l = 0; l < 80 ; l++) repeat[l] = 1;
int num = 1;
i=0,l=0;
for(i = 0;i<numOfWords;i++){
for(l=i+1;l<numOfWords;l++){
if(help[i]==help[l]){
repeat[l]='\0';
num++;
repeat[i] = num;
}
}
num = 1;
}
l=0;
for (l=0; l<numOfWords; l++)
if (repeat[l]!='\0' && help[--l]!=help[++l]){
printf("Length %d: %d\n", help[l],repeat[l]);
}
}
So, the problem is that if i inpute a text like"abc abcd abc abc"
the result will be like "length 3: 3 length 4: 1 length 3: 2".
So when the program already compared 1st (here 3) element with otheres, this element will not be comparing again, but i have the 3d element with same length, and it that case, the program compare it with remained elements and print 2. How can i rework my code, if i dont wont to compare already compared elements.
Second problem is that i need to get results from lowest length till highst.
Please help me someone, and sry for my english.