1. strcat函数–字符串连接函数。
#include"stdio.h"
#include"string.h" //为了引用strcat函数。
int main()
{
char str1[30]={"hello "},str2[]={"word."};
printf("%s",strcat(str1,str2)); //strcat(a字符数组,b字符数组);
} //strcat函数--字符串连接函数
//a字符数组必须足够大,以便容纳后面的新字符串。
2.strcpy和strncpy函数–字符串复制函数。
#include<stdio.h>
#include<string.h> //为了使用strcpy和strncpy函数
int main()
{
char str[20]={"1234567890"},a[]={"hello"},b[20],d[20]={"1234567890"};
strcpy(str,a); //一般形式 strcpy(字符数组,字符串); 作用:将字符串复制到字符数组中去。
strcpy(b,"world");
strncpy(d,a,2); //作用 将a中最前面两个字符复制到字符数组d中,字符数组d后面的保持原样。
puts(str);
puts(b);
puts(d);
}
3.strcmp函数–字符串比较函数
#include<stdio.h>
#include<string.h> //strcmp(字符数组1,字符数组2);比较第一个不同字符的ASCII码
int main()
{
char a[]={"happy"},b[]={"help"},c[]={"happy"};
printf("%d\n",strcmp(a,b)); //'a' <'e' 函数值为负数
printf("%d\n",strcmp(b,a));
printf("%d\n",strcmp(a,c)); //全部字符相同 函数值为0
}
4.strlen函数–测字符串长度的函数。
作用:测字符串长度
函数值为字符串的实际长度(不包括’\0’)。
#include"stdio.h"
#include"string.h"
int main()
{
char a[10]={"12345"};
printf("%d\n",strlen(a));
printf("%d\n",strlen("he"));;
}
5.strlwr函数与strupr函数 --大小写字母转换函数
#include"stdio.h"
#include"string.h"
int main()
{
char a[]={"aBcDeFgH"};
puts(strlwr(a)); //strlwr函数 将字符串中的大写字母转换为小写。
puts(strupr(a)); //strupr函数 将字符串中的小写字母转换为大写。
}