c primer plus 7编程练习

2021/8/23 1:28:29

本文主要是介绍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;
} 

 

 

#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;
}

 

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;
}

 

#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编程练习的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程