(已编辑添加此位)我正在编写一个程序,其中读取一串字符,然后计算字符串中每个字母的频率并输出每个字母的次数或非信件。
我正在使用tolower(),但这造成了一个问题。
我已经改变了所以我现在使用循环来降低案例。不幸的是,程序仍在崩溃,我无法发现问题。
我已经完成了每个功能,以确保我没有任何松动。我已经删除了新的线元素但是当我运行它时,我输入了一些简单的“hello”并且它崩溃了。
#include <stdio.h>
#include <string.h>
void read(char letterfreq[], int STRSIZE)
{
printf("The capacity of the string array is %d\n", STRSIZE-1);
printf("Please enter your string:\n");
letterfreq[strlen(letterfreq) - 1] = '\0';
return;
}
void lowerCase(char letterfreq[], int STRSIZE)
{
}
}
void countFreq(int counter[], int COUNT, char letterfreq[], int STRSIZE)
{
int c = 0;
int i;
for(i = 0; i < STRSIZE; i++)
}
else
if((letterfreq[i] < 'a') || (letterfreq[i] > 'z'))
{
counter[COUNT-1]++;
}
}
}
return;
}
void printFreq(int counter[], int COUNT)
{
int c;
printf("Here is the letter frequency of your string:\n");
for(c = 0; c < COUNT; c++)
printf("There were also %d special characters in the string.\n", counter[26]);
return;
}
char promptReset(char reset)
{
printf("Would you like to calculate the letter frequency of another string? (Y/N)\n");
scanf("%c%*c", &reset);
do
{
readString(letterfreq, STRSIZE);
lowerCase(letterfreq, STRSIZE);
countFreq(counter, COUNT, letterfreq, STRSIZE);
printFreq(counter, COUNT);
reset = promptReset(reset);
}while(reset == 'y' || 'Y');
return(0);
}
答案 0 :(得分:4)
这里(在函数lowerCase
中):
letterfreq[i] = letterfreq[i] - 32;
您需要添加 32才能将字符转换为大写。查看ASCII table以了解原因。
这里(在函数countFreq
中):
for(i = 0; i < STRSIZE; i++)
{
while(letterfreq[i] != '\0')
{
if(letterfreq[i] >= 'a' && letterfreq[i] <= 'z')
{
counter[c]++;
c++;
}
else
if((letterfreq[i] < 'a') || (letterfreq[i] > 'z'))
{
counter[COUNT-1]++;
}
}
}
for
循环没有任何意义。去掉它。然后,while
循环永远不会结束,因为您不会增加i
。您需要在i++;
循环结束时while
。
下面:
if(letterfreq[i] >= 'a' && letterfreq[i] <= 'z')
{
counter[c]++;
c++;
}
counter[c]++
不计算每个角色的频率。你需要
counter[letterfreq[i]-'a']++;
为此。不需要c
。将其从功能中删除。
此外,作为@iharob has mentioned in a comment,
}while(reset == 'y' || 'Y');
应该是
}while(reset == 'y' || reset == 'Y');
<小时/> 修正程序:
#include <stdio.h>
#include <string.h>
const int COUNT = 27;
const int STRSIZE = 100;
void readString(char letterfreq[], int STRSIZE)
{
printf("The capacity of the string array is %d\n", STRSIZE-1);
printf("Please enter your string:\n");
fgets(letterfreq, STRSIZE, stdin);
letterfreq[strlen(letterfreq) - 1] = '\0';
//return; Not required
}
void lowerCase(char letterfreq[], int STRSIZE)
{
int i;
for(i = 0; i < STRSIZE; i++)
{
if(letterfreq[i] >= 'A' && letterfreq[i] <= 'Z')
{
letterfreq[i] = letterfreq[i] + 32; //Add instead of subtract
}
//i++; You already increment i from the loop
}
}
void countFreq(int counter[], int COUNT, char letterfreq[], int STRSIZE)
{
int i=0; // You forgot to initialize i with 0
while(letterfreq[i] != '\0')
{
if(letterfreq[i] >= 'a' && letterfreq[i] <= 'z')
{
counter[letterfreq[i]-'a']++;
}
else if((letterfreq[i] < 'a') || (letterfreq[i] > 'z')) //is always true; Use `else`
{
counter[COUNT-1]++;
}
i++; //You forgot this
}
//return; Not required
}
void printFreq(int counter[], int COUNT)
{
int c;
printf("Here is the letter frequency of your string:\n");
for(c = 0; c < COUNT-1; c++) //See change here too
{
printf("%c \t %d \n", c + 'a', counter[c]);
}
printf("There were also %d special characters in the string.\n", counter[26]);
//return; Not required
}
char promptReset(char reset)
{
printf("Would you like to calculate the letter frequency of another string? (Y/N)\n");
scanf("%c%*c", &reset);
return(reset);
}
int main()
{
char letterfreq[STRSIZE];
char reset = '\0'; //Always initialize variables to prevent unexpected behavior
int counter[COUNT];
printf("Welcome to the Letter Frequency program\n");
do
{
memset(counter,0,sizeof(counter)); //Initializing counter with zeros
memset(letterfreq,'\0',sizeof(letterfreq)); //Do the same for letterfreq
readString(letterfreq, STRSIZE);
lowerCase(letterfreq, STRSIZE);
countFreq(counter, COUNT, letterfreq, STRSIZE);
printFreq(counter, COUNT);
reset = promptReset(reset);
}while(reset == 'y' || reset == 'Y'); //Changed
return(0);
}