C++ 字符串函数:Strcat()

C++ 字符串函数:Strcat(),c++,C++,我目前正在编写一个使用字符串函数的程序。我需要一些建议/提示,告诉我如何使用myStrcat显示Hello World及其长度。我是编程新手,任何支持都将不胜感激 我的代码: #include <iostream> #include <cstring> #include <string> using namespace std; int myStrlen(char str1[]) { int i = 0; for (i=0; str1[i]

我目前正在编写一个使用字符串函数的程序。我需要一些建议/提示,告诉我如何使用myStrcat显示Hello World及其长度。我是编程新手,任何支持都将不胜感激

我的代码:

#include <iostream>
#include <cstring>
#include <string>

using namespace std;

int myStrlen(char str1[])
{
    int i = 0;
    for (i=0; str1[i] != '\0'; i++)
    str1[i] = '\0';

    return i;
}

int myStrcat(char str2[], char str3[])
{

}

int myStrcpy(char str4[], char str5[])
{
    int i = 0;
    for (i=0; str5[i] != '\0'; i++)
    str4[i] = str5[i];
    str4[i] = '\0';

    return i;
}

int main()
{
    const int SIZE = 11;
    char s1[SIZE] = "Hello";
    char s2[SIZE] = "World";

    cout << "s1: " << " " << s1 << endl << endl; ///Should display "Hello"
    cout << "The length of s1: " << myStrlen(s1) << endl << endl;

    cout << "Doing strcat(s1, s2) " << endl;
    myStrcat(s1, s2);
    cout << "s1: " << " " << s1 << endl; /// Should display "Hello World"
    cout << "The length of s1: " << myStrlen(s1) << endl << endl;

    cout << "Doing strcpy(s1, s2) " << endl;
    myStrcpy(s1, s2);
    cout << "s1: " << " " << s1 << endl; /// Should display "World"
    cout << "The length of s1: " << myStrlen(s1) << endl << endl;

假设第6行和第7行显示Hello World,其长度为11。

您的每个函数都有许多不完全正确的开头。首先,让我们考虑一下每种产品的回报。MySTLLN应该返回siZeTt而不是int。C++指定计数器、测量等的sisiz类型。失败时,其余函数应返回char*或nullptr

看看你的myStrlen函数

for (i=0; str1[i] != '\0'; i++)
str1[i] = '\0';
您正在将str1中的每个字符设置为nul字符,因为您正在将循环应用于下一个表达式。你不应该担心nul终止myStrlen中的任何内容——你只是在计算字符数。因此,您可以将其重写如下:

size_t myStrlen (const char *str)
{
    size_t l = 0;
    for (; str[l]; l++) {}
    return l;
}
您的myStrcpy看起来是可行的,不过在使用它们之前,您应该始终验证您的输入参数不是null ptr——我把这留给您。由于您有一个myStrlen函数,您只需将其与memcpy一起使用即可创建myStrcpy函数,如下所示:

char *myStrcpy (char *dest, const char *src)
{
    size_t len = myStrlen(src);
    return (char *)memcpy (dest, src, len + 1);
}
注意:在复制或连接时,通常使用源src和目标dest参数

对于myStrcat函数,您只需要使用myStrlen函数在dest中查找偏移量以附加src,因此您实际上只需要调用myStrlen,然后调用myStrcpy将src复制到dest中的偏移量,例如

在您的main中,如果您想在Hello和World之间留一个空格,那么const int SIZE=11;值太低,无法容纳连接字符串Hello World,该字符串需要12个字节,包括nul终止字符。不要忽略缓冲区大小。128相当小

保留主屏幕,但更新大小=12;在Hello和World之间添加一个空格,再调用myStrcat,您可以执行以下操作:

int main (void)
{
    const int SIZE = 12;    /* too short by 1 if you add space between */
    char s1[SIZE] = "Hello";
    char s2[SIZE] = "World";

    std::cout << "s1: " << " " << s1 << std::endl << std::endl;
    std::cout << "The length of s1: " << myStrlen(s1) << std::endl << std::endl;

    std::cout << "Doing strcat(s1, s2) " << std::endl;
    myStrcat(s1, " ");
    myStrcat(s1, s2);
    std::cout << "s1: " << " " << s1 << std::endl;
    std::cout << "The length of s1: " << myStrlen(s1) << std::endl << std::endl;

    std::cout << "Doing strcpy(s1, s2) " << std::endl;
    myStrcpy(s1, s2);
    std::cout << "s1: " << " " << s1 << std::endl;
    std::cout << "The length of s1: " << myStrlen(s1) << std::endl << std::endl;
}
仔细检查一下,如果你还有其他问题,请告诉我。

首先你应该阅读

如果开始编程,不要使用c风格的字符串。使用使用起来简单多了

#include <iostream>
#include <string>

int myStrlen(const std::string &str) {
    return str.length();
}

int myStrcat(std::string &str1, const std::string &str2) {
    str1 += str2;
    str1.length();
}

int myStrcpy(std::string &str1, const std::string &str2) {
    str1 = str2;
    return str1.length();
}

int main() {
    std::string s1 = "Hello";
    std::string s2 = "World";

    std::cout << "s1:  " << s1 << "\n\n"; ///Should display "Hello"
    std::cout << "The length of s1: " << myStrlen(s1) << "\n\n";

    std::cout << "Doing strcat(s1, s2) " << '\n';
    myStrcat(s1, s2);
    std::cout << "s1:  " << s1 << '\n'; /// Should display "Hello World"
    std::cout << "The length of s1: " << myStrlen(s1) << "\n\n";

    std::cout << "Doing strcpy(s1, s2) " << '\n';
    myStrcpy(s1, s2);
    std::cout << "s1:  " << s1 << '\n'; /// Should display "World"
    std::cout << "The length of s1: " << myStrlen(s1) << "\n\n";

    return 0;
}

现在是学习如何调试代码的时候了。一个好的编辑器,可以自动缩进一些应该走很长的路。否则,请使用实际的调试器逐语句逐步执行代码语句,尤其是myStrlen函数。大小11的字母太短,不适合Hello World,因为没有空间终止空“\0”字符。如何使用myStrcat在main中显示Hello World及其长度。陈列可能是糟糕的措辞。假设您的myStrlen和myStrcpy可以正确运行我没有检查的内容,使用myStrlen获取str2的长度len。将str3复制到str2+len。但是,我建议首先修复上面评论中提到的错误。未定义的行为会让你发疯-最好尽快修复。@Scheff我也一样。但我想被告知。特别是在我的空函数情况下,请填写完整的代码。什么?str1[i]='\0';在Strlen中,查找长度时是否将字符串归零??
int main (void)
{
    const int SIZE = 12;    /* too short by 1 if you add space between */
    char s1[SIZE] = "Hello";
    char s2[SIZE] = "World";

    std::cout << "s1: " << " " << s1 << std::endl << std::endl;
    std::cout << "The length of s1: " << myStrlen(s1) << std::endl << std::endl;

    std::cout << "Doing strcat(s1, s2) " << std::endl;
    myStrcat(s1, " ");
    myStrcat(s1, s2);
    std::cout << "s1: " << " " << s1 << std::endl;
    std::cout << "The length of s1: " << myStrlen(s1) << std::endl << std::endl;

    std::cout << "Doing strcpy(s1, s2) " << std::endl;
    myStrcpy(s1, s2);
    std::cout << "s1: " << " " << s1 << std::endl;
    std::cout << "The length of s1: " << myStrlen(s1) << std::endl << std::endl;
}
$./bin/mystrcpy
s1:  Hello

The length of s1: 5

Doing strcat(s1, s2)
s1:  Hello World
The length of s1: 11

Doing strcpy(s1, s2)
s1:  World
The length of s1: 5
#include <iostream>
#include <string>

int myStrlen(const std::string &str) {
    return str.length();
}

int myStrcat(std::string &str1, const std::string &str2) {
    str1 += str2;
    str1.length();
}

int myStrcpy(std::string &str1, const std::string &str2) {
    str1 = str2;
    return str1.length();
}

int main() {
    std::string s1 = "Hello";
    std::string s2 = "World";

    std::cout << "s1:  " << s1 << "\n\n"; ///Should display "Hello"
    std::cout << "The length of s1: " << myStrlen(s1) << "\n\n";

    std::cout << "Doing strcat(s1, s2) " << '\n';
    myStrcat(s1, s2);
    std::cout << "s1:  " << s1 << '\n'; /// Should display "Hello World"
    std::cout << "The length of s1: " << myStrlen(s1) << "\n\n";

    std::cout << "Doing strcpy(s1, s2) " << '\n';
    myStrcpy(s1, s2);
    std::cout << "s1:  " << s1 << '\n'; /// Should display "World"
    std::cout << "The length of s1: " << myStrlen(s1) << "\n\n";

    return 0;
}