内容正文:
5.3节:字符数组的定义和使用
一、单项选择题
1.执行 char x[] = “STRING”; x[0]=0; x[1]=‘\0’; x[2]=‘0’;后,strlen(x)和 sizeof(x)的值分别是( )
A. 0, 7 B. 1, 7
C. 0, 8 D. 1, 8
【答案】A
2.以下正确的字符数组定义与初始化是( )
A. char s1[5] = {“abcde”};
B. char s2[3] = “ab”;
C. char s3[ ] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’};
D. char s4[6]; s4 = “Hello”;
【答案】B A错误,“abcde”需要6个空间(含\0)。B正确,“ab”需要3个空间,数组大小3刚好。C合法,但用字符列表初始化不会自动添加 \0,因此不能作为“字符串”被 puts, strlen等函数安全使用。D错误,数组名不能直接赋值。
3.若有 char a[10]=“0\0a0”;,strlen(a)、sizeof(a)以及 a[2]的值分别是( )
A. 1, 10, ‘a’
B. 1, 10, ‘\0’
C. 3, 10, ‘a’
D. 4, 10, ‘a’
【答案】A 初始化后 a[0]=‘0’, a[1]=‘\0’。strlen遇 a[1]结束,长度为1。sizeof是数组总大小10。a[2]是第三个元素,初始化为 ‘a’。
4.以下能正确输出字符数组 str中字符串长度(不使用 strlen)的循环是( )
A. for(i=0; str[i]!=‘\0’; i++);
B. for(i=0; str[i]!=‘0’; i++);
C. for(i=0; str[i]!=NULL; i++);
D. for(i=0; i< sizeof(str); i++);
【答案】A 字符串以 \0(ASCII码0)结束。B错在 ‘0’ 是字符零,ASCII码48。C中 NULL通常用于指针,虽然值可能是0,但不规范。D使用 sizeof会计算数组总大小,而非字符串长度。
5.以下程序段的输出是( )
char s[80];
strcpy(s, “hello”);
if(strcmp(s, “hello”) == 0)
printf(“=“);
else printf(“!=”);
A. = B. !=
C. 可能为 =,也可能为 != D. 程序编译错误
【答案】A strcpy后 s中的内容为 “hello”,与字符串常量 “hello” 用 strcmp比较,结果相等,返回0,输出 “=”。
6.执行以下语句后,变量 i的值是( )
char str[] = “-123,456”; int i; sscanf(str, “%d”, &i);
A. -123 B. 123
C. -123456 D. 0
【答案】A
7.以下代码执行后,c的值是( )
char a[20] = “Hello\0Hi”; char b[] = “World”; int c = strcmp(a, b);
A. 一个负数(因为 ‘H’ < ‘W’)
B. 一个正数
C. 0
D. 不确定
【答案】A a中实际有效的字符串是 “Hello”,因为第一个 \0就结束了。比较 “Hello” 和 “World”,第一个字符 ‘H’ (72) < ‘W’ (87),所以 strcmp返回负数。
8.以下程序段的输出是( )
char s[] = “abcd”;
int i=0;
do putchar(s[i++]); while(s[i] != ‘\0’);
putchar(‘#’);
A. abcd# B. abcd
C. abc# D. 程序陷入死循环
【答案】A
二、程序运行结果题
9.执行以下程序的输出结果是( )
#include <stdio.h>
#include <string.h>
main()
{
char a[20] = "abc\0def";
printf("%d, %d", strlen(a), sizeof(a));
}
【答案】3, 20
10.执行以下程序的输出结果是( )
#include <stdio.h>
main()
{
char s[] = "\\\'1234\0abcd";
int i;
for(i=0; s[i]!='\0'; i++)
{
if(s[i] == '\'')
putchar('*');
else putchar(s[i]);
}
}
【答案】\*1234
11.执行以下程序的输出结果是( )
#include <stdio.h>
#include <string.h>
main()
{
char str1[10] = "China", str2[] = "Beijing";
strcpy(str1, str2);
printf("%s", str1);
}
【答案】Beijing
12.执行以下程序的输出结果是( )
#include <stdio.h>
#include <string.h>
main()
{
char s[10] = "12345";
s[2] = '\0';
printf("%d", strlen(s));
}
【答案】2
13.执行以下程序的输出结果是( )
#include <stdio.h>
#include <string.h>
main()
{
char a[] = "language", b[] = "program";
int i;
for(i=0; a[i]!='\0'&& b[i]!='\0'; i++)
if(a[i] == b[i])
printf("%c", a[i]);
}
【答案】ga
14.执行以下程序的输出结果是( )
#include <stdio.h>
main()
{
char str[] = "SSSWLIA", c;
int k;
for(k=2; (c=str[k]) != '\0'; k++)
{
switch(c)
{
case 'I': ++k; break;
case 'L': continue;
default: putchar(c); continue;
}
putchar('*');
}
}
【答案】SW*
15.执行以下程序的输出结果是( )
#include <stdio.h>
#include <string.h>
main()
{
char s1[20] = "ABCD", s2[] = "EFG", s3[] = "HIJ";
strcat(strcat(s1, s2), s3);
printf("%s", s1);
}
【答案】ABCDEFGHIJ
16.执行以下程序的输出结果是( )
#include <stdio.h>
#include <string.h>
main()
{
char p[] = "abcdefghijklmnopq";
int i=0;
while(p[i] != '\0')
{
if(p[i] >= 'a' && p[i] <= 'z')
p[i] = p[i]-32;
i += 2;
}
puts(p);
}
【答案】AbCdEfGhIjKlMnOpQ
17.执行以下程序的输出结果是( )
#include <stdio.h>
main()
{
char s[] = "12134211";
int v[4]={0}, i;
for(i=0; s[i]; i++) {
switch(s[i]) {
case '1': v[0]++;
case '2': v[1]++;
case '3': v[2]++;
default: v[3]++;
}
}
for(i=0; i<4; i++)
printf("%d,", v[i]);
}
【答案】4,6,7,8,
18.执行以下程序的输出结果是( )
#include <stdio.h>
main()
{
char str[] = "welcome2018";
int i, n=0;
for(i=0; str[i]!='\0'; i++)
if(str[i] >= '0' && str[i] <= '9')
n = n*10 + (str[i] - '0');
printf("%d", n);
}
【答案】2018
19.执行以下程序的输出结果是( )
#include <stdio.h>
#include <string.h>
main()
{
char s[] = "012xy\08s34f4w2";
int i, n=0;
for(i=0; s[i]!=0; i++)
if(s[i] >= '0' && s[i] <= '9')
n++;
printf("%d", n);
}
【答案】3
20.执行以下程序的输出结果是( )
#include <stdio.h>
#include <string.h>
main()
{
char s[20] = "One", t[20] = "Two";
if(strcmp(s, t) < 0)
strcpy(s, t);
else strcat(s, t);
printf("%s,%s", s, t);
}
【答案】Two,Two
三、编程题
1.定义一个初始化为"Programming"的字符数组,通过循环逐个引用并输出每个字符。
【答案】
#include <stdio.h>
main()
{
// 对应“字符数组的引用”
char str[] = "Programming";
int i = 0;
while(str[i] != '\0') { // 逐个引用,直到结束符
putchar(str[i]); // 输出单个字符
i++;
}
}
2.提示用户输入一个单词,使用getchar()逐个读入并存入数组,再逐个转换为大写后输出。
【答案】
#include <stdio.h>
main()
{
char word[6]; // 存储最多5个字符+1个‘\0‘
int i = 0,j;
char ch;
printf("请输入一个长度不超过5的单词: ");
while((ch = getchar()) != '
' && i < 5) { // 逐个读入
word[i++] = ch;
}
word[i] = '\0'; // 手动添加结束符
printf("转换后: ");
for(j=0; j<i; j++) {
if(word[j] >= 'a' && word[j] <='z')
putchar(word[j] - 32); // 小写转大写
else
putchar(word[j]);
}
}
3.编写密码验证程序,用strcmp()比较用户输入与预设密码。
【答案】
#include <stdio.h>
#include <string.h>
main()
{
char password[] = "Secure123";
char input[20];
printf("请输入密码: ");
gets(input);
if(strcmp(input, password) == 0)// 字符串比较
printf("Access Granted
");
else
printf("Access Denied
");
}
4.用字符数组存储图案"*********",通过控制输出次数和空格,输出一个3行的三角形。
【答案】
#include <stdio.h>
#include <string.h>
main()
{
char star[] = "*********";
int i, j;
for(i=0; i<3; i++)
{
// 输出空格
for(j=0; j<3-i-1; j++)
printf(" ");
// 输出星号
for(j=0; j<2*i+1; j++)
putchar(star[j]);
printf("
");
}
}
原创精品资源学科网独家享有版权,侵权必究!
学科网(北京)股份有限公司
$
5.3节:字符数组的定义和使用
一、单项选择题
1.执行 char x[] = “STRING”; x[0]=0; x[1]=‘\0’; x[2]=‘0’;后,strlen(x)和 sizeof(x)的值分别是( )
A. 0, 7 B. 1, 7
C. 0, 8 D. 1, 8
2.以下正确的字符数组定义与初始化是()
A. char s1[5] = {“abcde”};
B. char s2[3] = “ab”;
C. char s3[ ] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’};
D. char s4[6]; s4 = “Hello”;
3.若有 char a[10]=“0\0a0”;,strlen(a)、sizeof(a)以及 a[2]的值分别是()
A. 1, 10, ‘a’
B. 1, 10, ‘\0’
C. 3, 10, ‘a’
D. 4, 10, ‘a’
4.以下能正确输出字符数组 str中字符串长度(不使用 strlen)的循环是()
A. for(i=0; str[i]!=‘\0’; i++);
B. for(i=0; str[i]!=‘0’; i++);
C. for(i=0; str[i]!=NULL; i++);
D. for(i=0; i< sizeof(str); i++);
5.以下程序段的输出是()
char s[80];
strcpy(s, “hello”);
if(strcmp(s, “hello”) == 0)
printf(“=“);
else printf(“!=”);
A. = B. !=
C. 可能为 =,也可能为 != D. 程序编译错误
6.执行以下语句后,变量 i的值是()
char str[] = “-123,456”; int i; sscanf(str, “%d”, &i);
A. -123 B. 123
C. -123456 D. 0
7.以下代码执行后,c的值是()
char a[20] = “Hello\0Hi”; char b[] = “World”; int c = strcmp(a, b);
A. 一个负数(因为 ‘H’ < ‘W’)
B. 一个正数
C. 0
D. 不确定
8.以下程序段的输出是()
char s[] = “abcd”;
int i=0;
do putchar(s[i++]); while(s[i] != ‘\0’);
putchar(‘#’);
A. abcd# B. abcd
C. abc# D. 程序陷入死循环
二、程序运行结果题
9.执行以下程序的输出结果是()
#include <stdio.h>
#include <string.h>
main()
{
char a[20] = "abc\0def";
printf("%d, %d", strlen(a), sizeof(a));
}
10.执行以下程序的输出结果是()
#include <stdio.h>
main()
{
char s[] = "\\\'1234\0abcd";
int i;
for(i=0; s[i]!='\0'; i++)
{
if(s[i] == '\'')
putchar('*');
else putchar(s[i]);
}
}
11.执行以下程序的输出结果是()
#include <stdio.h>
#include <string.h>
main()
{
char str1[10] = "China", str2[] = "Beijing";
strcpy(str1, str2);
printf("%s", str1);
}
12.执行以下程序的输出结果是()
#include <stdio.h>
#include <string.h>
main()
{
char s[10] = "12345";
s[2] = '\0';
printf("%d", strlen(s));
}
13.执行以下程序的输出结果是()
#include <stdio.h>
#include <string.h>
main()
{
char a[] = "language", b[] = "program";
int i;
for(i=0; a[i]!='\0'&& b[i]!='\0'; i++)
if(a[i] == b[i])
printf("%c", a[i]);
}
14.执行以下程序的输出结果是()
#include <stdio.h>
main()
{
char str[] = "SSSWLIA", c;
int k;
for(k=2; (c=str[k]) != '\0'; k++)
{
switch(c)
{
case 'I': ++k; break;
case 'L': continue;
default: putchar(c); continue;
}
putchar('*');
}
}
15.执行以下程序的输出结果是()
#include <stdio.h>
#include <string.h>
main()
{
char s1[20] = "ABCD", s2[] = "EFG", s3[] = "HIJ";
strcat(strcat(s1, s2), s3);
printf("%s", s1);
}
16.执行以下程序的输出结果是()
#include <stdio.h>
#include <string.h>
main()
{
char p[] = "abcdefghijklmnopq";
int i=0;
while(p[i] != '\0')
{
if(p[i] >= 'a' && p[i] <= 'z')
p[i] = p[i]-32;
i += 2;
}
puts(p);
}
17.执行以下程序的输出结果是()
#include <stdio.h>
main()
{
char s[] = "12134211";
int v[4]={0}, i;
for(i=0; s[i]; i++) {
switch(s[i]) {
case '1': v[0]++;
case '2': v[1]++;
case '3': v[2]++;
default: v[3]++;
}
}
for(i=0; i<4; i++)
printf("%d,", v[i]);
}
18.执行以下程序的输出结果是()
#include <stdio.h>
main()
{
char str[] = "welcome2018";
int i, n=0;
for(i=0; str[i]!='\0'; i++)
if(str[i] >= '0' && str[i] <= '9')
n = n*10 + (str[i] - '0');
printf("%d", n);
}
19.执行以下程序的输出结果是()
#include <stdio.h>
#include <string.h>
main()
{
char s[] = "012xy\08s34f4w2";
int i, n=0;
for(i=0; s[i]!=0; i++)
if(s[i] >= '0' && s[i] <= '9')
n++;
printf("%d", n);
}
20.执行以下程序的输出结果是()
#include <stdio.h>
#include <string.h>
main()
{
char s[20] = "One", t[20] = "Two";
if(strcmp(s, t) < 0)
strcpy(s, t);
else strcat(s, t);
printf("%s,%s", s, t);
}
三、编程题
1.定义一个初始化为"Programming"的字符数组,通过循环逐个引用并输出每个字符。
2.提示用户输入一个单词,使用getchar()逐个读入并存入数组,再逐个转换为大写后输出。
3.编写密码验证程序,用strcmp()比较用户输入与预设密码。
4.用字符数组存储图案"*********",通过控制输出次数和空格,输出一个3行的三角形。
原创精品资源学科网独家享有版权,侵权必究!
学科网(北京)股份有限公司
$