全国计算机等级考试二级教程-C语言程序设计_第10章_字符串

2023-07-29,,

字符型指针数组

#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>
#include<stdlib.h> //参数中,int a[5],对应指向int类型的指针int *
//参数中,指针数组对应char *cmd[]二级指针char **
void showcmd(char **cmd, int n)
{
//此时数组退化成一个指针
printf("show=%d\n", sizeof(cmd)); int i; for (i = ;i < n;i++)
{
printf("%s\n", cmd[i]);
}
} main()
{
//一个指针数组,每个元素是指针,每个指针保存字符串常量的地址
char *cmd[] = { "notepad","calc","mspaint","write","mstsc" }; printf("main=%d\n", sizeof(cmd)); showcmd(cmd, ); system("pause");
}

指针循环,字符型指针数组

 #define _CRT_SECURE_NO_WARNINGS

 #include<stdio.h>
#include<stdlib.h> main()
{
//一个指针数组,每个元素是指针,每个指针保存字符串常量的地址
char *cmd[] = { "notepad","calc","mspaint","write","mstsc" }; printf("%x\n", cmd); char **p = NULL; for (p = cmd;p < cmd + ;p++)
{
printf("%s\n", *p);
} system("pause");
}

//在函数里面改变一个外部变量,就需要变量的地址
//如果是整数,函数指向数据的指针存储数据的地址
//如果是指针,就需要指向指针的指针的地址
//二级指针一般用于改变一个字符串指针的指向,指向不同的字符串

 #define _CRT_SECURE_NO_WARNINGS

 #include<stdio.h>
#include<stdlib.h> char str1[] = "notepad";
char str2[] = "tasklist";
char str3[] = "calc"; //在函数里面改变一个外部变量,就需要变量的地址
//如果是整数,函数指向数据的指针存储数据的地址
//如果是指针,就需要指向指针的指针的地址
//二级指针一般用于改变一个字符串指针的指向,指向不同的字符串 void change(char **pp)
{
*pp = str3;
} main()
{
char *p = str1; change(&p); printf("%s", p); system("pause");
}

输入的扫描器:

 #define _CRT_SECURE_NO_WARNINGS

 #include<stdio.h>

 main()
{
char str[] = { }; //scanf("%[xyz]", str);
/* 只接受xyz的输入,有一个不匹配,就停止,即使后面还有xyz */ //scanf("%[^xyz\n]", str);
/* 前面加上^按位异或,可以读取xyz以外的任何字符,遇到一个匹配xyz\n就自动终止 */ scanf("%[^xyz]", str);
/* 如果没有\n,回车不会当作终止,会当作一个字符 */ //scanf("%[A-Z]", str);
/* 只接受A-Z的输入,有一个不匹配,就停止,即使后面还有A-Z */ puts(str);
}

char 字符串可以显示汉字,字符要连在一起%c%c

 #include<stdio.h>

 main()
{
char str[] = "你好"; printf("%c%c\n", str[], str[]);
}

输出结果:


请按任意键继续. . .

 #include <stdio.h>
main()
{
int a;
a = ;
printf("sizeof(int)=%ld\n", sizeof(int));
printf("sizeof(a)=%ld\n", sizeof(a));
}

输出结果:

sizeof(int)=4
sizeof(a)=4
请按任意键继续. . .

字符串排序有2种:

1长度strlen

2比较strcmp

读入一个3行的二维字符串数组,使用求字符串长度函数strlen,进行从大到小排序,使用冒泡排序。

 #define _CRT_SECURE_NO_WARNINGS

 #include <stdio.h>
#include <string.h>
main()
{
int i, j;
char t[], a[][];
for (i = ;i < ;i++) /* 为a表赋值 */
{
gets(a[i]);
} printf("\n");
for (i = ;i < ;i++) /* 输出a表 */
{
puts(a[i]);
}
printf("\n"); for (i = ;i < - ;i++)
{
for (j = ;j < - - i;j++)
{
if (strlen(a[i]) < strlen(a[j])) /* 求字符串长度函数strlen */
{
strcpy(t, a[i]); /* 字符串复制函数strcpy */
strcpy(a[i], a[j]);
strcpy(a[j], t);
}
}
} for (i = ;i < ;i++) /* 输出a表 */
{
puts(a[i]);
} system("pause");
}

