写出一个程序,接受一个由字母、数字和空格组成的字符串,和一个字符,然后输出输入字符串中该字符的出现次数。(不区分大小写字母)
数据范围:
#include <stdio.h> #include <string.h> int charToAscii(char ch) { return (int)ch; } int main() { int len, count[256]={0}, ch_ascii; char str[1000],ch,c; scanf("%[^\n] %c", str, &ch); if(ch >= 'A' && ch <= 'Z'){ ch += 32; } len = strlen(str); ch_ascii = charToAscii(ch); for(int i=0;i<len;i++){ c = str[i]; if (c >= 'A' && c <= 'Z'){ c += 32; } str[i] = c; count[str[i]]++; } printf("%d",count[ch_ascii]); return 0; }
#include <stdio.h> #include <string.h> #include <ctype.h> int main() { char str[1000] = {0}; char ch; char ch2; int cnt = 0; int i = 0; scanf("%[^\n]\n", str); ch = getchar(); //scanf("%c\n", &ch); ch2 = toupper(ch); for (i = 0; i < strlen(str); i++) { if (str[i] == ch2 + 32) { str[i] = toupper(str[i]); } } for (i = 0; i < strlen(str); i++) { if (ch2 == str[i]) { cnt++; } } printf("%d", cnt); return 0; }
#include "stdio.h" int main(){ char ch; char str[1000]; int count=0,length=0; ch=getchar(); while(ch!='\n'){ str[length++]=ch; ch=getchar(); } scanf("%c",&ch); for(int i=0;i<length;i++){ if(str[i]>='A'&&str[i]<='Z'&&str[i]==ch-32) count++; else if(str[i]>='a'&&str[i]<='z'&&str[i]==ch+32) count++; else if(str[i]==ch) count++; } printf("%d",count); return 0; }
#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; }