c primer plus 7编程练习

 

 

1、

#include <stdio.h>
#include <ctype.h>

int main(void)
{
    char ch;
    int n_space = 0, n_lines = 0, n_other = 0; 
    
    
    while((ch = getchar()) != '#')
    {
        if(ch == ' ')
            n_space++;
        else if(ch == '\n')
            n_lines++;
        else
            n_other++;
    }
    
    printf("n_space: %d;  n_lines: %d;  n_other: %d\n", n_space, n_lines, n_other);
    
    return 0;
} 

c primer plus 7编程练习

 

 

#include <stdio.h>
int main(void)
{
    char ch;
    int blank = 0;
    int endline = 0;
    int others = 0;
    
    printf("please input chars(# to quit): ");
    
    while((ch = getchar()) != '#')
    {
        if(ch == ' ')
            blank++;
        else if(ch == '\n')
            endline++;
        else
            others++;
    }
    
    printf("blank: %d;  endline: %d;  other: %d.\n", blank, endline, others);
    
    return 0;
}

c primer plus 7编程练习

 

2、

#include <stdio.h>

int main(void)
{
    char ch;
    int n = 1;
    
    while((ch = getchar()) != '#')
    {
        printf("%c→%d  ", ch, ch);
        if(n++ % 8 == 0)
            printf("\n");
    }
    
    return 0;
}

c primer plus 7编程练习

 

#include <stdio.h>

int main(void)
{
    char ch;
    int counter = 0;
    printf("please input chars(# for exit): ");
    
    while((ch = getchar()) != '#')
    {
        if(counter++ % 8 == 0)
            printf("\n");
        if(ch == '\n')
            printf("\\n-%03d  ", ch);
        else if(ch == '\t')
            printf("\\t-%03d  ", ch);
        else
            printf("%c-%03d  ", ch, ch);
    }
    
    return 0;
}

c primer plus 7编程练习

 

上一篇:《C++primer学习笔记》——第6章函数


下一篇:c primer plus 10 复习题