首页 > 代码库 > 自己实现strcat函数
自己实现strcat函数
#include <iostream>
using namespace std;
// 自己实现strcat函数
void Strcat(char to[], char from[]);
int main()
{
char to[30], from[30];
cout << "请输入第一个字符串:";
cin >> to;
cout << "请输入第二个字符串:";
cin >> from;
Strcat(to, from);
system("pause");
return 0;
}
void Strcat(char to[], char from[])
{
if (30 < strlen(from) + strlen(to) + 1) //判断是否满足将from中的全部字符连接到to字符串后面;
cout << "连接失败!不满足连接的要求!" << endl;
else
{
int i = 0;
for (; to[i] != ‘\0‘; i++);
for (int j = 0; from[j] != ‘\0‘; j++, i++)
to[i] = from[j];
to[i] = ‘\0‘; //给字符数组加上‘\0‘形成字符串;
cout << "第一个与第二个字符串连接后的结果:" << to << endl;
}
}
using namespace std;
// 自己实现strcat函数
void Strcat(char to[], char from[]);
int main()
{
char to[30], from[30];
cout << "请输入第一个字符串:";
cin >> to;
cout << "请输入第二个字符串:";
cin >> from;
Strcat(to, from);
system("pause");
return 0;
}
void Strcat(char to[], char from[])
{
if (30 < strlen(from) + strlen(to) + 1) //判断是否满足将from中的全部字符连接到to字符串后面;
cout << "连接失败!不满足连接的要求!" << endl;
else
{
int i = 0;
for (; to[i] != ‘\0‘; i++);
for (int j = 0; from[j] != ‘\0‘; j++, i++)
to[i] = from[j];
to[i] = ‘\0‘; //给字符数组加上‘\0‘形成字符串;
cout << "第一个与第二个字符串连接后的结果:" << to << endl;
}
}
自己实现strcat函数
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。