写出一个程序,接受一个由字母、数字和空格组成的字符串,和一个字符,然后输出输入字符串中该字符的出现次数。(不区分大小写字母)
数据范围:
#include <stdio.h> #include <string.h> #define MaxInputLength 1010 int main() { char strRev[MaxInputLength] = {0}; char checkChar = 0, checkChar2 = 0; int num = 0; int check2Flag = 0; fgets(strRev,MaxInputLength,stdin); do { checkChar = getchar(); }while(checkChar =='\n'); if(checkChar>='a' && checkChar<='z') { checkChar2 = checkChar+'A'-'a'; check2Flag = 1; } else if(checkChar>='A' && checkChar<='Z') { checkChar2 = checkChar+'a'-'A'; check2Flag = 1; } else { check2Flag = 0; } for(int i=0;i<MaxInputLength;i++) { if(check2Flag) { if(strRev[i] == checkChar || strRev[i] == checkChar2) num++; } else { if(strRev[i] == checkChar) num++; } } printf("%d",num); return 0; }
#include <stdio.h> #include<string.h> int main() { char str[1000],a; int len,count=0,i; gets(str); scanf("%c",&a); len=strlen(str); for(i=0;i<len;i++) { if(a>='0'&& a<='9') { if(str[i]==a) count++; } else if(str[i]==a||str[i]+32==a||str[i]-32==a) count++; } printf("%d",count); return 0; }如果是数字要单独排除
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char str[1002] = {0};//测1000个用例,多出的2个字节是保存'\0''\n'的? int num = 0; fgets(str,sizeof(str), stdin); char ch = 0; scanf("%c", &ch); if((ch >= 'a') && (ch <= 'z') ) { ch -= 32; } for (int i = 0;i < strlen(str);i++) { if((str[i] >= 'a') && (str[i] <= 'z') ) { if (ch == (str[i] - 32)) { num++; } } else if (ch == str[i]) { num++; } } printf("%d\n", num); return 0; }
#include <ctype.h> #include <stdio.h> #include <string.h> #define STLEN 1010 // 1.处理输入 // 2、统计个数strchr // 2.1、若是数字 // 2.2、若是字母,则要分别统计大写字母和小写字母的个数再相加 int main() { char str[STLEN] = {0}; char ch; unsigned count = 0; fgets(str, STLEN, stdin); ch = getchar(); char *find = NULL; if(isdigit(ch)) { // while((find = strchr(str, ch))) // { // ++ count; // } find = strchr(str, ch); while(find) { count ++; find = strchr(find+1, ch); } } if(isalpha(ch)) { ch = tolower(ch); find = strchr(str, ch); while(find) { count ++; find = strchr(find+1, ch); } ch = toupper(ch); find = strchr(str, ch); while(find) { count ++; find = strchr(find+1, ch); } } printf("%u", count); return 0; }
#include <stdio.h> #include <string.h> #include <ctype.h> int main() { char str[1002]; char word; //fgets()函数,从文件或标准输入中读取一行文本 fgets(str, sizeof(str), stdin); scanf("%c", &word); int len = strlen(str); int count = 0; for(int i=0; i<len-1; i++) { //tolower()函数,将字母字符转换为小写形式 if(tolower(word) == tolower(str[i])) count++; } printf("%d\n", count); return 0; }
#include <stdio.h> int main() { char str[1000]; gets(str); char a = getchar(); int len = strlen(str); int i; int times= 0; for (i=0;i<len;i++) { if (str[i] == a) { times++; } else if (65<=str[i]&&str[i]<=90&&str[i]==a-32)//小写 { times++; } else if (97<=str[i]&&str[i]<=122&&str[i]==a+32)//大写 { times++; } else { } } printf("%d\n",times); }
#include <stdio.h> #include <string.h> #include <ctype.h> // 1001 -- 通过 12/13 测试用例,1002 -- 通过全部测试用例 (疑惑) #define MAX 1002 int main(int argc, char* argv[]) { int i, count = 0, len; // count 用于统计输入字符串中含有该字符的个数 char temp, str[MAX]; // str[MAX] 数组用于存储字符串 fgets(str, sizeof(str), stdin); // 输入一行字符串 len = strlen(str); // 统计字符串的长度 scanf("%c", &temp); // 第二行输入一个字符 temp = toupper(temp); // 如果 temp 是小写字母, 则 toupper(c) 返回相应的大写字母, 否则返回 temp for (i = 0; i < len - 1; i ++) { char c = toupper(str[i]); // 不区分大小写 if (c == temp) { count ++; } } printf("%d\n", count); return 0; }