gpt4 book ai didi

计算字符的出现次数并将其打印出来

转载 作者:行者123 更新时间:2023-11-30 16:55:25 25 4
gpt4 key购买 nike

我已经设法从我的输入中获取字母表的频率。我还找到了最大的出现字符。但我不知道如何打印实际字符。现在我的程序显示 A-Z 并显示每个字母表的出现次数。我希望能够在下一行打印出最大的出现的字母以及它出现的次数。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX 200

int readLine(char string[]);
void find_frequency(char string[], int count[]);
int maxArray(int a[]);

int main(void)
{
char array[MAX];
int freq[MAX];
int nrOfChar;
int i;
char c;
int max;

printf("Command Line Tool\n");
printf("Please enter text here: ");
nrOfChar = readLine(array);

for(c = 'A'; c<= 'Z'; c++)
{
printf("%c ", c);
}


find_frequency(array, freq);

printf("\n");
for(i=0;i<26;i++)
{
printf("%d ", freq[i]);
}
printf("\n");

max=maxArray(freq);

printf("Print letter and how many occurrence.\n");
printf("Finished excuting.\n");

return 0;
}

int readLine(char string[])
{
int ch;
int i=0;
while (isspace(ch = getchar()))
;
while (ch != '\n' && ch != EOF)
{
if (i < MAX)
{
string[i++] = ch;
ch = getchar();
}
}
string[i] = '\0';
return i;
}

void find_frequency(char string[], int count[])
{
int i;

for(i = 0; string[i] != '\0'; i++)
{
if (string[i] >= 'A' && string[i] <= 'Z' )
{
count[string[i]-'A']++;
}
}
}

int maxArray(int a[])
{
int i, max=0;

for (i=0; i<26; i++)
{
if (a[i]>max)
{
max=a[i];
}
}


return max;
}

最佳答案

下面我结合了评论中的各种建议以及其他一些修复和样式更改供您考虑:

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define ALPHABET_LENGTH 26
#define MAXIMUM_CHARACTERS 200

int readLine(char string[], int maximum);
void findFrequency(char string[], int count[]);
int findMaximumIndex(int array[], int length);

int main(void)
{
char characters[MAXIMUM_CHARACTERS];
int frequencies[ALPHABET_LENGTH];

printf("Command Line Tool\n");
printf("Please enter text here: ");
(void) readLine(characters, MAXIMUM_CHARACTERS);

for (char c = 'A'; c <= 'Z'; c++)
{
printf("%c ", c);
}
printf("\n");

findFrequency(characters, frequencies);

for (int i = 0; i < ALPHABET_LENGTH; i++)
{
printf("%d ", frequencies[i]);
}
printf("\n");

int maximum = findMaximumIndex(frequencies, ALPHABET_LENGTH);

printf("%c occurred most often (%d times)\n", maximum + 'A', frequencies[maximum]);
printf("Finished excuting.\n");

return 0;
}

int readLine(char string[], int maximum)
{
int c, count = 0;

while (isspace(c = getchar()))
;

while (c != EOF && c != '\n')
{
if (count < maximum - 1)
{
string[count++] = c;
c = getchar();
}
}

string[count] = '\0';

return count;
}

void findFrequency(char string[], int count[])
{
for (int i = 0; string[i] != '\0'; i++)
{
char c = string[i];

if (c >= 'A' && c <= 'Z' )
{
count[c - 'A']++;
}
}
}

int findMaximumIndex(int array[], int length)
{
int index = 0;

for (int i = 1; i < length; i++)
{
if (array[i] > array[index])
{
index = i;
}
}

return index;
}

示例输出

> ./a.out
Command Line Tool
Please enter text here: IN TEACHING OTHERS WE TEACH OURSELVES
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
2 0 2 0 6 0 1 3 2 0 0 1 0 2 2 0 0 2 3 3 1 1 1 0 0 0
'E' occurred most often (6 times)
Finished excuting.
>

还有一些事情需要考虑:如果任何字符出现超过 9 次,你的输出看起来就不太好了——你该如何解决这个问题;您可能希望将混合大小写作为 readLine() 例程中的一个选项来处理,以将输入大写;最常出现的字母可能有(多向)并列,你能处理这个问题吗?你的 readLine() 返回一个你没有做任何事情的字符计数——它有什么用处(可能在错误检查中)?

关于计算字符的出现次数并将其打印出来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40313134/

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