[英]My program doesn't work with strcat - I can't seem to get why?
As a homework, I have to make a program which replaces all the words made of 3 letters with a "*".作为作业,我必须制作一个程序,用“*”替换所有由 3 个字母组成的单词。
For example:例如:
input: cod is everything and I love it
output: * is everything * I love it
Here is my program:这是我的程序:
int main()
{
char cuvant[101], final[101];
char* pch;
cin.get(cuvant, 100);
pch = strtok(cuvant, " ");
while (pch != NULL)
{
if (strlen(pch) == 3)
{
strcat(final, " *");
}
else
{
strcat(final, " ");
strcat(final, pch);
}
pch = strtok(NULL, " ");
}
cout << final;
}
I don't get any error - but the output is blank.我没有收到任何错误 - 但输出是空白的。 Also, the program doesn't exit with the code 0 (as it should be), it says "exited with code -1073741819".
此外,程序不会以代码 0 退出(应该是这样),它说“以代码 -1073741819 退出”。 So obviously something is going on.
很明显,有些事情正在发生。
Am I using strtok/strcat wrong?我使用 strtok/strcat 错了吗?
I feel like strcat can't be used in my case and this is the reason for the error, but I'm not sure.我觉得 strcat 不能用于我的情况,这就是错误的原因,但我不确定。 Am I right?
我对吗?
If so, how can I solve my problem?如果是这样,我该如何解决我的问题?
Thanks.谢谢。
Your problem is that all the C-String
functions expect your string to be null terminated (ie the character '\\0'
as the last character of a string.您的问题是所有
C-String
函数都希望您的字符串以空字符结尾(即字符'\\0'
作为字符串的最后一个字符。
The problem is that final
is not initialized.问题是
final
没有初始化。 It can contain any amount of raw garbage.它可以包含任何数量的原始垃圾。 So when you use
strcat()
it has to find the end (which could be anywhere).因此,当您使用
strcat()
它必须找到结尾(可能在任何地方)。
The solution is to initialize final
to make sure it is a zero length C-String
.解决方案是初始化
final
以确保它是零长度的C-String
。
char cuvant[101];
char final[101] ={0}; // Fills the array with zero
Or.或者。
char final[101];
final[0] = '\0'; // Make a C-String of length 0
Return code.返回代码。
In C++ the return code is 0. But in C it is undefined (as you have no return statement).在 C++ 中,返回码是 0。但在 C 中它是未定义的(因为你没有 return 语句)。 So you compiled your code with the C compiler not the C++ compiler.
因此,您使用 C 编译器而不是 C++ 编译器编译了代码。
An undefined value can be anything.未定义的值可以是任何东西。 The value
-1073741819
is valid as anything.值
-1073741819
是有效的。
You need to initialize final
您需要初始化
final
strcpy(final, "");
to make it null terminated before you start using it with strcat.在开始将其与 strcat 一起使用之前,使其空终止。
or just set或者只是设置
final[0] = 0;
try this for 'c'试试这个'c'
memset( cuvant, 0, sizeof( cuvant ));
memset( final, 0, sizeof( final ));
C++'s string extractors split input on whitespace; C++ 的字符串提取器在空白处拆分输入; that can save you a bunch of parsing.
这可以为您节省大量解析。
std::string word;
std::string result;
while (std::cin >> word) {
result += ' ';
if (word.length == 3)
result += '*';
else
result += word;
you can make the if statement looks like this :你可以让 if 语句看起来像这样:
if (strlen(pch) == 3)
{
strcat(final, "* ");
}
else
{
strcat(final, pch);
strcat(final, " ");
}
so the final string will not start by termination char.所以最终的字符串不会以终止字符开始。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.