1.字符串的定义方式有以下4种,一般用指针最为合适。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
char str[5]={'a','b','c','d','e'}; //第一种
for(i=0;i<sizeof(str)/sizeof(str[0]);i++){
printf("%c",str[i]);
}
putchar('\n');
printf("=====================");
putchar('\n');
char str1[5]="abcde"; //第二种
for(i=0;i<sizeof(str1)/sizeof(str1[0]);i++){
printf("%c",str1[i]);
}
putchar('\n');
printf("=====================");
putchar('\n');
char str2[]="abcde"; //第三种注意
//(当计算数组大小的时候括号里不写会多一个。多一个/0代表的是字符串的结束标志)
//字符串在内存中除了有效字符外,还会自动的在结尾补个/0作为字符串的结束标志。(很重要的特性)
for(i=0;i<sizeof(str2)/sizeof(str2[0]);i++){
printf("%c",str2[i]);
}
putchar('\n');
printf("=====================");
putchar('\n');
char *str3="abcde"; //第四种指针。一般用指针定义。但要注意非法内存
printf("%s\n",str3);
system("pause");
return 0;
}
结果:
abcde
=====================
abcde
=====================
abcde
=====================
abcde
请按任意键继续. . .
2.strlen 和size of的区别
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[]={1,2,3};
char b[]="abc";
char c[128]="abc";
printf("a的有效个数是%d\n",sizeof(a)/sizeof(a[0]));
printf("b的有效个数是%d\n",sizeof(b)/sizeof(b[0]));
printf("c的有效个数是%d\n",sizeof(c)/sizeof(c[0]));
printf("c的有效个数是%d\n",strlen(c));
system("pause");
return 0;
}
结果:
a的有效个数是3
b的有效个数是4
c的有效个数是128
c的有效个数是3
请按任意键继续. . .
总结:我们要计算数组的有效个数的时候不能用sizeof,要用strlen。strlen在计算数组个数的时候遇到’\0’就结束计数。例如"hello ‘\0’ nihao",它只计算到hello遇到\0就结束了。
=========================================================================
3.字符串常用的API
(1) puts() //输出
char * str="月亮很帅!";
//printf("%s\n",str);
puts(str); //puts会自动的加换行
(2)gets() //输入
char *str=NULL;
str=(char *)malloc(128);//用指针必须注意非法内容,去开辟空间。否则会造成段错误
if(str==NULL){
printf("malloc fail\n");
exit(-1);
}
printf("请输入字符!\n");
//scanf("%s",str);
gets(str);
puts(str);
(3)memset //初始化
char *str;
str=(char *)malloc(128);//用指针必须注意非法内容,去开辟空间。否则会造成段错误
if(str==NULL){
printf("malloc fail\n");
exit(-1);
}
memset(str,'\0',128);//参数:1.初始化的对象2.初始化的内容3.初始化的大小
printf("请输入字符!\n");
//scanf("%s",str);
gets(str);
puts(str);
(4)strcpy() //拷贝
函数原型:char *strcpy(char *dest, const char *src);
参数:1.目标地址 2.源地址(1拷到哪里 2 从哪里拷)
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *str="yl hen shuai!";
char *des;
des=(char *)malloc(strlen(str)*2);
memset(des,'\0',strlen(str)*2);
if(des==NULL){
printf("malloc fail\n");
exit(-1);
}
strcpy(des,str);
printf("拷贝成功:%s\n",des);
system("pause");
return 0;
}
结果:
拷贝成功:yl hen shuai!
请按任意键继续. . .
(5)strncpy() //最大长度拷贝
函数原型: char *strncpy(char *dest, const char *src, size_t n);
参数:1.目标地址 2.源地址 3拷贝的最大长度(1拷到哪里 2 从哪里拷 3.拷贝的最大长度)
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *str="yl hen shuai!";
char *des;
des=(char *)malloc(strlen(str)*2);
memset(des,'\0',strlen(str)*2);
if(des==NULL){
printf("malloc fail\n");
exit(-1);
}
strncpy(des,str,5);
printf("拷贝成功:%s\n",des);
system("pause");
return 0;
}
结果:
拷贝成功:yl he
请按任意键继续. . .
(6)strcat //拼接
函数原型:char *strcat(char *dest, const char *src);
参数:1.目标地址 2.源地址(1拼接到哪里 2 从哪里获取)
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *str="yl hen shuai!";
char asd[128]="hello ";
strcat(asd,str);
puts(asd);
system("pause");
return 0;
}
结果:
hello yl hen shuai!
请按任意键继续. . .
(7)strcmp //比较
函数原型:int strcmp(const char *s1, const char *s2);
参数:俩个比较字符的地址
返回值:如果第一个ACSII码比第二个小返回-1,第一个ACSII码比第二个大返回1,相等返回0
说明: strcmp()函数是根据ACSII码的值来比较两个字符串的;strcmp()函数首先将s1字符串的第一个字符值减去s2第一个字符,若差值为零则继续比较下去;若差值不为零,则返回差值。
直到出现不同的字符或遇’\0’为止。
特别注意: strcmp(const char *s1,const char * s2)这里面只能比较字符串,不能比较数字等其他形式的参数。
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *str="kk";
char *asd="aaa";
int ret=strcmp(str,asd);
printf("ret=%d\n",ret);
system("pause");
return 0;
}
结果:
ret=1
请按任意键继续. . .
(8)strchr //查找字符
函数原型: char *strchr(const char *s, int c);
参数:1.目标地址2.找的参数(1.从哪里找2.找什么)
返回值:strchr()和strrchr()函数返回指向匹配字符或NULL的指针
如果找不到字符就返回NULL。
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *str="yl hen shuai";
char *find='s';
char *p;
p=strchr(str,find);
if(p==NULL){
printf("no find\n");
}else{
printf("find\n");
puts(p);
}
system("pause");
return 0;
}
结果:
find
shuai
请按任意键继续. . .
(9)strstr //查找字符串
函数原型:char *strstr(const char *haystack, const char *needle);
参数:1.目标地址2.找的参数(1.从哪里找2.找什么)
返回值:返回指向匹配字符或NULL的指针。如果找不到字符就返回NULL。
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *str="yl hen shuai";
char *find="hen";
char *p;
p=strstr(str,find);
if(p==NULL){
printf("no find\n");
}else{
printf("find\n");
puts(p);
}
system("pause");
return 0;
}
结果:
find
hen shuai
请按任意键继续. . .
(10)strlwr //转为小写
函数原型:extern char *strlwr(char *s);
参数:str为要转换的字符串
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str[128]="YL HEN SHUAI";
strlwr(str);
puts(str);
system("pause");
return 0;
}
结果:
yl hen shuai
请按任意键继续. . .
(11)strupr //转为大写
函数原型:extern char *strupr(char *s);
参数:str为要转换的字符串
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str[128]="yl hen shuai";
strupr(str);
puts(str);
system("pause");
return 0;
}
结果:
YL HEN SHUAI
请按任意键继续. . .
(12)strtok //字符串分割
函数原型:char *strtok(char s[], const char *delim)
参数:1.要分割的字符串 2.以什么来分割
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i=1;
int j;
char str[]="yl,hen,shuai";
char *p;
char *arry[10];
p=strtok(str,",");
if(p!=NULL){
printf("第%d个:%s\n",i,p); //获取第一个串
arry[i-1]=p;
}
while(1){
i++;
p=strtok(NULL,","); //获取第二个串要把目标地址改为NULL,比较奇葩
if(p!=NULL){
printf("第%d个:%s\n",i,p);
arry[i-1]=p;
}
else if(p==NULL){
printf("no zifu\n");
break;
}
}
for(j=0;j<i-1;j++){
printf("cun fang:%s\n",arry[j]);
}
printf("j=%d\n",j);
printf("i=%d\n",i);
system("pause");
return 0;
}
结果:
第1个:yl
第2个:hen
第3个:shuai
no zifu
cun fang:yl
cun fang:hen
cun fang:shuai
j=3
i=4
请按任意键继续. . .