写出一个程序,接受一个由字母、数字和空格组成的字符串,和一个字符,然后输出输入字符串中该字符的出现次数。(不区分大小写字母) 数据范围:
区块链毕设网qklbishe.com为您提供问题的解答
import java.uti
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.regex.Pattern; import java.util.regex.Matcher; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //Scanner sc = new Scanner(System.in); String all = br.readLine().toLowerCase(); String ch = br.readLine().toLowerCase(); Pattern compile = Pattern.compile(ch, Pattern.CASE_INSENSITIVE); Matcher matcher = compile.matcher(all); int count = 0; while (matcher.find()) { count++; } System.out.println(count); } }
String ster="ABCabc".toLowerCase();
int count=0;
for(int i=0;i<ster.length();i++){
int index=ster.substring(i,i+1).indexOf("a");
if(index!=-1){
count++;
}
}
System.out.println(count);
}
s=input()
count=0
for i in str:
if i.lower() ==s.lower():
count+=1
print(count)
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); String str = sc.nextLine(); String str2 =sc.next(); char goal = str2.charAt(0); int [] count = new int [128]; int res =0; for(int i =0;i<str.length();i++){ char temp = str.charAt(i); count[temp]++; } if(goal>='a'&&goal<='z'){ res =count[goal-'a'+'A']+count[goal]; }else if(goal>='A' && goal<='Z'){ res = count[goal]+count[goal-'A'+'a']; }else{ res = count[goal]; } System.out.println(res); } }
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s1 = sc.nextLine().toLowerCase();
String s2 = sc.nextLine().toLowerCase();
System.out.print(s1.length() – s1.replaceAll(s2, "").length());
}
}
#include <iostream> #include <string> #include <cctype> #include <algorithm> using namespace std; int main(){ string s; getline(cin, s); char c; cin>>c; c=tolower(c); transform(s.begin(), s.end(), s.begin(), ::tolower); int count=0; for(int i=0;i<s.size();i++){ if(s[i]==c) count++; } cout<<count; return 0; }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); String str = scanner.nextLine().toUpperCase(); //转化为大写 char c = scanner.nextLine().toUpperCase().charAt(0); int count = 0; for(int i=0;i < str.length();i++){ char ch = str.charAt(i); if(ch == c) count++; // //如果忽略大小写相同 没有方法的时候 // if(c >= 'a' && c <= 'z'){ // if(ch == c || ch == (c-('a'-'A'))) count++; // }else{ // if(ch == c || ch == (c+('a'-'A'))) count++; // } } System.out.println(count); } }
package main import ( "strings" "fmt" "bufio" "os" ) func main() { scanner := bufio.NewScanner(os.Stdin) scanner.Scan() str := scanner.Text() scanner.Scan() targetChar := scanner.Text() array := strings.Split(str, "") count := 0 for _, char := range array { if strings.ToLower(char) == strings.ToLower(targetChar) { count ++ } } fmt.Println(count) }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); if (sc.hasNext()){ String str = sc.nextLine(); String s = sc.nextLine(); String STR = str.toUpperCase(); String replace = STR.replaceAll(s.toUpperCase(), ""); System.out.println(str.length()-replace.length()); } } }
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String one = scanner.nextLine().toLowerCase();
String two = scanner.next().toLowerCase();
int count=0;
for (int i = 0; i < one.length(); i++) {
char c = one.charAt(i);
boolean b = two.equalsIgnoreCase(String.valueOf(one.charAt(i)));
if (b){
count=count+1;
}
}
System.out.println(count);
}
}
根据题意,统计某字符target在输入字符串input中的出现频次,同时忽略大小写。为了忽略大小写,可以先将input和target分别转换为大写格式,或者小写格式,保持一致即可。然后,对input的组成字符进行遍历,如果当前字符与target相同,则令计数器count加一。直到遍历结束,count即input中target的出现次数。代码如下所示。
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { String input = in.nextLine().toLowerCase(); String target = in.nextLine().toLowerCase(); System.out.println(countOfTargetChar(input, target)); } } private static int countOfTargetChar(String input, String target) { //统计字符target在字符串input中的出现次数 int count = 0; char temp = target.charAt(0); for (int i = 0; i < input.length(); i++) { if (input.charAt(i) == temp) count++; } return count; } }
#include<stdio.h> #include<string.h> int count(char *str,char key){ int count=0; int len=strlen(str); for(int i=0;i<len;i++){ if(key==' '||(key>='0'&&key<='9')){ if(str[i]==key)count++; }else if(key>='a'&&key<='z'){ if(str[i]==key||str[i]==key-32)count++; }else if(key>='A'&&key<='Z'){ if(str[i]==key||str[i]==key+32)count++; }else count=-1; } return count; } int main() { char str[1024]; scanf("%[^n]",str); scanf("%*[^n]");scanf("%*c"); char key; scanf("%c",&key); int t=count(str,key); printf("%dn",t); return 0; }
#include <iostream> using namespace std; int main() { string str; char c; getline(cin, str); cin >> c; int count[128] = {0}; for (int i=0; i<str.size(); i++) { count[str[i]]++; } if (c >= 65 && c <= 90) { cout << count[c] + count[c+32] << endl; } else if (c >= 97 && c <= 122) { cout << count[c] + count[c-32] << endl; } else{ cout << count[c] << endl; } }
#include<stdio.h> #include<string.h> int main(){ char s[1001],c; gets(s); //注:scanf()不能接受带空格的字符串 { int count=0,len=strlen(s); scanf("%c",&c); for(int i=0;i<len;i++) //判断,注意不区分大小写。 if(s[i]==c||(s[i]==c+32&&s[i]>='a'&&c<='Z')||(s[i]==c-32&&s[i]<='Z'&&c>='a')) count++; printf("%dn",count); } }
import java.util.*; public class Main{ public static void main(String[] arg){ Scanner sc = new Scanner(System.in); String words = sc.nextLine().toLowerCase(); char CHA = sc.nextLine().toLowerCase().charAt(0);//为啥要取0 char[] word = words.toCharArray(); int i = 0; for(char c : word){ if(c == CHA){i++;} } System.out.println(i); } }
#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; }
const char = readline()
const fn = function (str, char) {
// 转换小写
const lowerStr = str.toLocaleLowerCase()
const lowerChar = char.toLocaleLowerCase()
// 使用char来分割字符串,查看得到数组的长度 – 1 就是char出现的次数
const arr = lowerStr.split(lowerChar)
return arr.length – 1
}
console.log(fn(str,char))
letter = input().lower()
count = 0
for i in str:
if i == letter:
count+=1
print(count)
#include<iostream> #include<string> using namespace std; int main() { string str; char c; while(getline(cin,str)&&cin>>c) { //hash表 int hash[128]={0}; for(int i=0;i<str.size();i++) { if(str[i]>='A'&&str[i]<='Z') { hash[str[i]+32]++; } else hash[str[i]]++; } if(c>='A'&&c<='Z') { cout<<hash[c+32]<<endl; } else cout<<hash[c]<<endl; } return 0; }
以上就是关于问题写出一个程序,接受一个由字母、数字和空格组成的字符串,和一个字符,然后输出输入字符串中该字符的出现次数。(不区分大小写字母)
数据范围:的答案
欢迎关注区块链毕设网-
专业区块链毕业设计成品源码,定制。
区块链NFT链游项目方科学家脚本开发培训