|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
输入描述:
第一行输入一个有字母和数字以及空格组成的字符串,第二行输入一个字符。
输出描述:
输出输入字符串中含有该字符的个数。
#include<stdio.h>
int main()
{
char str[1000],b;
int i=0,j=0;
scanf("%s",str);
scanf("%c",&b);
while(str[j]!=EOF)
{
if(str[j]==b||str[j]==b+32)
{
i++;
}
j++;
}
printf("%d",i);
return 0;
}
- #include<stdio.h>
- #include"stdlib.h"
- int main()
- {
- char str[1000]= "\0",b;
- int i=0,j=0;
- //scanf("%s",str);//用scanf输入字符串是不对滴~~,遇到空白符(空格、Tab、回车)就会结束了,
- //而正常情况下我们应该是想输入回车的时候结束,
- //还有个问题scanf函数不能清空缓冲区,你的程序中用scanf读取%s之后,接着用scanf读取%c会读取到之前留下的空白符
- //两种方法,一种是用gets读取字符串,不过这种方式有一定的风险,建议少用
- //第二种方法是是用getchar逐个读取字符,赋值给字符数组
- //gets(str);
- while((b=getchar())!=EOF&&b!='\n'){
- if (i<999)//保证字符数组能容下输入的内容
- str[i++]=b;
- }
- printf("%s\n",str);
- scanf("%c",&b);
- printf("%c\n",b);
- i=0;
- while(str[j]!=EOF)
- {
- if(str[j]==b||str[j]==b+32)
- {
- i++;
- }
- j++;
- }
- printf("%d",i);
- return 0;
- }
复制代码
|
|