字符串函数:strcat()

String Functions: Strcat()

本文关键字:strcat 函数 字符串      更新时间:2023-10-16

我目前正在编写一个使用字符串函数的程序。我需要一些关于如何在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] != ''; i++)
str1[i] = '';
return i;
}
int myStrcat(char str2[], char str3[])
{
}
int myStrcpy(char str4[], char str5[])
{
int i = 0;
for (i=0; str5[i] != ''; i++)
str4[i] = str5[i];
str4[i] = '';
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(。

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

查看您的myStrlen功能,您拥有

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

您将str1中的每个字符都设置为nul-字符,因为您将循环应用于下一个表达式。你不应该担心 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,因此您实际上只需要调用myStrlen,然后调用myStrcpy以将src复制到该偏移量dest,例如

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

在你的main()中,如果你想要在"Hello""World"之间有一个空格,那么const int SIZE = 11;太低了,无法容纳需要12-bytes的串联字符串"Hello World"(包括nul终止字符(。不要吝啬缓冲区大小。128很小。

保留您的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

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

首先,您应该阅读为什么"使用命名空间 std;"被认为是不好的做法?

如果要开始编程,请不要使用 c 样式字符串。使用 std::string。它使用起来要简单得多。

#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 << "nn"; ///Should display "Hello"
std::cout << "The length of s1: " << myStrlen(s1) << "nn";
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) << "nn";
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) << "nn";
return 0;
}