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