读入一个3行的二维字符串数组,使用字符串比较函数strcmp,进行从大到小排序,使用冒泡排序。

 #define _CRT_SECURE_NO_WARNINGS

 #include <stdio.h>
#include <string.h>
main()
{
int i, j;
char t[], a[][];
for (i = ;i < ;i++) /* 为a表赋值 */
{
gets(a[i]);
} printf("\n");
for (i = ;i < ;i++) /* 输出a表 */
{
puts(a[i]);
}
printf("\n"); for (i = ;i < - ;i++)
{
for (j = ;j < - - i;j++)
{
if (strcmp(a[i], a[j]) < ) /* 字符串比较函数strcmp,若前者a[i]<后者a[j],函数值小于0 */
{
strcpy(t, a[i]); /* 字符串复制函数strcpy */
strcpy(a[i], a[j]);
strcpy(a[j], t);
}
}
} for (i = ;i < ;i++) /* 输出a表 */
{
puts(a[i]);
} system("pause");
}

例10.1

编写函数 slength(char * s),函数返回指针 s 所指字符串的长度,即相当于库函数 strlen 的功能。

 #include <stdio.h>

 int slength(char * s)
{
int n = ;
while (*(s + n) != '\0')
{
n++;
}
return n;
} main()
{
char str[] = "ABCDEF";
int len1, len2;
len1 = slength("");
len2 = slength(str);
printf("len1=%d,len2=%d\n", len1, len2);
}

例10.2

编写函数 scopy(char * s, char * t),将指针 t 所指的字符串复制到指针 s 所指的存储空间中。

 #include <stdio.h>

 void scopy(char * s, char * t)
{
int i = ;
while ((s[i] = t[i]) != '\0')
{
i++;
}
} main()
{
char str1[], str2[] = "ABCDEFGH";
scopy(str1, str2);
puts(str1);
}

例10.3

编写函数 scomp(char * s1, char * s2),将两个字符串 s1 和 s2 进行比较,若串 s1>串 s2,函数返回值为正数;若串 s1=串 s2,函数返回值为0;若串 s1<串 s2,函数返回值为负数。即相当于库函数strcmp的功能。

 #include <stdio.h>
int scomp(char * s1, char * s2)
{
int i = ;
while (s1[i] == s2[i] && s1[i])
{
i++;
}
return(s1[i] - s2[i]);
} main()
{
char str1[] = "ABC", str2[];
gets(str2);
if (scomp(str1, str2) > )
{
printf("str1>str2\n");
}
else if (scomp(str1, str2) < )
{
printf("str1<str2\n");
}
else
{
printf("str1=str2\n");
}
}

10.20

请编写函数,判断一字符串是否是回文。若是回文函数返回值为1;否则返回值为0。回文是顺读和倒读都一样的字符串。

 #include <stdio.h>
#include <string.h>
int ishuiwen(char * s)
{
int a;
int i = ;
int j = strlen(s) - ;
while (i < j&&s[i] == s[j])
{
i++;
j--;
}
if (i < j)
{
a = ;
}
else
{
a = ;
}
return a;
} main()
{
char str[];
scanf("%s", str);
if (ishuiwen(str))
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}

10.21

请编写函数,删除字符串中指定位置(下标)上的字符。删除成功函数返回被删字符;否则返回空置。

 #include <stdio.h>
#include <string.h>
char delchar(char * s, int pos)
{
int i = ;
char ch = s[pos];
if (pos >= strlen(s) || pos < )
{
return ;
}
for (i = pos;s[i];i++)
{
s[i] = s[i + ];
}
return ch;
} main()
{
char str[];
int pos;
gets(str);
scanf("%d", &pos);
if (delchar(str, pos))
{
puts(str);
}
else
{
printf("NO\n");
}
}

全国计算机等级考试二级教程-C语言程序设计_第10章_字符串的相关教程结束。

《全国计算机等级考试二级教程-C语言程序设计_第10章_字符串.doc》

下载本文的Word格式文档,以方便收藏与打印。