Posted on 2007-07-01 02:46
MiweiDev 阅读(612)
评论(0) 编辑 收藏 引用 所属分类:
C / C++Language
3.5 字符常量和字符串常量的区别:
答案:字符常量是一个字符,用单引号括起来,而字符串常量是有零个或一组字符组成,用双引号括起来,存储时自动在后面加上'/0'作为结尾,因此在开辟字符串数组的长度时候应该是字符串长度加一.
3.6 译码:把China 译成密码:要求所有的字符都向后面移动四位:
1
//方法一:适用于用户的直接输入的字符串来译码!
2
#include <stdio.h>
3
4
void main()
5
{
6
char strArray[] = "China";
7
char c = ' ';
8
int i = 0;
9
10
while((c = strArray[i]) != 0)
11
{
12
if(c == 'w')
13
{
14
c = 'a';
15
}
16
else if(c == 'x')
17
{
18
c = 'b';
19
}
20
else if(c == 'y')
21
{
22
c = 'c';
23
}
24
else if(c == 'z')
25
{
26
c = 'd';
27
}
28
else if(c == 'W')
29
{
30
c = 'A';
31
}
32
else if(c == 'X')
33
{
34
c = 'B';
35
}
36
else if(c == 'Y')
37
{
38
c = 'C';
39
}
40
else if(c == 'Z')
41
{
42
c = 'D';
43
}
44
else
45
{
46
c += 4;
47
}
48
printf("%c",c);
49
i++;
50
}
51
printf("\n");
52
}
1
//方法二:对于本题比较直接:
2
#include <stdio.h>
3
4
void main()
5
{
6
char c1 = 'C', c2 = 'h', c3 = 'i', c4 = 'n', c5 = 'a';
7
8
c1 += 4;
9
c2 += 4;
10
c3 += 4;
11
c4 += 4;
12
c5 += 4;
13
printf("%c%c%c%c%c\n",c1,c2,c3,c4,c5);
14
}