第8章 编程练习

#include <stdio.h>

int main(int argc, char const *argv[])
{
    FILE* fp = fopen("1.txt","r");//只读的方式

    char ch;
    int num = 0;
 
    while((ch = getc(fp))!=EOF)
    {
        num++;
    }    
    printf("%d\n",num);
    return 0;
}

思考:getc和getchar有什么区别?
           getchar()只能从标准输入读取,相当于getc(stdin) 

           getchar()和putchar()直接从命令行读入,并且输出到命令行!

      

#include <stdio.h>

int main(int argc, char const *argv[])
{
    char ch;
    while((ch = getchar())!=EOF)
    {
        //空格之前的字符是非打印字符,需要特殊处理
        if (ch<' ')
        {
            if('\t' == ch)
            {
                putchar('\\');
                putchar('t');  //把 \t 打印到命令行
                printf(":%d ",ch);
            }
            else if('\n' == ch)
            {
                putchar('\\');
                putchar('n');  //把 \n 打印到命令行
                printf(":%d ",ch);
            }
            else
            {
                putc('^',stdout);
                putchar(ch+64);
                printf(":%d ",ch);
            }
        }
        else
        {
            putchar(ch);
            printf(":%d ",ch);
        }
    }
    return 0;
}

//fgetc和getc有什么区别?

//fgetc是函数,而getc是宏

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

int main(int argc, char const *argv[])
{
    char ch;
    int numB = 0,numS = 0;
    while ((ch = getchar())!=EOF)
    {
        if(islower(ch))//小写字母
        {
            numS++;
        }
        else if(isupper(ch))//大写字母
        {
            numB++;
        }
        else
            break;
    }
    printf("%d,%d",numB,numS);
    return 0;
}

 思考:islower()  判断是否小写,isupper()判断是否大写

            头文件 #include<ctype.h>    

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    char ch = ' ';
    //int num[10];
    int *num = (int*)malloc(sizeof(int)*10);
    memset(num,0,sizeof(int)*10);
    int i = 0;
    while((ch = getchar())!= '\n')
    {
        if(ch != ' ' && ch != '\0')
        {
            num[i]++;
        }
        else
        {
            i++;
        }
    }

    for (int i = 0; i < 10; i++)
    {
        if(num[i]!=0)
        {
            printf("%d\n",num[i]);
        }
    }
    free(num);
    
    return 0;
}

思考:isspace() 检测是否是空格,ispunct()检测是否是标点符号,isalpha()检测是否英文字母

头文件:#include <ctype.h>

#include <stdio.h>

int main(int argc, char const *argv[])
{
    int low = 0;
    int high = 100;
    int guest = 50;
    printf("I guest the answer is 50,Y or N ?\n");
    char ch;
    scanf("%c",&ch);
    getchar(); // 吃掉回车

    while('N' == ch)
    {
        printf("Ok, the number you chosen is bigger or smaller than I guest?(B/S)");
        scanf("%c",&ch);
        getchar();

        if('B' == ch)
        {
            low = guest+1;
        }
        else
        {
            high = guest-1;
        }
        guest = (low+high)/2;
        printf("I guest the answer is %d,Y or N ?\n",guest);
        scanf("%c",&ch);
        getchar();
    }

    printf("the last answer is %d.\n",guest);

    return 0;
}

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

int main(){
    char ch;
    // while ((ch = getchar())== ' ')
    // {
    //     continue;
    // }
    //为啥isspace不行,搞不懂??
    while (!isalpha(ch = getchar()))
    {
        continue;
    }
    putchar(ch);
    return 0;
 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

coder_Alger

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值