连接两个字符串的C ++程序

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-30 13:06   11   0

Here you will get C++ program to concatenate two string without using library function.

在这里,您将获得C ++程序来连接两个字符串而不使用库函数。

Below program will read two strings from user and then concatenate them to form third string.

下面的程序将从用户读取两个字符串,然后将它们连接起来以形成第三个字符串。

#include<iostream>
 
using namespace std;
 
int main()
{
 char str1[30],str2[30],str3[60];
 int i,j;
 cout<<"Enter first string:";
 gets(str1);
 cout<<"\nEnter second string:";
 gets(str2);
 
 for(i=0;str1[i]!='\0';++i)
  str3[i]=str1[i];
  
 for(j=0;str2[j]!='\0';++j)
  str3[i+j]=str2[j];
  
 str3[i+j]='\0';
 
 cout<<"\nThe concatenated string is "<<str3;
 
 return 0;
}

Output

输出量

Enter first string:Hello

输入第一个字符串:你好

Enter second string:World

输入第二个字符串:World

The concatenated string is HelloWorld

串联的字符串是HelloWorld

翻译自: https://www.thecrazyprogrammer.com/2011/03/c-program-to-concatenate-two-strings-2.html

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP