C Primer Plus(第6版) 第十一章 编程练习及部分复习题参考答案
编译环境:Microsoft Visual Studio 2019
备注:本文留作作者自用,如有错误敬请指出
(针对Microsoft Visual Studio 2019的一些特性对答案进行了修改)
复习题
9.本章定义的s_gets()函数,用指针表示法代替数组表示法便可减少一个变量i。请改写该函数。
char* s_gets(char* st, int n)
{
char* ret_val;
ret_val = fgets(st,n,stdin);
if (ret_val)
{
while (*st != '\n' && *st != '\0')
st++;
if (*st == '\n')
*st = '\0';
else
while (getchar() != '\n')
continue;
}
return ret_val;
}
- strlen()函数接受一个指向字符串的指针作为参数,并返回该字符串的长度。请编写一个这样的函数。
int* strleng(char* st)
{
int len;
for (len = 0;st[len] != '\0';len++);
return len;
}
11.本章义的s_gets()函数,可以用 strchr()函数代替其中的 while循环来查找换行符请改写该函数。
char* s_gets(char* st, int n)
{
char* ret_val;
char * new;
ret_val = fgets(st,n,stdin);
if (ret_val)
{
new = strchr(st,'\n');
if (new)
*new= '\0';
else
while (getchar() != '\n')
continue;
}
return ret_val;
}
12.设计一个函数,接受一个指向字符串的指针,返回指向该字符串第1个空格字符的指针,或如果未找到空格字符,则返回空指针。
char* blank(char* p)
{
char* bl;
int i;
for (i = 0;p[i] != '\0';i++)
if (p[i] == ' ')
{
bl = &p[i];
return bl;
}
return NULL;
}
13.重写程序清单11.21,使用 ctype.h头文件中的函数,以便无论用户选择大写还是小写,该程序都能正确识别答案。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#define ANSWER "GRANT"
#define SIZE 40
char* s_gets(char* st, int n);
int main(void)
{
int i;
char try[SIZE];
puts("Who is buried in Grant's tomb?");
s_gets(try,SIZE);
for (i = 0;i < SIZE;i++)
try[i] = toupper(try[i]);
while (strcmp(try, ANSWER) != 0)
{
puts("No,that's wrong.Try again.");
s_gets(try,SIZE);
}
puts("That's right!");
system("pause");
return 0;
}
char* s_gets(char* st, int n)
{
char* ret_val;
int i = 0;
ret_val = fgets(st,n,stdin);
if (ret_val)
{
while (st[i] != '\n' && st[i] != '\0')
i++;
if (st[i] == '\n')
st[i] ='\0';
else
while (getchar() != '\n')
continue;
}
return ret_val;
}
编程练习
1.设计并测试一个函数,从输入中获取n个字符(包括空白、制表符、换行符),把结果存储在一个数组里,它的地址被传递作为一个参数。
#include<stdio.h>
#include<stdlib.h>
#define SIZE 40
void gain(char* st,int n);
int main(void)
{
char ch[SIZE+1];
gain(ch,SIZE);
puts(ch);
system("pause");
return 0;
}
void gain(char* st,int n)
{
char ch;
int i = 0;
for(;(ch=getchar())!= EOF && i<n;i++)
st[i] = ch;
st[i] = '\0';
}
2.修改并编程练习1的函数,在n个字符后停止,或在读到第1个空白、制表符或换行符时停止,哪个先遇到哪个停止。不能只使用scanf()。
#include<stdio.h>
#include<stdlib.h>
#define SIZE 5
char* gain(char* st,int n);
int main(void)
{
char ch[SIZE+1];
*gain(ch,SIZE) = '\0';
puts(ch);
system("pause");
return 0;
}
char* gain(char* st,int n)
{
char ch;
int i = 0;
for(;(ch=getchar())!= '\0' &&ch!='\b'&&ch!='\n'&& i<n;i++)
st[i] = ch;
return st + i;
}
3.设计并测试一个函数,从一行输入中把一个单词读入一个数组中,并丢弃输入行中的其余字符。该函数应该跳过第1个非空白字符前面的所有空白。将一个单词定义为没有空白、制表符或换行符的字符序列。
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#define SIZE 20
void gain(char* st);
int main(void)
{
char ch[SIZE];
gain(ch);
puts(ch);
system("pause");
return 0;
}
void gain(char* st)
{
char ch;
int i = 0;
int flag=0;
ch = getchar();
while (ch != EOF)
{
for (;!isblank(ch);i++)
{
st[i] = ch;
ch = getchar();
flag = 1;
}
ch = getchar();
if (flag)
break;
}
st[i] = '\0';
return;
}
4.设计并测试一个函数,它类似编程练习3的描述,只不过它接受第2个参数指明可读取的最大字符数。
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#define SIZE 5
void gain(char* st,int n);
int main(void)
{
char ch[SIZE+1];
gain(ch,SIZE);
puts(ch);
system("pause");
return 0;
}
void gain(char* st,int n)
{
char ch;
int i = 0;
int flag=0;
ch = getchar();
while (ch != EOF)
{
for (;!isblank(ch) && i<n;i++)
{
st[i] = ch;
ch = getchar();
flag = 1;
}
ch = getchar();
if (flag)
break;
}
st[i] = '\0';
}
5.设计并测试一个函数,搜索第1个函数形参指定的字符串,在其中查找第2个函数形参指定的字符首次出现的位置。如果成功,该函数返指向该字符的指针,如果在字符串中未找到指定字符,则返回空指针(该函数的功能与 strchr()函数相同)。在一个完整的程序中测试该函数,使用一个循环给函数提供输入值。
#include<stdio.h>
#include<stdlib.h>
#define SIZE 200
char* find(char *st,char c);
char* s_gets(char* st, int n);
int main(void)
{
char ch;
char string[SIZE];
char* pos;
printf("Please enter the character you want to find(enter to quit):");
scanf_s("%c", &ch,sizeof(ch));
while (ch != '\n')
{
while (getchar() != '\n')//丢弃换行符
continue;
printf("Please enter a string to find the specified character:\n");
s_gets(string, SIZE);
if (pos = find(string, ch))
puts(pos);
else
printf("The character was not found!\n");
printf("Please enter the character you want to find(enter to quit):");
scanf_s("%c", &ch, sizeof(ch));
}
printf("Bye.");
system("pause");
return 0;
}
char* find(char* st, char c)
{
for (;*st != '\0';st++)
if (*st == c)
return st;
return NULL;
}
char* s_gets(char* st, int n)
{
char* ret_val;
int i = 0;
ret_val = fgets(st,n,stdin);
if (ret_val)
{
while (st[i] != '\n' && st[i] != '\0')
i++;
if (st[i] == '\n')
st[i] ='\0';
else
while (getchar() != '\n')
continue;
}
return ret_val;
}
6.编写一个名为is_within()的函数,接受一个字符和一个指向字符串的指针作为两个函数形参。如果指定字符在字符串中,该函数返回一个非零值(即为真)。否则,返回0(即为假).在一个完整的程序中测试该函数,使用一个循环给函数提供输入值。
#include<stdio.h>
#include<stdlib.h>
#define SIZE 200
int* find(char *st,char c);
char* s_gets(char* st, int n);
int main(void)
{
char ch;
char string[SIZE];
printf("Please enter the character you want to find(enter to quit):");
scanf_s("%c", &ch,sizeof(ch));
while (ch != '\n')
{
while (getchar() != '\n')
continue;
printf("Please enter a string to find the specified character:\n");
s_gets(string, SIZE);
if (find(string, ch))
printf("This character %c is in the string.\n",ch);
else
printf("The character was not found!\n");
printf("Please enter the character you want to find(enter to quit):");
scanf_s("%c", &ch, sizeof(ch));
}
printf("Bye.");
system("pause");
return 0;
}
int* find(char* st, char c)
{
for (;*st != '\0';st++)
if (*st == c)
return 1;
return 0;
}
char* s_gets(char* st, int n)
{
char* ret_val;
int i = 0;
ret_val = fgets(st,n,stdin);
if (ret_val)
{
while (st[i] != '\n' && st[i] != '\0')
i++;
if (st[i] == '\n')
st[i] ='\0';
else
while (getchar() != '\n')
continue;
}
return ret_val;
}
- strncpy(s1,s2,n)函数把s2中的n个字符拷贝至s1中,截断s2,或者有必要的话在末尾添加空字符。如果s2的长度是n或多于n,目标字符串不能以空字符结尾。该函数返回s1。自己编写一个这样的函数,名为 mystrncpy(),在一个完整的程序中测试该函数,使用一个循环给函数提供输入值。
#include<stdio.h>
#include<stdlib.h>
#define SIZE 5
char* mystrncpy(char *st1,char *st2,int n);
char* s_gets(char* st, int n);
int main(void)
{
char s1[SIZE];
char s2[100];
char* p;
printf("Please enter the source string(& to quit):");
s_gets(s2,100);
while (s2[0] != '&')
{
p = mystrncpy(s1, s2, SIZE - 1);
s1[SIZE - 1] = '\0';
puts(p);
printf("Please enter the source string(& to quit):");
s_gets(s2, 100);
}
printf("Bye.");
system("pause");
return 0;
}
char* mystrncpy(char* st1, char* st2, int n)
{
int i;
for (i = 0;i < n;i++)
{
if (st2[i] != '\0')
st1[i] = st2[i];
else
st1[i] = '\0';
}
return &st1[0];
}
char* s_gets(char* st, int n)
{
char* ret_val;
int i = 0;
ret_val = fgets(st,n,stdin);
if (ret_val)
{
while (st[i] != '\n' && st[i] != '\0')
i++;
if (st[i] == '\n')
st[i] ='\0';
else
while (getchar() != '\n')
continue;
}
return ret_val;
}
8.编写一个名为 string_in()的函数,接受两个指向字符串的指针作为参数。如果第2个字符串包含在第1个字符串中,该函数将返回第1个字符串开始的地址。例如,string_in(“hats”,“at”)将返回hats中a的地址,否则,该函数返回空指针。在一个完整的程序中测试该函数,使用一个循环给函数提供输入值。
//简单匹配算法
#include<stdio.h>
#include<stdlib.h>
#define SIZE 10
char* string_in(char *st1,char *st2);
char* s_gets(char* st, int n);
int main(void)
{
char s1[SIZE];
char s2[10];
char* p;
printf("Please enter the string you want to find(& to quit):\n");
s_gets(s2,10);
while (s2[0] != '&')
{
printf("Please enter the string to be searched:");
s_gets(s1, SIZE);
if (p = string_in(s1, s2))
puts(p);
else
printf("The string was not found!\n");
printf("Please enter the string you want to find(& to quit):");
s_gets(s2, 100);
}
printf("Bye.");
system("pause");
return 0;
}
char* string_in(char* st1, char* st2)
{
int i = 0, j = 0;
while (st1[i + j] != '\0' && st2[j] != '\0')
{
if (st1[i + j] == st2[j])
j++; // 继续比较后一字符
else
{
i++;
j = 0; // 重新开始新的一轮匹配
}
}
if (st2[j] == '\0')
return &st1[0]; // 匹配成功,返回地址
else
return NULL;
}
char* s_gets(char* st, int n)
{
char* ret_val;
int i = 0;
ret_val = fgets(st,n,stdin);
if (ret_val)
{
while (st[i] != '\n' && st[i] != '\0')
i++;
if (st[i] == '\n')
st[i] ='\0';
else
while (getchar() != '\n')
continue;
}
return ret_val;
}
9.编写一个函数,把字符串中的内容用其反序字符代替,在一个完整的程序中测试该函数,使用一个循环给函数提供输入值。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define SIZE 10
void rev(char *st1);
char* s_gets(char* st, int n);
int main(void)
{
char s1[SIZE];
printf("Please enter a string(& to quit):\n");
s_gets(s1,SIZE);
while (s1[0] != '&')
{
rev(s1);
printf("The string after reverse order is:\n");
puts(s1);
printf("Please enter a string(& to quit):\n");
s_gets(s1, SIZE);
}
printf("Bye.");
system("pause");
return 0;
}
void rev(char* st)
{
int len,i;
char str[SIZE];
len = strlen(st);
for (i = 0;i < SIZE;i++)
str[i] = st[i];
for (i = 0;i < (len / 2);i++)
st[i] = st[len-1-i];
for (i = len / 2;i < len;i++)
st[i] = str[len - 1 - i];
}
char* s_gets(char* st, int n)
{
char* ret_val;
int i = 0;
ret_val = fgets(st,n,stdin);
if (ret_val)
{
while (st[i] != '\n' && st[i] != '\0')
i++;
if (st[i] == '\n')
st[i] ='\0';
else
while (getchar() != '\n')
continue;
}
return ret_val;
}
10.编写一个函数接受一个字符串作为参数,并删除字符串中的空格。在一个程序中测试该函数,使用循环读取输入行,直到用户输入一行空行。该程序应该应用该函数读取每个输入的字符串,并显示处理后的结果。
#include<stdio.h>
#include<stdlib.h>
#define SIZE 100
void del(char *st1);
char* s_gets(char* st, int n);
int main(void)
{
char s1[SIZE];
printf("Please enter a string(enter to quit):\n");
cir:if (s_gets(s1, SIZE))
{
del(s1);
printf("After removing the space, the string is:\n");
puts(s1);
printf("Please enter a string(enter to quit):\n");
goto cir;
}
else
printf("Bye.");
system("pause");
return 0;
}
void del(char* st)
{
int i,j;
char str[SIZE];
for (i = 0;i < SIZE;i++)
str[i] = st[i];
for (i = 0, j = 0;str[j] != '\0';i++, j++)
if (str[j] == ' ')
{
do
{ j++;
} while (str[j] == ' ');
st[i] =str[j];
}
else
st[i] = str[j];
st[i] = '\0';
}
char* s_gets(char* st, int n)
{
char* ret_val;
int i = 1;
ret_val = fgets(st,n,stdin);
if (ret_val)
{
if (st[0] == '\n')
return NULL;
else
{
while (st[i] != '\n' && st[i] != '\0')
i++;
if (st[i] == '\n')
st[i] = '\0';
else
while (getchar() != '\n')
continue;
return st;
}
}
return ret_val;
}
11.编写一个程序,读入10个字符串或者读到EOF时停止,该程序为用户提供一个有5个选项的菜单:打印源字符串列表、以 ASCII中的顺序打印字符串,按长度递增顺序打印字符串、按字符串中第1个单词的长度打印字符串、退出。菜单可以循环显示,除非用户选择退出选项。当然,该程序要能真正完成菜单中各选项的功能。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define SIZE 100
#define LIM 10
char menu(void);
char* s_gets(char* st, int n);
void a(char(*p)[SIZE]);
void b(char(*p)[SIZE]);
void c(char(*p)[SIZE]);
void d(char(*p)[SIZE]);
int len(char *ptr);
int main(void)
{
char input[LIM][SIZE];
int i = 0;
int j;
char cho;
printf("Input up to %d lines.",LIM);
printf("To stop,enter '&' at start.\n");//不想用EOF
while (i < LIM && s_gets(input[i], SIZE) != NULL)
i++;
if (input[0][0] == '&')
goto bye;
else
{
cho = menu();
while (cho != 'q')
{
switch (cho)
{
case 'a':a(input);
break;
case 'b':b(input);
break;
case 'c':c(input);
break;
case 'd':d(input);
break;
}
cho = menu();
}
}
bye: printf("Bye.");
system("pause");
return 0;
}
char* s_gets(char* st, int n)
{
char* ret_val;
int i = 1;
ret_val = fgets(st,n,stdin);
if (ret_val)
{
if (st[0] == '&')
return NULL;
else
{
while (st[i] != '\n' && st[i] != '\0')
i++;
if (st[i] == '\n')
st[i] = '\0';
else
while (getchar() != '\n')
continue;
return st;
}
}
return ret_val;
}
char menu(void)
{
char choice,ch;
printf("***********************\n");
printf("Please select the operation you need:\n");
printf("a) Print source string list\n");
printf("b) Print strings in ASCII order\n");
printf("c) Print strings in length increasing order\n");
printf("d) Print a string by the length of the first word\n");
printf("q) quit\n");
printf("***********************\n");
scanf_s("%c", &choice, sizeof(char));
while ((ch = getchar()) != '\n')
continue;
return choice;
}
void a(char(*p)[SIZE])
{
int i;
printf("The results are as follows:\n");
for (i = 0;i < LIM;i++)
puts(p[i]);
}
void b(char(*p)[SIZE])
{
int i=0,j=0;
char* ptr[LIM];
char* temp;
while (i <LIM)
{
ptr[i] = p[i];
i++;
}
for(i=0;i<LIM-1;i++)
for(j=i+1;j<LIM;j++)
if (strcmp(ptr[i], ptr[j]) > 0)
{
temp = ptr[i];
ptr[i] = ptr[j];
ptr[j] = temp;
}
printf("The results are as follows:\n");
for(i=0;i<LIM;i++)
puts(ptr[i]);
}
void c(char(*p)[SIZE])
{
int i = 0, j = 0;
char* ptr[LIM];
char* temp;
while (i < LIM)
{
ptr[i] = p[i];
i++;
}
for (i = 0;i < LIM - 1;i++)
for (j = i + 1;j < LIM;j++)
if (strlen(ptr[i])>strlen(ptr[j]))
{
temp = ptr[i];
ptr[i] = ptr[j];
ptr[j] = temp;
}
printf("The results are as follows:\n");
for (i = 0;i < LIM;i++)
puts(ptr[i]);
}
void d(char(*p)[SIZE])
{
int i = 0, j = 0;
char* ptr[LIM];
char* temp;
while (i < LIM)
{
ptr[i] = p[i];
i++;
}
for (i = 0;i < LIM - 1;i++)
for (j = i + 1;j < LIM;j++)
if (len(ptr[i]) > len(ptr[j]))
{
temp = ptr[i];
ptr[i] = ptr[j];
ptr[j] = temp;
}
printf("The results are as follows:\n");
for (i = 0;i < LIM;i++)
puts(ptr[i]);
}
int len(char* ptr)
{
int i;
for (i = 0;ptr[i] != ' ';i++);
return i;
}
12.编写一个程序,读取输入,直至读到EOF,报告读入的单词数、大写字母数、小写字母数、标点符号数和数字字符数。使用 ctype.h头文件中的函数。
//用&代替EOF
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
int main(void)
{
int i;
char ch[100];
int word=0, capital=0, low=0, punct=0, num=0;
int flag=0;
printf("请输入一些字符(&停止):\n");
for (i = 0;(ch[i] = getchar()) != '&';i++);
for (i = 0;ch[i] != '&';i++)
{
if (!isspace(ch[i]) && !flag&&!ispunct(ch[i]))
{
flag = 1;
word++;
}
if (isspace(ch[i])||ispunct(ch[i]) && flag)
flag = 0;
if (isupper(ch[i]))
capital++;
else if (islower(ch[i]))
low++;
else if (ispunct(ch[i]))
punct++;
else if (isdigit(ch[i]))
num++;
}
printf("一共读入%d个单词,%d个大写字母,%d个小写字母,",word,capital,low);
printf("%d个标点符号,%d个数字字符\n",punct,num);
system("pause");
return 0;
}
13.编写一个程序,反序显示命令行参数的单词。例如,命令行参数是 see you later,该程序应打印 later you see。
#include<stdio.h>
#include<stdlib.h>
int main(int argc,char *argv[])
{
int n;
for (n = argc - 1;n > 0;n--)
printf("%s ",argv[n]);
system("pause");
return 0;
}
在vs2019中,依次点击:项目–>属性–>配置属性–>调试–>命令参数,输入命令行参数
14.编写一个通过命令行运行的程序计算幂。第1个命令行参数是 double类型的数,作为幂的底数,第2个参数是整数,作为幂的指数。
#include<stdio.h>
#include<stdlib.h>
int main(int argc,char*argv[])
{
double base;
int index;
int i;
double n;
base = atof(argv[1]);
index = atoi(argv[2]);
for (i = 0,n=1;i < index;i++)
n *= base;
printf("%f的%d次幂为%f",base,index,n);
system("pause");
return 0;
}
15.使用字符分类函数实现atoi()函数,如果输入的字符串不是纯数字,该函数返回0。
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
char* s_gets(char* st, int n);
int ati(char *p);
int power(int time);
int main(void)
{
char ch[10];
int num;
printf("请输入一个全为数字的字符串:\n");
s_gets(ch,10);
if (num = ati(ch))
printf("%d", num);
else
printf("输入的不是纯数字!");
system("pause");
return 0;
}
char* s_gets(char* st,int n)
{
int i=0;
char *ret_val;
ret_val = fgets(st,n,stdin);
if (ret_val)
{
while (st[i] != '\n' && st[i] != '\0')
i++;
if (st[i] == '\n')
st[i] ='\0';
else
while (getchar() != '\n')
continue;
}
return ret_val;
}
int ati(char* p)
{
int i = 0;
int n = strlen(p);
int sum=0;
for (;i <n;i++)
if (!isdigit(p[i]))
return 0;
for (i--;i >= 0;i--)//要将ASCII值转换为字符代表的数字
sum += (p[i]-48) * power(n - 1 - i);
return sum;
}
int power(int time)
{
int i,j ;
for (i=0,j=1;i<time;i++)
j *=10;
return j;
}
16.编写一个程序读取输入,直至读到文件结尾,然后把字符串打印出来。该程序识别和实现下面的命令行参数:
-p 按原样打印
-u把输入全部转换成大写
-l把输入全部转换成小写
如果没有命令行参数,则让程序像是使用了-p参数那样运行。
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
char* s_gets(char* st, int n);
int main(int argc,char *argv[])
{
char ch[100];
int i;
printf("请输入一个字符串:\n");
s_gets(ch,100);
if (strcmp("-p", argv[1]) == 0)
puts(ch);
if (strcmp("-u", argv[1]) == 0)
{
for (i = 0;i < strlen(ch);i++)
ch[i] = toupper(ch[i]);
puts(ch);
}
if (strcmp("-l", argv[1]) == 0)
{
for (i = 0;i < strlen(ch);i++)
ch[i] = tolower(ch[i]);
puts(ch);
}
system("pause");
return 0;
}
char* s_gets(char* st,int n)
{
int i=0;
char ret_val;
ret_val = fgets(st,n,stdin);
if (ret_val)
{
while (st[i] != '\n' && st[i] != '\0')
i++;
if (st[i] == '\n')
st[i] = '\0';
else
while (getchar() != '\n')
continue;
}
return ret_val;
}