c - C语言字长统计

标签 c string

谁能帮我做这个项目。

编写一个程序来计算句子的字长统计信息。该句子以“.”结尾 对于每个找到的单词长度,该长度的单词数 被打印。仅打印在输入中找到的那些长度。仅有信件来自 a-z 或 A-Z 可以组成单词。单词之间用空格分隔。无标点符号 允许使用“.”以外的字符。如果识别到任何其他输入字符或 输入的长度超过 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。如果我不喜欢比较已经比较过的元素,我可以重新编写我的代码吗?

第二个问题是我需要获得从最低长度到最高长度的结果。

最佳答案

#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;
}

关于c - C语言字长统计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26563886/

相关文章:

c - 如何创建一个指向另一个链表的链表?

c - 使用另一个数组索引一个数组 - gcc 6.3 运行时错误

c - 在C中使用scanf提取key=value

c - 我在数组中的一个值是 "0"

java - java中删除特殊字符的策略建议

c# - 为什么.Net中Exception类的ToString()方法不使用StringBuilder来构建字符串?

python - 如何将带有单词的字符串中的数字相加?

android - 在 Android 中验证手机号码

java - 一个32个字符的字符串如何变成46个字节?

c - 为不存在的函数声明函数原型(prototype)是否安全?