c语言如何将8个字符串串联

In this article, we will unveil the various ways of performing string concatenation in the C++ language. This method can be used for various purposes while programming. But in general, the concept is the same as combining two strings from different locations and placing them together.

在本文中,我们将介绍使用C ++语言执行字符串连接的各种方法。 编程时,该方法可用于多种目的。 但总的来说,此概念与组合来自不同位置的两个字符串并将它们放置在一起相同。



C ++中的字符串连接技术 (Techniques of String Concatenation in C++)

The following techniques can be taken into consideration while concatenating strings in C++:

在C ++中连接字符串时,可以考虑以下技术:

  • C++ concatenate (+) operatorC ++串联(+)运算符
  • The strcat() method所述 的strcat()方法
  • C++ append() functionC ++ append()函数
  • Using C++ for loop for concatenation使用C ++进行串联


1.用于字符串连接的C ++'+'运算符 (1. C++ ‘+’ operator for String Concatenation)

C++ '+' operator can be used to concatenate two strings easily.

C ++ '+' operator可用于轻松连接两个字符串。

The ‘+’ operator adds the two input strings and returns a new string that contains the concatenated string.

“ +”运算符将两个输入字符串相加,返回一个包含串联字符串 的新 字符串

Syntax:

句法:


string1 + string2;

Example:

例:


#include <bits/stdc++.h>
using namespace std; int main()
{   string str1="", str2="";cout<<"Enter String 1:\n";cin>>str1;cout<<"Enter String 2:\n";cin>>str2;string res = str1 + str2;cout<<"Concatenated String:"<<endl;cout<<res;return 0;
} 

Output:

输出:


Enter String 1:
Journal
Enter String 2:
Dev
Concatenated String:
JournalDev


2. C ++ strcat()方法 (2. C++ strcat() method)

C++ has a built-in method to concatenate strings. The strcat() method is used to concatenate strings in C++.

C ++具有内置方法来连接字符串。 strcat()方法用于连接C ++中的字符串。

The strcat() function takes char array as input and then concatenates the input values passed to the function.

strcat()函数将char数组作为输入,然后将传递给该函数的输入值连接起来。

Syntax:

句法:


strcat(char *array1, char *array2)

Example 1:

范例1:


#include <bits/stdc++.h>
using namespace std; int main()
{   char str1[100] = "Journal";char str2[100]= "Dev";cout<<"Concatenated String:"<<endl;strcat(str1, str2);cout<<str1;return 0;
} 

In the above example, we have declared two char arrays mainly str1 and str2 of size 100 characters. Then, we have passed the char array str1 and str2 to the strcat() function to get the concatenated string as a result.

在上面的示例中,我们声明了两个char数组,主要是大小为100个字符的str1和str2。 然后,我们将char数组str1和str2传递给strcat()函数,以获取串联的字符串。

Output:

输出:


Concatenated String:
JournalDev

Example 2:

范例2:


#include <bits/stdc++.h>
using namespace std; int main()
{   char str1[100], str2[100];cout << "Enter String 1:\n";cin.getline(str1, 100);cout << "Enter String 2:\n";cin.getline(str2, 100);cout<<"Concatenated String:"<<endl;strcat(str1, str2);cout<<str1;return 0;
} 

In the above example, we accept string input values from the user using the getline() function of C++ which fetches the input from the terminal character by character.

在上面的示例中,我们使用C ++的getline()函数从用户那里接收字符串输入值,该函数从终端字符中逐个字符地提取输入。

Output:

输出:


Enter String 1:
JournalDev-
Enter String 2:
Python
Concatenated String:
JournalDev-Python


3. C ++中用于字符串连接的append()方法 (3. The append() Method for String Concatenation in C++)

C++ has another built-in method: append() to concatenate strings. The append() method can be used to add strings together. It takes a string as a parameter and adds it to the end of the other string object.

C ++有另一个内置方法: append()连接字符串。 append()方法可用于将字符串添加在一起。 它使用一个字符串作为参数,并将其添加到另一个字符串对象的末尾。

Syntax:

句法:


string1.append(string2);

Example:

例:


#include <bits/stdc++.h>
using namespace std; int main()
{   string str1="", str2="";cout<<"Enter String 1:\n";cin>>str1;cout<<"Enter String 2:\n";cin>>str2;str1.append(str2);cout<<"Concatenated String:"<<endl;cout<<str1;return 0;
}

In the above example, we have passed str2 as a parameter to the append() function. Further, the append() functions add the contents of the string object str2 to the end of the contents of string object str1. Thus, serving the purpose of string concatenation.

在上面的示例中,我们已将str2作为参数传递给append()函数。 此外,append()函数将字符串对象str2的内容添加到字符串对象str1的内容的末尾。 因此,达到了字符串串联的目的。

Output:

输出:


Enter String 1:
Journal
Enter String 2:
Dev
Concatenated String:
JournalDev

4.使用C ++进行循环 (4. Using C++ for loop)

In order to concatenate strings, we can use C++ for loops to serve the purpose without the need of any in-built function.

为了连接字符串,我们可以使用C ++循环来达到目的,而无需任何内置函数。

Example:

例:


#include<iostream>
#include<string.h>
using namespace std;int main()
{char x[100]="Journal", y[100]="Dev";cout<<"String 1:\n";cout<<x<<endl;cout<<"String 2:\n";cout<<y<<endl;int p;for(p=0; x[p] != '\0'; p++);//pointing to the index of the last character of xfor(int q=0; y[q] != '\0'; q++,p++){x[p]=y[q];}x[p]='\0';cout<<"Concatenated String:\n";cout<<x<<endl;return 0;
}

In the above snippet of code, we have accepted two char array inputs mainly: x and y, respectively.

在上面的代码片段中,我们主要接受了两个char数组输入: xy

