注:c语言中提供了许多有用的字符串处理函数,若要使用这些函数需使用头文件<string.h>
1.字符串整体输出函数puts
格式:puts(字符串)
例如:输出一个字符串"Hello"
puts("Hello");
printf("Hello\n");
你还可以使用如下方式
puts(str); //自带换行
printf("%s\n",str);
2.字符串整行输入函数gets
格式:gets(字符数组名)
例如:输入字符串"Hello World"
#include<stdio.h>
#include<string.h>
int main()
{
char a[15];
gets(a);
printf("a=%s\n",a);
return o;
}
此时只要直接输入Hello World即可。
3.字符串连接函数strcat
格式:strcat(字符数组1,字符串2)
例如:将"Find a new"与"world"一起输出
#include<stdio.h>
#include<string.h>
int main()
{
char a1[20]="Find a new";
strcat(a1,"world");
puts(a1);
return 0;
}
4.字符串复制函数strcpy
格式:strcpy(字符数组1,字符串2)
例如:将"Hi"代替"Hello"输出
#include<stdio.h>
#include<string.h>
int main()
{
char a1[6]="Hello";
strcpy(a1,"Hi");
puts(a1);
return 0;
}