几种常用的API
输出字符串
puts()
printf("%s",p)'
获取字符串
gets()
scanf("%s",p)
计算长度
strlen
初始化
memset
拷贝
strcpy
strncpy
拼接
strcat
比较
strcmp
int main()
{
char *str = "Leoishandsome";
char c = 'i';
char *p = NULL;
p = strchr(str, c);
if (p == NULL)
printf("没有找到");
else
puts(p);
return 0;
}
查找
strchr
字符串分割
strstr
strtok
char str[] = "hello,world,leo,heyheyhey";
p = strtok(str,",");
if (p != NULL){
printf("获取到第一个串p=%s\n",p);
}
p = strtok(NULL,",");
printf("获取到第二个串p=%s\n",p);
int i=1;
while()
{
p = strtok(NULL,",");
if (p != NULL)
{
printf("获取到第%d个字符串p = %s\n",i,p);
}
else{
printf("没有字符串");
break;
}
i++;
}
转为小写的函数
strlwr
char str[] = "Hello World";
purts(strlwr(str));
转为大写的函数
strupr
代码例子
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *str = "Leo是个大帅哥哎";
puts(str);
printf("%s\n",str);
char *pstr
pstr = (char *)malloc(128)
if (pstr == NULL){
printf("申请内存失败");
eixt(-1);
}
memset(pstr, '\0', 128)