c++ - 字符串函数: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)。
解决方案
您的每个功能都有许多不完全正确的开始。首先,让我们考虑一下每个人的回报。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-bytes
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
如果您还有其他问题,请仔细查看并告诉我。
推荐阅读
- javascript - 停止自动播放到轮播嵌入视频上的下一张幻灯片
- javascript - JavaScript throw 语句 *需要帮助*
- javascript - 移动鼠标时在 Firefox 中出现生涩的 javascript 动画
- android - 您的应用不符合 Google Play 政策,但确实如此
- rabbitmq - RabbitMQ Http Authentication Backend with SSL
- fortran - Undefined symbols for architecture x86_64 in a NLopt Fortran compilation
- python - Two-point Euclidean distance from csv file
- rust - 所有权转移后的变异变量
- r - 如何加入相似字符的数据集?
- swift - 以版本为条件的 Swift 扩展