字符串函数:strcat()
String Functions: 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] != ' '; 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;
}
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 为什么使用 "this" 指针调用派生成员函数?
- 类似于strcat()的函数出现问题
- 字符串函数:strcat()
- 从 'char' 到 'char*' strcat 函数的转换无效
- 使用指针编写 strcat 函数
- 使用char数组的strcat函数
- 我正在尝试创建自己的 strcat() 函数,而不是使用库实现函数<cstring>
- 为什么我无法将迭代器作为字符串传递给函数 strcat() 的参数
- 如何修复strcat函数中的错误
- C++ 字符串行为在定义和在 strcat 函数中是不同的