目录
5.下面的练习涉及字符串、循环、指针和递增指针。首先,假设定义了下面的函数:
考虑下面的函数调用: x = pr("Ho Ho Ho!");
sign占用多少字节的内存?'$'占用多少字节的内存?"$"占用多少字节的内存?
9.本章定义的s_gets()函数,用指针表示法代替数组表示法便可减少一个变量i。请改写该函数
10.strlen()函数接受一个指向字符串的指针作为参数,并返回该字符串的长度。请编写一个这样的函数
11.本章定义的s_gets()函数,可以用strchr()函数代替其中的while循环来查找换行符。请改写该函数
12.设计一个函数,接受一个指向字符串的指针,返回指向该字符串第1个空格字符的指针,或如果未找到空格字符,则返回空指针
13.重写程序清单11.21,使用ctype.h头文件中的函数,以便无论用户选择大写还是小写,该程序都能正确识别答案
1.设计并测试一个函数,从输入中获取下n个字符(包括空白、制表符、换行符),把结果储存在一个数组里,它的地址被传递作为一个参数
2.修改并编程练习1的函数,在n个字符后停止,或在读到第1个空白、制表符或换行符时停止,哪个先遇到哪个停止。不能只使用scanf()
3.设计并测试一个函数,从一行输入中把一个单词读入一个数组中,并丢弃输入行中的其余字符。该函数应该跳过第1个非空白字符前面的所有空白。将一个单词定义为没有空白、制表符或换行符的字符序列
4.设计并测试一个函数,它类似编程练习3的描述,只不过它接受第2个参数指明可读取的最大字符数
9.编写一个函数,把字符串中的内容用其反序字符串代替。在一个完整的程序中测试该函数,使用一个循环给函数提供输入值
10.编写一个函数接受一个字符串作为参数,并删除字符串中的空格。在一个程序中测试该函数,使用循环读取输入行,直到用户输入一行空行。该程序应该应用该函数只每个输入的字符串,并显示处理后的字符串
12.编写一个程序,读取输入,直至读到 EOF,报告读入的单词数、大写字母数、小写字母数、标点符号数和数字字符数。使用ctype.h头文件中的函数。
13.编写一个程序,反序显示命令行参数的单词。例如,命令行参数是 see you later,该程序应打印later you see。
14.编写一个通过命令行运行的程序计算幂。第1个命令行参数是double类型的数,作为幂的底数,第2个参数是整数,作为幂的指数。
15.使用字符分类函数实现atoi()函数。如果输入的字符串不是纯数字,该函数返回0。
16.编写一个程序读取输入,直至读到文件结尾,然后把字符串打印出来。该程序识别和实现下面的命令行参数:
复习题
1.下面字符串的声明有什么问题?
int main(void)
{
char name[] = {'F', 'e', 's', 's' };
...
}
没有终结符'\0',不是一个字符串,是一个字符数组
2.下面的程序会打印什么
#include <stdio.h>
int main(void)
{
char note[] = "See you at the snack bar.";
char *ptr;
ptr = note;
puts(ptr);
puts(++ptr);
note[7] = '\0';
puts(note);
puts(++ptr);
return 0;
}
See you at the snack bar.
ee you at the snack bar.
See you
e you//前面++了一次
3.下面的程序会打印什么
#include <stdio.h>
#include <string.h>
int main(void)
{
char food [] = "Yummy";
char *ptr;
ptr = food + strlen(food);
while (--ptr >= food)
puts(ptr);
return 0;
}
y
my
mmy
ummy
Yummy
4.下面的程序会打印什么
#include <stdio.h>
#include <string.h>
int main(void)
{
char goldwyn[40] = "art of it all ";
char samuel[40] = "I read p";
const char * quote = "the way through.";
strcat(goldwyn, quote);
strcat(samuel, goldwyn);
puts(samuel);
return 0;
}
I read part of it all the way through.
5.下面的练习涉及字符串、循环、指针和递增指针。首先,假设定义了下面的函数:
#include <stdio.h>
char *pr(char *str)
{
char *pc;
pc = str;
while (*pc)
putchar(*pc++);
do
{
putchar(*--pc);
}while (pc - str);
return (pc);
}
考虑下面的函数调用: x = pr("Ho Ho Ho!");
a.将打印什么?
b.x是什么类型?
c.x的值是什么?
d.表达式*--pc是什么意思?与--*pc有何不同?
e.如果用*pc--替换*--pc,会打印什么?
f.两个while循环用来测试什么?
g.如果pr()函数的参数是空字符串,会怎样?
h.必须在主调函数中做什么,才能让pr()函数正常运行?
a. Ho Ho Ho!!oH oH oH
b. char*
c. 第一个H的地址
d. 当前指针所指元素的下一个元素的值;后者的值为当前元素的值 - 1
//*--pc的意思是把指针递减1,并使用存储在其位置上的值。--*pc指解引用pc所指向的值,然后把该值减一
e. Ho Ho Ho! !oH oH o
//!之间会有一个空字符,一般不产生打印效果,但我所用的编译器打印了个空格- -
f. 检测pc是否指向字符串的空字符
g. 第一个循环检测为空不执行,第二个循环pc递减指向空字符前面的存储区,并把其中的内容当做字符打印,此后pc将不会等于str,这个循环过程将一直持续
h. 声明pr(): char* pr(char*);
6.假设有如下声明:
char sign = '$';
sign占用多少字节的内存?'$'占用多少字节的内存?"$"占用多少字节的内存?
- char型变量占一个字节
- 字符型常量一般当做int型存储,一般是2或4个字节,我所用的编译器是4个字节
- 字符串占2个字节,除了'$'还有一个空字符占一个字节
7.下面的程序会打印出什么
#include <stdio.h>
#include <string.h>
#define M1 "How are ya, sweetie? "
char M2[40] = "Beat the clock.";
char * M3 = "chat";
int main(void)
{
char words[80];
printf(M1);
puts(M1);
puts(M2);
puts(M2 + 1);
strcpy(words, M2);
strcat(words, " Win a toy.");
puts(words);
words[4] = '\0';
puts(words);
while (*M3)
puts(M3++);
puts(--M3);
puts(--M3);
M3 = M1;
puts(M3);
return 0;
}
//直接编译打印的- -
How are ya, sweetie? How are ya, sweetie?
Beat the clock.
eat the clock.
Beat the clock. Win a toy.
Beat
chat
hat
at
t
t
at
How are ya, sweetie?
8.下面的程序会打印出什么
#include <stdio.h>
int main(void)
{
char str1 [] = "gawsie";
char str2 [] = "bletonism";
char *ps;
int i = 0;
for (ps = str1; *ps != '\0'; ps++)
{
if (*ps == 'a' || *ps == 'e')
putchar(*ps);
else
(*ps)--;
putchar(*ps);
}
putchar('\n');
while (str2[i] != '\0')
{
printf("%c", i % 3 ? str2[i] : '*');
++i;
}
return 0;
}
faavrhee
//a和e要输出两次
*le*on*sm
9.本章定义的s_gets()函数,用指针表示法代替数组表示法便可减少一个变量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 != '\n' && *st != '\0')
st++;
if (*st == '\n')
*st = '\0';
else
while (getchar() != '\n')
continue;
}
return ret_val;
}
10.strlen()函数接受一个指向字符串的指针作为参数,并返回该字符串的长度。请编写一个这样的函数
int sl(char* st)
{
int n = 0;
while(*st++)
{
n++;
}
return n;
}
11.本章定义的s_gets()函数,可以用strchr()函数代替其中的while循环来查找换行符。请改写该函数
char* s_gets(char* st, int n)
{
char* ret_val;
int i = 0;
ret_val = fgets(st, n, stdin);
if (ret_val)
{
char* f = strchr(st, '\n');
if (f)
*f = '\0';
else
while (getchar() != '\n')
continue;
}
return ret_val;
}
12.设计一个函数,接受一个指向字符串的指针,返回指向该字符串第1个空格字符的指针,或如果未找到空格字符,则返回空指针
char* fs(char* st)
{
while(*st++)
{
if(*st == ' ')
{
return st;
}
}
return NULL;
}
13.重写程序清单11.21,使用ctype.h头文件中的函数,以便无论用户选择大写还是小写,该程序都能正确识别答案
#include <stdio.h>
#include <string.h> // strcmp()函数的原型在该头文件中
#include <ctype.h>
#define ANSWER "grant"
#define SIZE 40
char * s_gets(char * st, int n);
char* tl(char* a)
{
char* b = a;
while(*a)//不能写*a++或者*++a, 其效果都是递增一位再解引用
{
*a = tolower(*a);
a++;
}
return b;
}
int main(void)
{
char try[SIZE];
puts("Who is buried in Grant's tomb?");
s_gets(try, SIZE);
while (strcmp(tl(try), ANSWER) != 0)
{
puts(try);
puts("No, that's wrong. Try again.");
s_gets(try, SIZE);
}
puts("That's right!");
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>
#include <string.h>
void func(char* a, int n)
{
int i;
for (i = 0; i < n; ++i)
{
a[i] = getchar();
}
a[i] = '\0';
}
int main(void)
{
int n;
scanf("%d", &n);
char a[n + 1];
func(a, n);
puts(a);
return 0;
}
2.修改并编程练习1的函数,在n个字符后停止,或在读到第1个空白、制表符或换行符时停止,哪个先遇到哪个停止。不能只使用scanf()
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void func(char* a, int n)
{
int i;
for (i = 0; i < n; ++i)
{
a[i] = getchar();
if(a[i] <= ' ')
{
break;
}
}
a[i] = '\0';
}
int main(void)
{
int n;
scanf("%d", &n);
getchar();
char a[n + 1];
func(a, n);
puts(a);
return 0;
}
3.设计并测试一个函数,从一行输入中把一个单词读入一个数组中,并丢弃输入行中的其余字符。该函数应该跳过第1个非空白字符前面的所有空白。将一个单词定义为没有空白、制表符或换行符的字符序列
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void func(char* a)
{
char ch;
while ((ch = getchar()) <= ' ')
{
continue;
}
while(ch > ' ')//不能在这里写个getchar,第一次循环时已经读入了一个非空白符
{
*a = ch;
a++;
ch = getch