gpt4 book ai didi

c - K&R 练习 1-14 解决方案

转载 作者:太空宇宙 更新时间:2023-11-04 04:00:28 25 4
gpt4 key购买 nike

<分区>

我正在做这个练习,想知道我的答案是否正确;

Write a program to print a histogram of the frequencies of different characters in the input.

我在网上看到了其他几个答案,但它们与我的答案相差很大。另外,如果我的代码格式有任何问题或需要进行任何改进。欢迎提出任何建议。我确实理解这个问题需要一个直方图,但是一旦我有了所需的数据就很容易构建一个。

#include <stdio.h>
int main(){

int userInput;
int arrayStuff[92];
int i, j;

for(i = 0; i < 92; ++i){
arrayStuff[i] = 0;
}

while((userInput = getchar()) != '\n'){
if(userInput >= 30 && userInput <= 122){
if(userInput != '\n'){
++arrayStuff[(userInput-30)];
}

if(userInput == '\n'){
break;
}
}
}

printf("Case\t|\tOccurances\n");

for(i = 0; i < 92; ++i){
printf("%c\t|\t%d\n", (i+30), arrayStuff[i]);
}
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com