简体   繁体   English

我的程序不适用于 strcat - 我似乎不明白为什么?

[英]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.

相关问题 为什么我的程序无法正常工作 - Why Doesn't My Program Work Correctly strcmp 不起作用,我似乎不明白为什么 - 转换为 ASCII 代码使程序按预期工作 - Strcmp doesn't work and I don't seem to understand why - transformation to ASCII code makes the program work as expected though 似乎无法使我的IF语句正常工作 - Can't seem to get my IF statement to work properly 我似乎无法让 msvc 链接器在 vscode 上正常工作 - I can't seem to get the msvc linker to work properly on vscode 为什么我的自动售货机程序不能正常工作? - Why doesn't my vending machine program work correctly? 为什么 For 循环中的 While 循环在我的程序中不起作用? - Why doesn't a While Loop inside a For Loop work in my program? 我的计算最终成绩的程序没有计算它,我不知道为什么 - My program for calculating the final grade doesn't calculate it and I can't tell why C++:我的链表尾部没有变化? 我似乎无法让节点正确指向彼此? - C++: My linked list tail doesn't change? I can't seem to get the nodes to point to each other correctly? 为什么当我放置偶数个元素时我的程序不起作用 - Why my program doesn't work when I put an Even Number of Elements 当我在终端中输入程序时,我的程序不起作用 - My program doesn't work when I enter it in the terminal
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM