【发布时间】:2016-08-30 08:37:03
【问题描述】:
我正在尝试编写一个小函数来打印中间和末尾包含 0 的字符数组。我的功能无法正确打印,我缺少什么 :-(
预期结果:再见0ld0
实际:再见了
// call function
char str5[] = { 'G','o','o','d','-','b','y','e',0,'l','d',0 };
displayResult(str5);
// This is function
void displayResult(string _st)
{
int l = _st.length();
for (int i = 0; i<=l; i++)
{
cout << _st[i];
//printf("%s\n", _st[i]);
}
cout << endl;
}
【问题讨论】:
-
0 是一个 eos 标记。使用“0”。
-
我不想改变我的数组。我想修改我的打印功能。 @约翰尼莫普
-
cout << ( _st[i] ? _st[i] : '0' ); -
你想打印一个零还是空格?零字节是 C 风格字符串中的字符串终止符。
-
1.
<=在您的for循环中太远了 2. 为什么要将数组作为std::string传递?