首页 > 其他 > 详细

C Primer Plus 第11章 字符串和字符串函数 复习题与编程练习

时间:2015-11-30 02:29:59      阅读:533      评论:0      收藏:0      [点我收藏+]
今天学到了一个新知识——选择排序算法
核心思想:(查找和放置)选择剩余最大值的一个办法就是比较剩余数组的第一和第二个元素。如果第二个元素大,就交换这两个数据。现在比较第一个和第三个元素。如果第三个大,就交换这两个数据。每次交换都把大的元素移到上面。继续这种方法,直到比较第一个和最后一个元素。完成以后,最大的数就在剩余数组的第一个元素中。此时第一个元素已经排好了序,但是数组中的其他元素还很混乱。
外部循环表明要填充哪一个数组元素,内循环找出该数组元素中要放置的值。(这句话,理解不是很清楚!!!,还是用一个例子来说明吧!)
// 把一个字符串按字母表顺序排序
#include <stdio.h>
#include <string.h>
#define SIZE 81
#define LIM 20
#define HALT " "
// 字符串排序函数
void stsrt(char *strings[], int num);
int main(void)
{
    char input[LIM][SIZE];
    char *ptstr[LIM];
    int ct = 0;
    int k;

    printf("Input up to %d lines, and I will sort them.\n", LIM);
    printf("To stop, press the Enter key at a line‘s start.\n");
    while(ct < LIM && gets(input[ct]) != NULL && input[ct][0] != ‘\0‘)
    {
        ptstr[ct] = input[ct];
        ct++;
    }
    stsrt(ptstr, ct);
    puts("\nHere‘s the sorted list: \n");
    for(k = 0; k < ct; k++)
        puts(ptstr[k]);

    return 0;
}
void stsrt(char *strings[], int num)
{
    char *temp;
    int top, seek;

    for(top = 0; top < num - 1; top++)
        for(seek = top + 1; seek < num; seekas++)
            if(strcmp(strings[top], strings[seek]) > 0)
            {
                temp = strings[top];
                strings[top] = strings[seek];
                strings[seek] = temp;
            }
}
复习题
1、下面这个字符串的声明错在哪里?
int main(void)
{
char name[] = {‘F‘, ‘e‘, ‘s‘, ‘s‘};
bubuko.com,布布扣
}
答:
如果想得到一个字符串,就应该在初始化中包含一个‘\0‘。当然,一种语法可以自动添加空字符:
char name[] = "Fess";
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   注意:不是e you,ptr指针已经指向note[1]元素
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";
    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的指针,也就是说,char *类型
c.
第一个H的地址
d.
*--pc是把指针减1并使用那里的值。--*pc取出pc指向的值然后把那个值减1
e.
Ho Ho Ho!!oH oH o(注意:在!和!之间有一个空字符,但是它不产生任何打印效果)
f.
while(*pc)检查pc是否指向一个空字符(也就是说字符串的结尾)。这个表达式使用指针所指向位置的值。
while(pc - str)检查pc是否与str指向同一个地址(字符串的开始)。这个表达式使用指针本身的值。
g.
在第一个while循环之后,pc指向空字符。在进入第二个循环后令它指向空字符之前的存储区,也就是说str指向的位置之前的位置,把那个字节解释为一个字符并进行打印。然后指针再退回到前面的字节处。永远都不会满足终止条件(pc == str),所以这个过程会一直继续下去。
h.
必须在调用程序中对pr()进行声明:char *pr(char *);
6、假定有下列声明:
char sign = ‘$‘;
sign的存储需要多少字节?"$"呢?
答:字符变量占用一个字节,所以sign占用一个字节。但是字符常量是被存储在一个int中的,也就是说‘$‘通常会使用2个或4个字节;但是实际上只使用int的一个字节来存储‘$‘的编码。字符串"$"使用两个字节,一个用来保存‘$‘,一个用来保存‘\0‘。
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. (注意:M2的指向没有改变,M2 + 1怎么没有改变指针指向呢?)
Beat
chat
hat
at
t
t
at
How are ya, sweetie?
8、下面程序会打印出什么?
待续。。。。





























C Primer Plus 第11章 字符串和字符串函数 复习题与编程练习

原文:http://www.blogjava.net/BeautifulMan/archive/2015/11/29/428413.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!