目录
第1节 字符的妙用
从键盘读入一个字符并将其输出:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a;
scanf("%c", &a);
printf("你刚才输入的字符是%c\n", a);
system("pause");
return 0;
}
调试结果:
做一个稍微复杂的(整数)计算器:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a, b;
char c;
scanf("%d%c%d", &a, &c, &b);
if (c == '+')
printf("%d\n", a + b);
if (c == '-')
printf("%d\n", a - b);
if (c == '*')
printf("%d\n", a * b);
if (c == '/')
printf("%d\n", a / b);
system("pause");
return 0;
}
调试结果:
第2节 多余的回车键
使用getchar()显示你刚才输入的字符是……:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a;
a = getchar();
printf("你刚才输入的字符是%c\n", a);
system("pause");
return 0;
}
调试结果:
使用getche()显示你刚才输入的字符是……:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a;
a = getche();
printf("你刚才输入的字符是%c", a);
system("pause");
return 0;
}
调试结果:
第3节 字符的本质
循环变量i从1开始一直循环到128:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
for (i = 0; i <= 127; i++)
{
printf("%d %c\n", i, i);
}
system("pause");
return 0;
}
调试结果:
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
8
29
30
31
32
33 !
34 "
35 #
36 $
37 %
38 &
39 '
40 (
41 )
42 *
43 +
44 ,
45 -
46 .
47 /
48 0
49 1
50 2
51 3
52 4
53 5
54 6
55 7
56 8
57 9
58 :
59 ;
60 <
61 =
62 >
63 ?
64 @
65 A
66 B
67 C
68 D
69 E
70 F
71 G
72 H
73 I
74 J
75 K
76 L
77 M
78 N
79 O
80 P
81 Q
82 R
83 S
84 T
85 U
86 V
87 W
88 X
89 Y
90 Z
91 [
92 \
93 ]
94 ^
95 _
96 `
97 a
98 b
99 c
100 d
101 e
102 f
103 g
104 h
105 i
106 j
107 k
108 l
109 m
110 n
111 o
112 p
113 q
114 r
115 s
116 t
117 u
118 v
119 w
120 x
121 y
122 z
123 {
124 |
125 }
126 ~
127
请按任意键继续. . .
第四节 人名怎么存储呢?
输入一行字符串,然后原封不动地将输入的字符串再次输出:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a[10];
scanf("%s", a);
printf("%s\n", a);
system("pause");
return 0;
}
调试结果:
比较两个人的分数高低:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a[101], b[101];
int x, y;
scanf("%s", a);
scanf("%x", &x);
scanf("%s", b);
scanf("%x", &y);
if (x > y)
{
printf("%s", a);
}
else
{
if (x < y)
{
printf("%s", b);
}
else
{
printf("%s和%s的分数相同\n", a, b);
}
}
system("pause");
return 0;
}
调试结果:
第5节 逻辑挑战16:字母的排序
如何对1行字母进行排序:
#define _CRT_SECURE_NO_WARNINGS
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a[101], t;
int len, i, j;
gets(a);
len = strlen(a);
for (i = 0; i <= len - 2; i++)
{
for (j = i + 1; j <= len - 1; j++)
{
if (a[i] > a[j])
{
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
puts(a);
system("pause");
return 0;
}
调试结果:
第6节 逻辑挑战17:字典序
输出两个字符串,将其按字典序输出:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a[101], b[101];
gets(a);
gets(b);
if (strcmp(a, b) <= 0)
{
puts(a);
puts(b);
}
else
{
puts(b);
puts(a);
}
system("pause");
return 0;
}
调试结果:
第7节 多行字符
读入5行字符串,然后将这5行字符串原封不动地输出:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a[5][11];
int i;
for (i = 0; i <= 4; i++)
{
gets(a[i]);
}
for (i = 0; i <= 4; i++)
{
puts(a[i]);
}
system("pause");
return 0;
}
调试结果:
输入5个单词,然后把这些单词按照字典序输出:
#include <string.h>
#include<stdio.h>
#include<stdlib.h>
int main()
{
char a[5][11], t[11];
int i, j;
for (i = 0; i <= 4; i++)
{
gets(a[i]);
}
for (i = 0; i <= 3; i++)
{
for (j = i + 1; j <= 4; j++)
{
if (strcmp(a[i], a[j]) > 0)
{
strcpy(t, a[i]);
strcpy(a[i], a[j]);
strcpy(a[j], t);
}
}
}
for (i = 0; i <= 4; i++)
{
puts(a[i]);
}
system("pause");
return 0;
}
调试结果:
第8节 存储一个迷宫
对二维字符数组进行初始化(存储一个迷宫):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int i;
char a[7][12] =
{
"###########",
"#O # ###",
"# ## ## #",
"# # # #",
"# #### ## #",
"# # ",
"###########",
};
for (i = 0; i < 6; i++)
{
puts(a[i]);
}
system("pause");
return 0;
}
调试结果: