------------有关string 的字符函数背后的故事----------
包含了strlen \ strcpy \ strcat \ strset \ strlwr \ strupr \ strrev \ strchr \strrchr \ strcmp \ strnset
1.strlen(测字符串长度)
#include<string.h>
int main ()
{
char str[] ="hello";
<span style="color:#cc0000;">int i = 0;
while(str[i] != 0)
{
i ++;
}</span>
printf("%d\n",strlen(str));//字符函数
printf("%d\n",i); //背后的实现红色部分
return 0;
}
注意和sizeof的区别;
2.strcpy(复制字符串)#include<string.h>
int main ()
{
char str1[6] ="hello";
char str2[20] ;
int len = strlen(str1);
<span style="color:#cc0000;">int i = 0;
while(i < len)
{
str2[i] =str1[i];
i ++;
}
str2[i]= 0</span>;
printf("%s\n",strcpy(str2,str1));//字符函数自己的功能
printf("%s\n",str2); //背后实现情况红色部分
return 0;
}
注:把字符串中的‘\0’都复制过来了
3.strcat(连接两个字符串)#include<string.h>
int main ()
{
char str1[20] ="hello";
char str2[20] ="world";
int len = strlen(str1);
<span style="color:#cc0000;">int i = 0;
int j = 0;
while(str1[i] != 0)
{
i ++;
}
while(str2[j] !=0)
{
str2[i]=str1[j];
j ++;
i ++;
}</span>
printf("%s\n",strcat(str2,str1));//字符函数自己的功能
printf("%s\n",str2); //背后实现情况红色部分
return 0;
}
注:把源字符串的‘\0’都连接过来
4.strset(重置字符串)<span style="font-size:32px;">#include<string.h>
int main ()
{
char str[20] ="hello";
char ch = '*';
int i = 0;
<span style="color:#cc0000;">while(str[i] !=0)
{
str[i] = ch;
i ++;
}</span>
printf("%s\n",strset(str,ch));//字符函数自己的功能
printf("%s\n",str);//背后实现情况红色部分
return 0;
}
注:不替换‘\0’4.Strlwr\strupr(大小写的转换)
#include<string.h>
int main ()
{
char str[20] ="Hello";
int i = 0;
<span style="color:#cc0000;">while(str[i] !=0)
{
if(str[i] >= 'A'&& str[i]<='Z')
{
str[i]=str[i] +32;
}
else if(str[i] >= 'a'&& str[i]<='z')
{
str [i]= str[i]-32;
}
i ++;
}</span>
printf("%s\n",str);
return 0;
}
5.strrev(翻转字符串)
#include<string.h>
int main ()
{
char str[20] ="Hello";
int i = 0;
int j = strlen(str)-1;
char temp ;
<span style="color:#cc0000;">while(i<j)
{
temp = str[j];
str[j] = str[i];
str[i] = temp;
i ++;
j--;
}</span>
printf("%s\n",str);
return 0;
}
6.strchr(从字符串左边开始中找指定字符)
#include<string.h>
int main ()
{
char str[20] ="Hello";
char key = 'l';
<span style="color:#ff0000;">int i= 0;
while(str[i] != 0)
{
if(str[i] == key)
{
printf("找到了!%s",str);break;
}
i ++;
}</span>
return 0;
}
7.strrchr(从字符串右边开始中找指定字符)int main ()
{
char str[20] ="Hello";
char key = 'l';
int count = 0;//用来计算第几次找到的
<span style="color:#cc0000;">for(int i = strlen(str);i >= 0;i --)
{
count ++;
if(str[i] == key)
{
printf("找到了!%s\n",str);
break;
}
printf("%d\n",count);
}</span>
return 0;
}
8.strcmp(比较两个字符串;如果又不一样返回ASCII大的那个字符串)
自身用法;
#include<string.h>
int main ()
{
char str1[20] ="Hello";
char str2[20] = "Helq";
printf("%d",strcmp(str1,str2));
return 0;
}
背后实现情况:
int main ()
{
char str1[20] ="Hello";
char str2[20] = "Helq";
<span style="color:#cc0000;">int i = 0;
int rs = 0;
while(str1[i] != 0 ||str2[i] != 0)
{
if(str1[i] != str2[i])
{
rs=str1[i] -str2[i] ;
printf("%d\n",rs); //108-112
break;
}
i ++;
}</span>
return 0;
}
9.strnset(清理你指定数目的字符串)
字符函数的用法
<span style="font-size:14px;">#include<string.h>
int main ()
{
char str[20] ="Hello";
printf("%s",strnset(str ,'*',2));
return 0;
}
</span>
背后实现的代码
<span style="font-size:14px;">int main ()
{
char str[20] ="Hello";
int i = 0;
int k = 2;//重置str中k个字符
<span style="color:#cc0000;">char ch = '*';//准备重置成ch
while(str[i] != 0 )
{
if(i <= k)
{
str[i] = ch;
}
i ++;
}</span>
printf("%s",str);
return 0;
}
</span>
这是这两个的运行结果。
以后就不用那么辛苦的 记住那些字符函数的功能了,你自己可以实现它的功能!