0%

C/操作字符串的函数

操作字符串的函数

strcpy()

1
2
3
4
5
6
7
#include<stdio.h>
#include<string.h>
int main(){
char a[100]="hello";
char b[100];
strcpy(b,a);
printf("b:%s\n",b);}

strcat()

1
2
3
4
5
6
7
8
#include<stdio.h>
#include<string.h>
//strcat()将后面的字符串接到前面的字符串后面
int main(){
char a[100]="I love you,";
char b[100]="999!";
strcat(a,b);//注意这里是连接了b字符串到a字符串
printf("a:%s\n",a);}//输出的a字符串即是加工后的字符串

输出结果是: I love you,999

srtlen()

1
2
3
4
5
6
7
8
9
10
11
12
#include<stdio.h>
#include<string.h>
//strlen()用于获取字符串的长度
int main(){
char a[100]="I love you,";
char b[100]="999!";
int len1=0,len2=0;
len1=strlen(a);
len2=strlen(b);
printf("a字符串的长度是:%d,b字符串的长度是:%d\n",len1,len2);}
//输出两个字符串的长度
//输出结果分别是:a字符串的长度是:11,b字符串的长度是:4