Further, we have traversed through the string of x char array till the pointer variable p points to the index of the last character of x.

此外,我们遍历了x char数组的字符串,直到指针变量p指向x的最后一个字符的索引

Then, we traverse through the character input of char array y and concatenate each character of y to x.

然后,我们通过char数组y的字符输入遍历和y的每一个字符串联至x。

In the end, we add a null character ('\0') to the end of the char array x which now contains the concatenated string as a result.

最后,我们在char数组x的末尾添加一个null character ('\0') ,该数组现在包含连接字符串。

Output:

输出:


String 1:
Journal
String 2:
Dev
Concatenated String:
JournalDev


结论 (Conclusion)

Thus, in this article, we have understood the various techniques to concatenate strings in the C++ language.

因此,在本文中,我们了解了使用C ++语言连接字符串的各种技术。



参考资料 (References)

  • Concatenate String in C++ – StackOverFlow用C ++连接字符串– StackOverFlow

翻译自: https://www.journaldev.com/37453/string-concatenation-in-c-plus-plus

c语言如何将8个字符串串联

c语言如何将8个字符串串联_C ++中的字符串串联:串联字符串的4种方法相关推荐

  1. 浅谈C语言将字符串中的空格替换成%20的几种方法(附图超级详细解答)

    文章目录 题目分析 暴力位移法 巧用数组法 高效倒放法 题目分析 将字符串中的空格替换为%20.样例: "abc defgx yz" 转换成 "abc%20defgx%2 ...

  2. C语言中三个数比较大小详解——三种方法

    ​ C语言中三个数比较大小详解--三种方法 方法一:if-else法 方法二:函数法 方法三:三目运算符法 C语言中比较三个数的大小有很多方法,以下是我总结的三种方法: 首先我定义 int a = 1 ...

  3. html语言显示动态当前日期和时间,举一反三 浅谈在网页上显示日期的两种方法-网页设计,HTML/CSS...

    在上网的时候,经常会在一些网页上看到当前的日期,如:"今天是×年×月×日星期×"等字样.为了显示系统当前的日期,一般采用脚本语言vbscript或javascript,两种语言有其 ...

  4. python多行字符串输入_python中怎么输入多行字符串

    python中怎么输入多行字符串,疾风,不言,努力,人生,起风了 python中怎么输入多行字符串 易采站长站,站长之家为您整理了python中怎么输入多行字符串的相关内容. Python中输入多行字 ...

  5. python判断字符串合法,详解Python判定IP地址合法性的三种方法 python中判断一个字符串是否是IP地址...

    html 中 鼠标放在标签上会显示小手状,其它标签在其他标签上,美工给加了一些样式,鼠标放上去也显示小手状.有哪位大手状样式 有什么不懂的前端问题可以去菜鸟驿站.全都是泡沫,只一刹的花火,所谓的友情, ...

  6. Java字符串替换所有指定字符_C++中string替换所有指定字符串的方法

    C++的string提供了replace方法来实现字符串的替换,但是对于将字符串中某个字符串全部替换这个功能,string并没有实现,我们今天来做的就是这件事. 首先明白一个概念,即string替换所 ...

  7. python空字符串意义_Python中的None与空字符串”的区别

    1.首先要了解Pythond的对象的概念: Python中,万物皆对象,所有的操作都是针对对象的,那什么是对象,5是一个int对象,'oblong'是一个str对象,异常也是一个对象,抽象一点是,人, ...

  8. qt5中字符串转字符串数组_Qt中整形数组转换成字符串的问题

    应项目的要求终于在一天中学会了把整型数组转换成QString,也算是小有成就的一天了. 由于还没发现怎么直接把整型数组转换成string类型,但是可以通过间接的方法来实现.首先要实现在Qt中建立整型数 ...

  9. c++十六进制加法_C++中输出十六进制形式的字符串

    前言 在进行 i18n 相关的开发时,经常遇到字符编码转换的错误.这时如果能把相关字符串用十六进制的形式打印出来,例如,"abc" 输出成 "\\x61\\x62\\x6 ...

最新文章

  1. [转帖]一位“鬼佬”总经理的管理艺术
  2. python控制结构(二)_Python程序控制结构---2
  3. RHEL6.5/Centos6.5 搭建bugzilla
  4. 世界顶级精英们的人生哲学 【转】
  5. webpack最新版本_webpack小结-开发环境构建优化
  6. 学习Python,在人工智能的风口抢占未来
  7. javascript中基本类型和引用类型的区别分析
  8. bjca客户端 win10_BJCA证书助手 V2.14.4 官方版
  9. 80-450-024-原理-索引-索引练习
  10. 零基础的同学看过来,如何系统学习前端,保证让你不亏
  11. Sybase:数据类型(对比sqlserver)
  12. Spring Security的HTTP基本验证示例
  13. 计算机网络应用基础_学习笔记之《计算机网络》概述
  14. A Simple Math Problem (矩阵快速幂)
  15. 六石管理学:人是不可以被说服的,除非自己想明白
  16. 文华软件怎样测试交易系统的收益,交易系统的测试与评估报告
  17. 政府信息化与电子政务
  18. 甲骨文裁员是在为云业务转型太慢埋单吗?
  19. 用react-custom-scrollbars插件美化 Ant Design Table 滚动条
  20. Provisional headers are shown axios 超时处理

热门文章

  1. linux使用mount命令挂载、umount命令取消挂载
  2. mysql 8.0以上重置密码
  3. [转载] 致 Python 初学者
  4. 在consul上注册web服务
  5. ROS工作空间和程序包创建
  6. Mybatis一对一和一对多配置
  7. Unity3D AssetBundle相关
  8. 2016/09/19
  9. Django实战(10):单元测试
  10. Python编程实例(4)