0. C++
using namespace std;
int main(){
string s="abcajdjhs";
string b="abcajdjhs";
char chr_add= 'h';
s=s+chr_add;
cout<<s<<endl;
cout<<b<<endl;
return 0;
}
cosole输出如下:
真是简单粗暴,直接+就行了。。。
#include<iostream>
#include <string>
using namespace std;
int main(){
string s="Hello,";
string b="abcajdjhs";
string chr_add= "World!";
s=s+chr_add;
cout<<s<<endl;
cout<<b<<endl;
return 0;
}
console输出如下:
字符串也可以直接加到后面。
1. C语言
C语言无法直接定义字符串,只能用字符指针或者字符数组的方式;
#include <stdio.h>
#include <string.h>
int main(){
char s[10]="abcajdjhs"; //字符数组
char *b="abcajdjhs"; //字符指针
char chr_add= 'h';
s[9]=chr_add;
s[10]=' ';
printf("字符串s为:%s\n",s);
printf("字符串b为:%s\n",b);
//printf("字符串s的地址为:%p\n",s);
//printf("字符串b的地址为:%p\n",b);
printf(" Hello \n");
return 0;
}
cosole输出如下: