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

字符型指针数组

#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
", sizeof(cmd));

    int i;

    for (i = 0;i < n;i++)
    {
        printf("%s
", cmd[i]);
    }
}

main()
{
    //一个指针数组,每个元素是指针,每个指针保存字符串常量的地址
    char *cmd[] = { "notepad","calc","mspaint","write","mstsc" };

    printf("main=%d
", sizeof(cmd));


    showcmd(cmd, 5);

    system("pause");
}

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

 1 #define _CRT_SECURE_NO_WARNINGS
 2 
 3 #include<stdio.h>
 4 #include<stdlib.h>
 5 
 6 main()
 7 {
 8     //一个指针数组,每个元素是指针,每个指针保存字符串常量的地址
 9     char *cmd[] = { "notepad","calc","mspaint","write","mstsc" };
10 
11     printf("%x
", cmd);
12 
13     char **p = NULL;
14 
15     for (p = cmd;p < cmd + 5;p++)
16     {
17         printf("%s
", *p);
18     }
19     
20     system("pause");
21 }

 

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

 1 #define _CRT_SECURE_NO_WARNINGS
 2 
 3 #include<stdio.h>
 4 #include<stdlib.h>
 5 
 6 char str1[20] = "notepad";
 7 char str2[20] = "tasklist";
 8 char str3[20] = "calc";
 9 
10 //在函数里面改变一个外部变量,就需要变量的地址
11 //如果是整数,函数指向数据的指针存储数据的地址
12 //如果是指针,就需要指向指针的指针的地址
13 //二级指针一般用于改变一个字符串指针的指向,指向不同的字符串
14 
15 void change(char **pp)
16 {
17     *pp = str3;
18 }
19 
20 main()
21 {
22     char *p = str1;
23 
24     change(&p);
25 
26     printf("%s", p);
27 
28     system("pause");
29 }

输入的扫描器:

 1 #define _CRT_SECURE_NO_WARNINGS
 2 
 3 #include<stdio.h>
 4 
 5 main()
 6 {
 7     char str[30] = { 0 };
 8 
 9     //scanf("%[xyz]", str);
10     /* 只接受xyz的输入,有一个不匹配,就停止,即使后面还有xyz */
11 
12     //scanf("%[^xyz
]", str);
13     /* 前面加上^按位异或,可以读取xyz以外的任何字符,遇到一个匹配xyz
就自动终止 */
14 
15     scanf("%[^xyz]", str);
16     /* 如果没有
,回车不会当作终止,会当作一个字符 */
17 
18     //scanf("%[A-Z]", str);
19     /* 只接受A-Z的输入,有一个不匹配,就停止,即使后面还有A-Z */
20 
21     puts(str);
22 }

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

1 #include<stdio.h>
2 
3 main()
4 {
5     char str[10] = "你好";
6     
7     printf("%c%c
", str[0], str[1]);
8 }

输出结果:


请按任意键继续. . .

1 #include <stdio.h>
2 main()
3 {
4     int a;
5     a = 6;
6     printf("sizeof(int)=%ld
", sizeof(int));
7     printf("sizeof(a)=%ld
", sizeof(a));
8 }

输出结果:

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

字符串排序有2种:

1长度strlen

2比较strcmp

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

 1 #define _CRT_SECURE_NO_WARNINGS
 2 
 3 #include <stdio.h>
 4 #include <string.h>
 5 main()
 6 {
 7     int i, j;
 8     char t[20], a[3][20];
 9     for (i = 0;i < 3;i++)        /* 为a表赋值 */
10     {
11         gets(a[i]);
12     }
13 
14     printf("
");
15     for (i = 0;i < 3;i++)        /* 输出a表 */
16     {
17         puts(a[i]);
18     }
19     printf("
");
20 
21     for (i = 0;i < 3 - 1;i++)
22     {
23         for (j = 0;j < 3 - 1 - i;j++)
24         {
25             if (strlen(a[i]) < strlen(a[j]))        /* 求字符串长度函数strlen */
26             {
27                 strcpy(t, a[i]);        /* 字符串复制函数strcpy */
28                 strcpy(a[i], a[j]);
29                 strcpy(a[j], t);
30             }
31         }
32     }
33 
34     for (i = 0;i < 3;i++)        /* 输出a表 */
35     {
36         puts(a[i]);
37     }
38 
39     system("pause");
40 }

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

 1 #define _CRT_SECURE_NO_WARNINGS
 2 
 3 #include <stdio.h>
 4 #include <string.h>
 5 main()
 6 {
 7     int i, j;
 8     char t[20], a[3][20];
 9     for (i = 0;i < 3;i++)        /* 为a表赋值 */
10     {
11         gets(a[i]);
12     }
13 
14     printf("
");
15     for (i = 0;i < 3;i++)        /* 输出a表 */
16     {
17         puts(a[i]);
18     }
19     printf("
");
20 
21     for (i = 0;i < 3 - 1;i++)
22     {
23         for (j = 0;j < 3 - 1 - i;j++)
24         {
25             if (strcmp(a[i], a[j]) < 0)        /* 字符串比较函数strcmp,若前者a[i]<后者a[j],函数值小于0 */
26             {
27                 strcpy(t, a[i]);        /* 字符串复制函数strcpy */
28                 strcpy(a[i], a[j]);
29                 strcpy(a[j], t);
30             }
31         }
32     }
33 
34     for (i = 0;i < 3;i++)        /* 输出a表 */
35     {
36         puts(a[i]);
37     }
38 
39     system("pause");
40 }

例10.1

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

 1 #include <stdio.h>
 2 
 3 int slength(char * s)
 4 {
 5     int n = 0;
 6     while (*(s + n) != '