C++ 从数组中计算字母字符数?
所以,在这个节目中,我们必须 A.计算字符数组中每个字母的数量,并将这些计数存储在整数数组中 我理解这一部分,但它说我们必须使用ASCII值,这让我感到困惑 这是供参考的文件:C++ 从数组中计算字母字符数?,c++,arrays,character,ascii,alphabet,C++,Arrays,Character,Ascii,Alphabet,所以,在这个节目中,我们必须 A.计算字符数组中每个字母的数量,并将这些计数存储在整数数组中 我理解这一部分,但它说我们必须使用ASCII值,这让我感到困惑 这是供参考的文件: " A frog walks into a bank and asks the teller, Miss Pattywack for a loan. Miss Pattywack tells the frog, "you must have collateral for the loan".
" A frog walks into a bank and asks the teller, Miss Pattywack
for a loan. Miss Pattywack tells the frog,
"you must have collateral for the loan".
The frog pulls out of his pouch his porcelain people collection
and offers them as collateral.
Miss Pattywack looks at the figurines and tells the frog that
the collection is not acceptable.
The bank manager overhears the conversation and yells to the
teller - "It's a nick nack Pattywack, give the frog a loan!"
使用for循环检查字符数组中的每个字符
一,。使用toupper来区分字母的大小写,因此您只处理大写字母
二,。如果字符是字母表中的一个字母,则在字符的ASCII值减去65的位置增加整数数组
1) 65是字母“A”的ASCII值
(1) 如果字符是A,则65-65=0要为字符A增加的位置
(2) 如果字符为C,则67-65=2要为字符C增加的位置
到目前为止,我有:
void CountAlphabetCharacters(char chArray[MAX_CHARACTERS], int lengthOfArray)
{
int index;
for(index = 0; index <= lengthOfArray; index++)
{
chArray[index] = toupper(chArray[index]);
static_cast<int>(index);
}
}
void CountAlphabetCharacters(字符字符[MAX_CHARACTERS],int lengthOfArray)
{
整数指数;
对于(索引=0;索引void CountAlphabetCharacters)(字符字符字符[MAX_CHARACTERS],int lengthOfArray)
{
整数指数;
int计数器[26]={0};//保存每个字符的匹配项
对于(索引=0;索引
一个数组,用于保存您读取的字符。我认为chArray
就是这个数组
保存字母表中字母数的数组。我没有看到那个数组
更新CountAlphabetCharacters
中的代码,查看chArray
中的字符,并更新chArray
中的字母计数
调用CountAlphabetCharacters
之前,请创建用于保存字符计数的数组
int charCountArray[26] = {0};
将函数CountAlphabetCharacters
更改为接受它作为参数
void CountAlphabetCharacters(char chArray[MAX_CHARACTERS],
int charCountArray[],
int lengthOfArray)
在调用CountAlphabetCharacters
时将其用作参数
void CountAlphabetCharacters(char chArray[MAX_CHARACTERS],
int charCountArray[],
int lengthOfArray)
{
int index;
int ch;
for(index = 0; index <= lengthOfArray; index++)
{
// The character at the index.
ch = chArray[index];
// Check whether it is an alphabetical character.
if ( isalpha(ch) )
{
// If so, make it the uppercase letter.
ch = toupper(ch);
// Increment the charCountArray for the letter.
charCountArray[ch-'A']++;
}
}
}
更新CountAlphabetCharacters
的实现
void CountAlphabetCharacters(char chArray[MAX_CHARACTERS],
int charCountArray[],
int lengthOfArray)
{
int index;
int ch;
for(index = 0; index <= lengthOfArray; index++)
{
// The character at the index.
ch = chArray[index];
// Check whether it is an alphabetical character.
if ( isalpha(ch) )
{
// If so, make it the uppercase letter.
ch = toupper(ch);
// Increment the charCountArray for the letter.
charCountArray[ch-'A']++;
}
}
}
void CountAlphabetCharacters(char chArray[MAX_CHARACTERS],
int charCountArray[],
国际长度(法拉利)
{
整数指数;
int-ch;
对于(index=0;index所以您基本上是在问如何将文件中的文本读取到字符数组中?您可以检查的功能。我已经将文本文件读取到数组中。我需要计算字母表中的每个字母在文件中出现的次数。但是使用ASCII值65,我不明白您是什么意思事实上:-/…for(char c='A',index=0;index@user2702245,复制粘贴效果:D。谢谢。这有点帮助,我可能需要更改一些以适应,但这有助于我找到正确的方向。谢谢!