字符型指针数组

 

#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 = 0;i < n;i++)
    {
        printf("%s\n", cmd[i]);
    }
}

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

    printf("main=%d\n", 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\n", cmd);
12 
13     char **p = NULL;
14 
15     for (p = cmd;p < cmd + 5;p++)
16     {
17         printf("%s\n", *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\n]", str);
13     /* 前面加上^按位异或,可以读取xyz以外的任何字符,遇到一个匹配xyz\n就自动终止 */
14 
15     scanf("%[^xyz]", str);
16     /* 如果没有\n,回车不会当作终止,会当作一个字符 */
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\n", str[0], str[1]);
8 }

 

输出结果:

 


请按任意键继续. . .

 

1 #include <stdio.h>
2 main()
3 {
4     int a;
5     a = 6;
6     printf("sizeof(int)=%ld\n", sizeof(int));
7     printf("sizeof(a)=%ld\n", 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("\n");
15     for (i = 0;i < 3;i++)        /* 输出a表 */
16     {
17         puts(a[i]);
18     }
19     printf("\n");
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("\n");
15     for (i = 0;i < 3;i++)        /* 输出a表 */
16     {
17         puts(a[i]);
18     }
19     printf("\n");
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) != '\0')
 7     {
 8         n++;
 9     }
10     return n;
11 }
12 
13 main()
14 {
15     char str[] = "ABCDEF";
16     int len1, len2;
17     len1 = slength("");
18     len2 = slength(str);
19     printf("len1=%d,len2=%d\n", len1, len2);
20 }

 

例10.2

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

 

 1 #include <stdio.h>
 2 
 3 void scopy(char * s, char * t)
 4 {
 5     int i = 0;
 6     while ((s[i] = t[i]) != '\0')
 7     {
 8         i++;
 9     }
10 }
11 
12 main()
13 {
14     char str1[20], str2[] = "ABCDEFGH";
15     scopy(str1, str2);
16     puts(str1);
17 }

 

例10.3

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

 

 1 #include <stdio.h>
 2 int scomp(char * s1, char * s2)
 3 {
 4     int i = 0;
 5     while (s1[i] == s2[i] && s1[i])
 6     {
 7         i++;
 8     }
 9     return(s1[i] - s2[i]);
10 }
11 
12 main()
13 {
14     char str1[] = "ABC", str2[10];
15     gets(str2);
16     if (scomp(str1, str2) > 0)
17     {
18         printf("str1>str2\n");
19     }
20     else if (scomp(str1, str2) < 0)
21     {
22         printf("str1<str2\n");
23     }
24     else
25     {
26         printf("str1=str2\n");
27     }
28 }

 

10.20

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

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 int ishuiwen(char * s)
 4 {
 5     int a;
 6     int i = 0;
 7     int j = strlen(s) - 1;
 8     while (i < j&&s[i] == s[j])
 9     {
10         i++;
11         j--;
12     }
13     if (i < j)
14     {
15         a = 0;
16     }
17     else
18     {
19         a = 1;
20     }
21     return a;
22 }
23 
24 main()
25 {
26     char str[20];
27     scanf("%s", str);
28     if (ishuiwen(str))
29     {
30         printf("YES\n");
31     }
32     else
33     {
34         printf("NO\n");
35     }
36 }

 

 10.21

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

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 char delchar(char * s, int pos)
 4 {
 5     int i = 0;
 6     char ch = s[pos];
 7     if (pos >= strlen(s) || pos < 0)
 8     {
 9         return 0;
10     }
11     for (i = pos;s[i];i++)
12     {
13         s[i] = s[i + 1];
14     }
15     return ch;
16 }
17 
18 main()
19 {
20     char str[20];
21     int pos;
22     gets(str);
23     scanf("%d", &pos);
24     if (delchar(str, pos))
25     {
26         puts(str);
27     }
28     else
29     {
30         printf("NO\n");
31     }
32 }