对于学习常用的字符串处理函数的理解
#include<stdio.h>
#include<string.h>
int main()
{
int i = 0;
int n = 0;
char c[50];
char d[] = {" is very good!"};
char a[50];
//puts输出字符串函数gets输入字符串函数
gets(c);//只能输入或输出一个字符串
puts(c);
//strcpy和strncpy字符串复制函数
//(字符数组1必须足够大,以便连接容纳连接后的新字符串)
//字符数组1必须写成数组名形式字符串2可以是字符数组名也可以是一个
//字符串常量
//字符串1中比字符串2多的部分是字符串1原来的内容
for(i = 0;c[i] != 0;i++)
{
a[i] = c[i];
a[i+1] = '\0';
}
printf("c复制给a\na:%s\n",a);
strcpy(a,c);//把后一个数组复制给前一个
printf("c复制给a\na:%s\n",a);
//strcat字符串联接函数(字符数组1必须足够大,以便连接容纳连接后的新字符串)
for(i = strlen(c),n = 0;d[n]! = '\0';i++,n++)
{
c[i] = d[n];
c[i+1] = '\0';
}
printf("连接后c:%s\n",c);
strcpy(c,a);
printf("d->c:%s\n",strcat(c,d));//连接前两个字符串后面都有'\0'
//连接时将字符串1后的'\0'取消只在新串最后保留'\0'
printf("连接后c:%s\n",c);//strcat直接修改了字符数组C将d直接放置c字符数组里面
printf("连接后d:%s\n",d);//d字符数组不变化
//strncmp(字符串1,字符串2) 比较前n个字符 1<2返回负数 1>2返回正数 1==2返回0
//strcmp字符串比较函数
//字符串1=字符串2函数值为0;
//字符串1>字符串2函数值为一个正整数;
//字符串1<字符串2函数值为一个负整数;
for(i = 0;a[i] == d[i]&&a[i] != '\0';i++);
if(a[i] > d[i])
{
printf("字符串a和d比大小:a > d\n");
}
if(a[i] == d[i])
{
printf("字符串a和d比大小:a = d\n");
}
if(a[i]<d[i])
{
printf("字符串a和d比大小:a < d\n");
}
if(strcmp(a,d) > 0)
printf("字符串a和d比大小:a > d\n");
if(strcmp(a,d) == 0)
printf("字符串a和d比大小:a == d\n");
if(strcmp(a,d) < 0)
printf("字符串a和d比大小:a < d\n");
//strlen测字符串长度的函数
for(i = 0;a[i] != '\0';i++);//求字符串长度
{
printf("字符串a的长度:%d\n",i);
}
printf("字符串a的长度:%d\n",strlen(a));
//strlwr和strupr转换为小写和大写的函数
//(strlwr(strupr)将字符中的大写(小写)转换为小写(大写))
for(i = 0;a[i] != 0;i++)
{
if(a[i] >= 65 && a[i] <= 90)
{
a[i] += 32;
}
else if(a[i] >= 97 && a[i] <= 122)
{
a[i] -= 32;
}
}
printf("a大转小,小转大:%s\n",a);
printf("a转换为大写:%s\n",strupr(a));
printf("a转换为小写:%s\n",strlwr(a));
return 0;
}