【发布时间】:2015-01-12 05:19:29
【问题描述】:
我和一个朋友正在尝试解决this 问题来练习编程。我们已经解决了它,但它实际上只能在他运行 ubuntu 的计算机上运行。他正在使用gedit进行编译。我们想出的解决方案是:
#include <iostream>
#include <string>
using namespace std;
int main()
{
char input[999], sentence[999];
cout << "Enter a sentence in all lower case with no punctuation." << endl;
cin.getline(input, 999);
strcat(sentence, input);
for(int x=0; input[x] != 0; x++)
{
if (!(input[x] == 'a' || input[x] == 'e' || input[x] == 'i' || input[x] == 'o' || input[x] == 'u' || input[x] == ' '))
{
cout << input [x];
}
}
cout << endl;
for(int x=0; input[x] != 0; x++)
{
if (input[x] == 'a' || input[x] == 'e' || input[x] == 'i' || input[x] == 'o' || input[x] == 'u')
{
cout << input[x];
}
}
cout << endl;
return 0;
}
它在他的计算机上成功运行,但我使用的是 Visual Studio 12,我得到了一个 debug assertion error。它指出
Expression: (L"String is not null terminated"&&0).
我收到此错误而他没有收到此错误的任何原因?
【问题讨论】:
-
'sentence' 未初始化,所以指向它的是 UB。
-
使用 strcpy 代替 strcat。
-
..或者使用实际的字符串类型而不是字符数组。
-
事实上,你为什么还需要一个句子变量呢?你似乎没有使用它。
-
@icemanind - 好点:)没有注意到它没有被使用。也许 OP 为 SSCCE 编辑了一堆杂乱无章的东西。
标签: c++