字符串函数
1、学习目标
- 字符串函数的用法-熟悉
- 总结与思考
2、字符串函数
- C库中实现了很多字符串处理函数
#include <string.h>
- 几个常见的字符串处理函数
- 求字符串长度的函数strlen
- 字符串拷贝函数strcpy
- 字符串连接函数strcat
- 字符串比较函数strcmp
3、字符串长度函数strlen
-
格式:strlen(字符数组)
-
功能:计算字符串长度。
-
返回值:返回字符串实际长度,不包括’\0’在内。
-
\xhh表示十六进制数代表的符号
-
\ddd表示八进制的
-
示例:
验证strlen()函数的功能
#include <stdio.h> #include <string.h> int main() { char s1[10]={'A','0','B','\0','C'}; int n; n = strlen(s1); printf("n=%d\n", n); puts(s1); return 0; }
运行结果:"\0"为结束标志 在strlen()函数中"\0"不包含在内,不计入长度计算。
string$ ./app n=3 A0B
strlen()函数和sizeof()函数的对比
#include <stdio.h> #include <string.h> int main() { // char s1[10]={'A','0','B','\0','C'}; // int n; // n = strlen(s1); char s1[] = "makeru"; printf("%d\n", strlen(s1));//字符串长度函数输出长度 printf("%d\n",sizeof(s1)/sizeof(char)); //用sizeof()函数来计算字符串长度 return 0; }
运行结果:
string$ ./app 6//(strlen函数不计算'\0') 7//(sizeof函数计算'\0')
特殊转义示例如下
#include <stdio.h> #include <string.h> int main() { char s1[] = "\tab\nc\vd\\e"; char s2[] = "\x69\141"; printf("%d\n", strlen(s1)); printf("%d\n",sizeof(s1)/sizeof(char)); printf("\n%d\n", strlen(s2)); printf("%d\n",sizeof(s2)/sizeof(char)); puts(s2); return 0; }
运行结果:
string$ ./app 9 //遇到转义的时候斜杠不算在内 10 2 3 ia //十六进制
4、字符串拷贝函数strcpy
-
格式:strcpy(字符数组1,字符串2)//(目标,原串)
-
功能:将字符串2拷贝到字符数组1中去
-
返回值:返回字符数组1的首地址
-
说明:
- 字符数组1必须足够大
- 拷贝时’\0’一同拷贝
-
示例:
将字符串拷贝到字符数组中
#include <stdio.h> #include <string.h> #define N 30 int main() { char src[] = "makeru"; char dest[N]; strcpy(dest,src); puts(src); puts(dest); return 0; }
运行结果:
string$ ./app makeru makeru
5、其他字符串函数
-
strncpy(p,p1,n)复制指定长度的字符串到字符数组中
-
示例:
#include <stdio.h> #include <string.h> #define N 30 int main() { char src[] = "makeru"; char dest[N] = ".com.cn"; strncpy(dest,src,4);//复制长度为4的字符串到字符数组中 puts(src); puts(dest); return 0; }
运行结果:
string$ ./app makeru make.cn
-
-
strncat(p,p1,n)附加指定长度字符串,字符串连接
-
示例:
#include <stdio.h> #include <string.h> #define N 30 int main() { char str[] = "makeru"; char dest[N] = ".com.cn"; strncat(dest,str,4);//附加 puts(str); puts(dest); return 0; }
运行结果:
string$ ./app makeru .com.cnmake
-
-
strcasecmp忽略大小写比较字符串
-
示例
#include <stdio.h> #include <string.h> int main() { char s1[] = "QUIT";//ASCII值不同 char s2[] = "quit"; printf("%d\n", strncmp(s1, s2, 4)); printf("%d\n", strcasecmp(s1, s2));//忽略大小写 return 0; }
运行结果:
$ ./app -32 0
-
-
strncmp(p,p1,n)比较指定长度字符串,字符串大小的比较是以ASCII码表上的顺序来决定的。
返回值:s1<s2,返回:负数;s1=s2,返回:0;s1>s2,返回:正数
-
示例
strcmp()比较字符串长度函数
#include <stdio.h> #include <string.h> #define N 30 int main() { char s1[] = "quit"; char s2[] = "quit\n"; printf("%d\n", strcmp(s1, s2)); return 0; }
运行结果:\n的ASCII码值是10
string$ ./app -10
strncmp()比较指定长度的字符串函数
#include <stdio.h> #include <string.h> #define N 30 int main() { char s1[] = "quit"; char s2[] = "quit\n"; printf("%d\n", strncmp(s1,s2,2)); return 0; }
运行结果:0 是s1和s2相等
string$ ./app 0
-
-
strchr(p,c)在字符串中查找指定字符
-
示例
#include <stdio.h> #include <string.h> int main() { char s1[] = "asdSfgh"; int ch; ch = 'S'; printf("%p %p \n", s1, strchr(s1, ch));//查找第一次 printf("%p %p \n", s1, strrchr(s1, ch)); //查找最后一次出现的,反向查找 return 0; }
运行结果:
/string$ ./app $ ./app 0x7ffefb2c29a0 0x7ffefb2c29a3 0x7ffefb2c29a0 0x7ffefb2c29a6
#include <stdio.h> #include <string.h> int main() { char s1[] = "asdSfgSh"; int ch; ch = 'S'; printf("%d\n", strchr(s1, ch)-s1); printf("%d\n", strrchr(s1, ch)-s1); return 0; }
运行结果:
string$ ./app 3 6
-
-
strstr(p,p1)查找字符串(返回查找内容在数组中的地址)
-
示例
#include <stdio.h> #include <string.h> int main() { char s1[] = "how are you"; char subs[] = "are"; printf("%d\n", strstr(s1,subs)-s1); return 0; }
运行结果:由于输出是%d所以输出是该地址的位置
$ ./app 4
-
6、检查字符串函数,头文件为#include(ctype.h)
-
isalpha()检查是否为字母字符
-
isupper()检查是否为大写字母字符
-
islower()检查是否为小写字母字符
-
isdigit()检查是否为数字
-
示例
#include <stdio.h> #include <ctype.h> int main() { int ch; while((ch = getchar()) != EOF){ if(isalpha(ch)){ if(isupper(ch)){ printf("Upper:%c\n",ch); } if(islower(ch)){ printf("Lower:%c\n",ch); } } if(isdigit(ch)) printf("Digit:%d %c\n",ch -'0',ch); //定义字符型ch,由%d输出需要通过ch-'0'才可以得到该数字,不然会认为输入该数字是字符。 // putchar(ch); } return 0; }
运行结果:
/string$ ./app A Upper:A s Lower:s 3 Digit:3 3
大小写转换
#include <stdio.h> #include <ctype.h> int main() { int ch; while((ch = getchar()) != EOF){ if(isalpha(ch)){ if(isupper(ch)){ ch = tolower(ch); } else{ ch = toupper(ch); } printf("%c\n", ch); } } return 0; }
运行结果:
string$ ./app ASD a s d sdf S D F
-