九. 字符数组和字符串
9.1 字符数组
字符数组是元素的数据类型为字符类型的数组
char c[10],ch[3][4];
字符数组的初始化
逐个字符赋值
char ch[5] = { 'H','e','l','l','o' };
char ch[5] = {'a','b','c'};
例如:
char ch[6]={"hello"};
char ch[6]="hello";
char ch[] = "hello";
进阶例子:
9.2 字符串数组
C语言中没有字符串常量,用字符数组处理字符串,字符串结束表示:‘\0’
char fruit[][7] = {"apple","grape","pear","orange","peach"};
题目:输入一个字符串,将字符串逆序输出。
方法:倒序,前后位置调换(可以使用strlen计算
输入字符长度)
法一:倒序
#include<stdio.h>
#include<string.h>
#define N 20
int main()
{
char str[N];
int m,a,b;
printf("please input str:\n");
gets(str);
m=strlen(str);
for(a=m-1;a>=0;a--)
{
putchar(str[a]);
}
return 0;
}
法二:前后位置调换
#include<stdio.h>
#include<string.h>
#define N 20
int main()
{
char str[N];
int m,a,b,t;
printf("please input str:\n");
gets(str);
m=strlen(str);
a=0,b=m-1;
while(a<b)
{
t=str[a];
str[a]=str[b];
str[b]=t;
a++;
b--;
}
puts(str);
printf("%d",m);
return 0;
}
9.3 字符串函数
C语言库中实现了很多字符串处理的函数
#include <string.h>
几个常见的字符串处理函数
1. strlen ,求字符串长度函数
2. strcpy,字符串拷贝函数
3. strcat,字符串连接函数
4. strcmp,字符串比较函数
9.3.1 strlen
字符串长度函数strlen
- 格式:strlen(字符数组)
- 功能:计算字符串长度
- 返回值:返回字符串实际长度,不包括'\0'在内
- \xhh 表示十六进制数代表的符号
- \ddd 表示八进制的
#include<stdio.h>
#include<string.h>
#define N 20
int main()
{
char a[]="hello";
int t;
t=strlen(a);
printf("%d\n",t);
return 0;
}
9.3.2 strcpy
字符串拷贝函数 strcpy
- 格式:strcpy(字符串1,字符串2);
- 功能:将字符串2拷贝到字符串1中
- 返回值:返回字符数组1的首地址
- 说明:
1.字符数组1必须足够大
2.拷贝时 ‘\0’ 一同拷贝
#include<stdio.h>
#include<string.h>
#define N 20
int main()
{
char a[]="hello";
char b[N];
strcpy(b,a);
puts(a);
puts(b);
return 0;
}
9.3.3 strcat
字符串连接函数strcat
- 格式:stracat(字符数组1,字符数组2)
- 功能:把字符数组2连接到字符数组1后面
- 返回值:返回字符数组1的首地址
- 说明:
- 字符数组1必须足够大
- 连接前,两串均以 '\0' 结束,连接后,串 1的 '\0' 取消,新串最后加 ‘\0’
#include<stdio.h>
#include<string.h>
#define N 20
int main()
{
char a[]="hello";
char b[N]="world,";
strcat(b,a);
puts(a);
puts(b);
return 0;
}
9.3.4 strcmp
字符串比较函数 strcmp
- 格式:strcmp(字符串1,字符串2)
- 功能:比较两个字符串
- 比较规则:对两串从左向右逐个字符比较(ASCII),直到遇到不同字符或 '\0'为止
- 返回值:返回int型整数
1.若字符串1 < 字符串2,返回负整数
2.若字符串1 > 字符串2,返回正整数
3.若字符串1 = 字符串2,返回零
#include<stdio.h>
#include<string.h>
#define N 20
int main()
{
char a[]="hello,world";
char b[N]="hello world";
int t;
t=strcmp(a,b);
puts(a);
puts(b);
printf("%d,%d\n",',',' ');
printf("%d\n",t);
return 0;
}
9.3.5 字符串扩展用法
- strncpy(p,p1,n) ,复制指定长度字符串
#include<stdio.h>
#include<string.h>
#define N 20
int main()
{
char a[]="hello,world";
char b[N]="time";
strncpy(a,b,3);
puts(a);
return 0;
}
- strncat(p,p1,n),附加指定长度字符串
#include<stdio.h>
#include<string.h>
#define N 20
int main()
{
char a[]="hello,world";
char b[N]="time";
strncat(a,b,3);
puts(a);
return 0;
}
- strcasecmp(p.p1), 忽略大小写比较字符串
#include<stdio.h>
#include<string.h>
#define N 20
int main()
{
int t=0;
char a[]="Hello,world";
char b[N]="hello";
t=strcasecmp(a,b);
printf("%d\n",t);
return 0;
}
- strncmp(p,p1,n),比较指定长度字符串
#include<stdio.h>
#include<string.h>
#define N 20
int main()
{
int t;
char a[]="hello,world";
char b[N]="hello,time";
t=strncmp(a,b,5);
printf("%d\n",t);
return 0;
}
- strchr(p,c),在字符串中查找指定字符
- strstr(p,p1)查找字符串
1. isalpha()检查是否为字母字符
2. isupper()检查是否为大写字母字符
3. islower()检查是否为小写字母字符
4. isdigit()检查是否为数字
#include <ctype.h>