繁体   English   中英

字长统计C语言

[英]word length statistic C language

谁能帮我做那个项目。

编写一个程序,计算一个句子的单词长度统计信息。 该句子以“。”开头。 对于找到的每个单词长度,将打印该长度的单词数。 仅打印在输入中找到的那些长度。 只允许来自z或AZ的字母组成单词。 单词之间用空格隔开。 除“。”外,没有标点符号。 被允许。 如果识别出任何其他输入字符或输入长度超过80个字符,程序将显示“ NOT VALID”(无效)(请参阅提示)。 请注意,在输入中没有单词的情况下,不会打印任何内容。

% 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
#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]);
}

因此,问题是,如果我输入“ abc abcd abc abc”之类的文本,结果将类似于“长度3:3长度4:1长度3:2”。 因此,当程序已经将第一个(此处为3个)元素与其他元素进行了比较时,该元素将不再进行比较,但是我具有相同长度的3d元素,在这种情况下,程序会将其与其余元素进行比较并打印2。如果我不比较已比较的元素,我可以重做我的代码吗?

第二个问题是我需要从最小长度到最大长度得到结果。

一种简单的方法是使用strtok( http://www.tutorialspoint.com/c_standard_library/c_function_strtok.htm )。

#include <stdio.h>

int numwords (char *str);

int main() {
    char word[] = "This is a test - does it pass?";
    int c = numwords(word);
    printf("Words = %d\n", c);
    return 0;
 }

int numwords(char *str)
{
    int n = 0;
    for(str=strtok(str, " -.!,;"); str; str=strtok(NULL, " -.!,;"))
        n++;
    return n;
}
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

void invalid(void){
    puts("NOT VALID");
    exit(EXIT_FAILURE);
}

int main(void){
    int length[80]={0};
    int i, ch, word_len=0;

    printf("Enter a sentence: ");
    while(1){
        ch=getchar();
        if(isalpha(ch)){
            ++word_len;
        } else if(ch == ' ' || ch == '.'){
            if(word_len>80)
                invalid();
            if(word_len)
                length[word_len-1]++;//-1: to 0 origin
            if(ch == '.')
                break;
            word_len = 0;
        } else {
            invalid();
        }
    }
    for(i=0;i<sizeof(length)/sizeof(*length);++i){
        if(length[i])
            printf("Length %d: %d\n", i+1, length[i]);
    }
    return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM