比较要打印的字符数组
Compare character arrays to be printed
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int i, a = 0, j;
int num[26]={};
char alp[26], ch[100]={'s', 'd', 'd', 'e', 'f', 'g' };
//Initialize array alp[] with alphabets a to z.
for(int i=97; i <(97+26) ; i++)
alp[i-97]=i;
for (i = 0; i < 26; i++)
{
for (j = 0; ch[j] != ' '; j++)
{
if (alp[i] == ch[j])
num[i] = a++;
}
cout << endl << string(5, ' ') << alp[i] << string(5, ' ');
if (num[i])
cout << num[i] << endl;
else cout << endl;
}
return 0;
}
没有编译错误。但是,当我打印时,我会得到垃圾值(num[i](。基本上,我将一个字符数组与一个字母数组进行比较,然后打印一个表,其中包含ch数组包含的字母数(alp数组,已经用所有字母a到z初始化(。
您的数组num
未初始化,因此它会得到垃圾值。如果你想用0初始化它,请更改你的代码:
int num[26] = {};
为了解决这个问题,当您初始化循环中使用的每个值时,问题会变得更清楚,但效果较差
int i, a = 0, j;
int num[26];
for (i = 0; i < 26; i++)
{
num[i]=0;
...
当然,初始化是更短、更干净的解决方案。
相关文章:
- 指向指向字符数组的指针数组的指针
- 比较字符数组
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 使用无符号字符数组有效存储内存
- 错误:字符数组的初始值设定项太多
- 对字符数组中的元素执行逐位操作
- C++ 无法在字符数组中使用 for 循环打印字母模式
- 如何在 C++ 中从文件中读取字符数组(带有一些空格)
- C++ 传递二维字符数组
- 无法在 C++ 中输入字符数组
- 字符数组中的元素数
- 我的目标是编写一个程序来计算和存储字符串在字符数组中出现的位置
- 使用字符数组作为 Map 中的键
- C++:__aligned(__alignof__) 导致字符数组数据出现问题?
- 按顺序声明的字符数组重叠
- 在 C++ 中字符串的大小与字符数组的大小
- 寻找一种更好的方法来表示无符号字符数组
- 如何打印 2D 字符数组C++
- 从字符数组的元素中减去'a'是什么意思
- 我是否不正确地集中了这些字符数组?