首页 > 解决方案 > 字符串函数:Strcat()

问题描述

我目前正在编写一个使用字符串函数的程序。我需要一些关于如何在 main() 中使用 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;

我的输出:

s1:  Hello

The length of s1: 5

Doing strcat(s1, s2)
s1:
The length of s1: 0

Doing strcpy(s1, s2)
s1:  World
The length of s1: 5

假设第 6 行和第 7 行显示 Hello World 及其长度(即 11)。

标签: c++

解决方案


您的每个功能都有许多不完全正确的开始。首先,让我们考虑一下每个人的回报。myStrlen应该返回size_t而不是int. C++ 为计数器、测量等指定 size_type。其余函数应返回char*(或nullptr失败时)。

查看myStrlen您拥有的功能

for (i=0; str1[i] != '\0'; i++)
str1[i] = '\0';

您将每个字符设置str1空字符,因为您将循环应用于下一个表达式。你不应该担心 nul 终止内部的任何东西myStrlen——你只是在计算字符。所以你可以改写如下:

size_t myStrlen (const char *str)
{
    size_t l = 0;
    for (; str[l]; l++) {}
    return l;
}

myStrcpy看起来可行,但您应该始终验证您的输入参数不是nullptr在使用它们之前 - 我把它留给您。既然你有一个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,因此您实际上只需要调用 tomyStrlen然后调用 tomyStrcpy以复制src到该偏移量 in dest,例如

char *myStrcat (char *dest, const char *src)
{
    size_t len = myStrlen (dest);
    return myStrcpy (dest + len, src);
}

在您的main()中,如果您想要在"Hello"and之间留一个空格"World",那么对于保存需要的连接字符串(包括nul 终止字符)const int SIZE = 11;来说太低了。不要吝啬缓冲区大小。很小。"Hello World"12-bytes128

保留您的main()但更新SIZE = 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;
}

注意:不要包括using namespace std;,这只是当今时代的不良形式)

示例使用/输出

$./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

如果您还有其他问题,请仔细查看并告诉我。


推荐阅读