计算字符的出现并打印出来

时间:2016-10-28 20:59:12

标签: c arrays loops

我已经设法从输入中获得字母表的频率。我也发现了最大的发生角色。但我不知道如何打印实际角色。现在我的程序显示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;
}

1 个答案:

答案 0 :(得分:0)

下面我从评论中结合了各种人的建议,以及其他一些修改和风格变化供您考虑:

#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()会返回你没有做任何事情的字符数 - 它可能有用(可能在错误检查中)?