写出一个程序,接受一个由字母、数字和空格组成的字符串,和一个字符,然后输出输入字符串中该字符的出现次数。(不区分大小写字母)
数据范围:
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> // 判断character是否大写字母 bool is_upper(const char c) { return c >= 65 && c <= 90; } // 判断character是否小写字母 bool is_lower(const char c) { return c >= 97 && c <= 122; } // 判断是不是字母 bool is_letter(const char c) { return is_lower(c) || is_upper(c); } // 大写字母转小写字母 char to_lower(const char c) { // boundary case if (!is_letter(c) || is_lower(c)) return c; return c ^ 1 << 5; } int main(const int argc, const char** argv) { char s[501] = ""; fgets(s, 501, stdin); char ch = to_lower(fgetc(stdin)); int cnt = 0; const char* p = s; while (*p) cnt += to_lower(*p++) == ch; return fprintf(stdout, "%d", cnt), 0; }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner input = new Scanner(System.in); String s = input.nextLine(); String wanted = input.nextLine(); Map<Character,Integer> map = new HashMap<Character,Integer>(); for(int i=0; i<s.length(); i++){ char aim = s.charAt(i); if(map.containsKey(aim)){ int times = map.get(aim) +1; map.put(aim,times); } else{ map.put(aim,1); } }//build a hashmap char wantedChar = wanted.charAt(0); int sumTime = 0; if(wantedChar>='A'&& wantedChar<='Z' && map.containsKey(wantedChar)){ sumTime += map.get(wantedChar); if(map.containsKey((char)(wantedChar+32))){ sumTime += map.get((char)(wantedChar+32)); } }else if(wantedChar>='a' && wantedChar<='z' && map.containsKey(wantedChar)){ sumTime += map.get(wantedChar); if(map.containsKey((char)(wantedChar-32))){ sumTime += map.get((char)(wantedChar-32)); } }else if(wantedChar>='0' && wantedChar<='9' && map.containsKey(wantedChar)){ sumTime += map.get(wantedChar); } System.out.println(sumTime); } }用了HashMap
#include <iostream> #include <string> using namespace std; int main() { string input; char s,m; getline(cin, input); cin>>s; m=s; if('A'<=s<='z') { if('A'<=s&&s<='Z') m=s+32; else if('z'>=s&&s>='a') m=s-32; int count=0; int length=input.size(); for(int i=0;i<length;i++) { if(input[i]==s||input[i]==m) { count++; } } cout<<count<<endl; } return 0; }
#include <stdio.h> int main(void) { char s[1000] = {0}, ch, *p = s - 1; int total = 0; gets(s), scanf("%c\n", &ch); if (ch >= 'A' && ch <= 'Z') ch -= 'A' - 'a'; while (*(++p) != '\0') if (*p == ch || *p == ch + 'A' - 'a') total++; return printf("%d\n", total), 0; }
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { String line = scanner.nextLine().toLowerCase(); int count = 0; if (line.matches("[0-9A-Za-z].*")) { for (int i = 0; i < 1; i++) { String charStr = scanner.nextLine(); if (charStr.length() != 1) { break; } char[] charArr = line.toCharArray(); char findCharArr = charStr.toLowerCase().toCharArray()[0]; for (int j = 0; j < charArr.length; j++) { if (charArr[j] == findCharArr) { count++; } } } System.out.println(count); } } }
/* 注意大小写转换*/ #include<iostream> (720)#include<string> using namespace std; int main(){ string input1,input2,input3; int k=0; while(cin>>input1&&cin>>input2); if('a'<=input2[0]&&'z'>=input2[0]) input3=input2[0]-32; if('A'<=input2[0]&&'Z'>=input2[0]) input3=input2[0]+32; for(int i=0;i<input1.length();i++){ if(input2[0]==input1[i]||input3[0]==input1[i]) ++k; } cout<<k<<endl; return 0; }
#include <iostream> #include <string> using namespace std; int main(void) { string s1; char s2; int num=0,i=0,j=0; getline(cin,s1); cin >> s2; num=s1.size(); for ( i = 0; i < num; i++) { if (s1[i]>='A' && s1[i]<='Z') { s1[i] = s1[i] + 32; } if (s2>='A' && s2<='Z') { s2 = s2 + 32; } if (s1[i] == s2 ) { j++; } } cout <<j <<endl; return 0; }
import java.util.Scanner; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String str=sc.next().toUpperCase(); String s=sc.next().toUpperCase(); List<String> list=new ArrayList<String>(); Collections.addAll(list, str.split("")); int a=list.size(); while(list.contains(s)) { list.remove(s); } System.out.println(a-list.size()); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String strIn = scanner.nextLine().toLowerCase(); char ch = scanner.next().toLowerCase().charAt(0); int num = calCharNum(strIn, ch); System.out.println(num); } public static int calCharNum(String str, char ch) { char[] strArray = str.toCharArray(); int num = 0; for (char tmp : strArray) { if (tmp == ch) { num++; } } return num; } }
var readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); var lines = []; var countLine=1; rl.on("line", function(line) { //将输入放到新建的空数组中 lines.push(line); if(countLine==2){ var str=lines[0].trim().toLowerCase(); var c=lines[1].trim().toLowerCase(); var count=0; var index=0; while(str.indexOf(c,index)!==-1&&index<str.length){ index=str.indexOf(c,index)+1; count++; } countLine=1; lines=[]; console.log(count); } else{ countLine++; } });
#include<iostream>
#include<string>
using namespace std;
int main()
{
int i = 0;
string str;
char a;
cin >> str;
cin >> a;
/因为不区分大小写,所以先全部统一成大写,再进行比较/
//将str和a都转换成大写
for (auto &s : str)
s=toupper(s);
a = toupper(a);
/用范围for循环将str的每个字符与a进行比较
,与a相等则i+1/
for (auto s : str)
{
if (s == a)
i++;
}
cout << i << endl;//输出i;
return 0;
}
#include<iostream> #include<string> #include<cmath> using namespace std; int PrintCharNum(string &str, char c) { int count = 0; for (int i = 0; i<str.size(); i++) { if (abs(str[i] - c) == 0 || abs(str[i] - c) == 32) count++; } return count; } int main() { string str; char c; getline(cin, str); cin >> c; cout << PrintCharNum(str, c) << endl; return 0; }大小写字母相差32,可以用其差的绝对值来进行条件判定。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int sum;
int main()
{
string a;
char b;
cin>>a>>b;
int len=a.length();
while(len--)
if(a[len]==b||a[len]==b+32||a[len]==b-32)sum++;
cout<<sum;
return 0;
}
1).前面的buf数组获取的是空格前的的字符串,并将字符串转换为小写再转换成字符数组 2).后面的字符变量c存储的是空格后面的单个字符,也转换成小写 3).然后遍历字符数组,如果字符数组中的字符和字符c相等,计数器count+1,最后输出计数器的值 import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner in = new Scanner(System.in); char[] buf=in.next().toLowerCase().toCharArray(); char c=in.next().toLowerCase().toCharArray()[0]; int count=0; for(int i=0;i<buf.length;i++) { if(buf[i]==c) count++; } System.out.println(count); } }
#include <iostream> #include <string> using namespace std; int main() { string str; getline(cin,str); char end = 0; cin>>end; //换行输入,那么最后一个字符就是end int len=str.size(); char End=str[len-1];//没有换行输入的最后一个字符 int num=0; for(int i=0;i<len-1;++i) { if(end !=0) //有换行输入 { if(end>='a') { if(str[i]==end || str[i]==end-32) num++; } else { if(str[i]==end || str[i]==end+32) num++; } } else //输入只有一行 { if(End>='a') { if(str[i]==End || str[i]==End-32) num++; } else { if(str[i]==End || str[i]==End+32) num++; } } } cout<<num; return 0; }