1.只用getchar函数读入一个整数。
输入保证输入的整数能够保存在int中
#include <stdio.h>
#include <stdlib.h>
#define MAXN 10
#include <string.h>
#include <time.h>
int main()
{
char c;
int sum=0;
while((c=getchar())!='\n')
{
sum=sum*10+(c-48);
}
printf("sum=%d",sum);
return 0;
}
2.
#include <stdio.h>
#include <stdlib.h>
#define MAXN 10
#include <string.h>
#include <time.h>
int main()
{
char s[20];
char c;
int sum=0;
int i=0;
fgets(s,20,stdin);
while(s[i]!='\n')
{
sum=sum*10+s[i]-48;
i++;
}
printf("%d\n",sum);
char m='\0';
if(s[i+1]==m)
printf("shide!");
else
printf("bushi");
return 0;
}
fgets函数会在键盘读取字符串之后自动在末尾加\0
还会读入字输入敲入的\n
3.用strchr
char s[20];
gets(s);
if(strchr(s,'B')!=NULL)
printf("zhaodaole\n");
char *p=strchr(s,'B');
printf("该字符是%c",*p);
char strchr(const char _Str,char _Val)
char strchr(char _Str,char _Ch)
头文件:#include