ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

c primer plus 7编程练习

2021-08-23 01:02:57  阅读:145  来源: 互联网

标签:ch int 编程 else ++ other plus printf primer


 

 

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

 

标签:ch,int,编程,else,++,other,plus,printf,primer
来源: https://www.cnblogs.com/liujiaxin2018/p/15174198.html

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有