首页 > 试题广场 >

计算某字符出现次数

[编程题]计算某字符出现次数
  • 热度指数:1302484 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
写出一个程序,接受一个由字母、数字和空格组成的字符串,和一个字符,然后输出输入字符串中该字符的出现次数。(不区分大小写字母)

数据范围:

输入描述:

第一行输入一个由字母、数字和空格组成的字符串,第二行输入一个字符(保证该字符不为空格)。



输出描述:

输出输入字符串中含有该字符的个数。(不区分大小写字母)

示例1

输入

ABCabc
A

输出

2
推荐
import java.util.*;
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner s=new Scanner(System.in);
        String all="";
        String one="";
        char[] ac;
        char temp;
        int num=0;
        while(s.hasNext())
        {
            //s.toUpperCase(),String 转化为大写
            //s.toLowerCase(),String 转化为小写
            //String字符转换,s.toCharArray()与s.charAt(index)
            //char字符转换,String.valueOf(c)转化为String
        	all=s.nextLine();
            one=s.nextLine();
            //存放原来所有的
            ac=all.toCharArray();
            //存放要的字符
            //temp=one.charAt(0);
            for(int i=0;i<ac.length;i++)
            {
            	if(one.equalsIgnoreCase(String.valueOf(ac[i])))    
                    num++;
            }
            System.out.println(num);
        }
        
    }
    
}

编辑于 2017-03-04 16:05:10 回复(45)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String line = in.nextLine();
        String matchStr = in.nextLine();
        char matchCharLowerCase = matchStr.toLowerCase().toCharArray()[0];
        char matchCharUpperCase = matchStr.toUpperCase().toCharArray()[0];
        char[] chars =  line.toCharArray();
        int result = 0;
        for(int i=0;i<chars.length;i++ ){
            if( (chars[i] == matchCharLowerCase) ||
             (chars[i] == matchCharUpperCase)){
                result ++;
            }
        }
        System.out.println(result);
    }
}

编辑于 2024-03-27 16:36:35 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String a = in.nextLine();
        String b =in.next();

        int sum=0;
        for(int i=0;i<a.length();i++){
           String temp = ""+a.charAt(i);
           if(temp.equalsIgnoreCase(b)){
            sum=sum+1;
           }
        }
        System.out.println(sum);

}
}


编辑于 2024-03-19 16:35:34 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case

            String s = in.nextLine().toLowerCase();
            String b = in.nextLine().toLowerCase();//一定要加toLowerCase(),否则会出现字符为空客的情况,结果为0;
            //s=s.toLowerCase();

            System.out.println(s.length()-s.replaceAll(b,"").length());
        }
    }
}
import java.util.Scanner;


// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case

            String s = in.nextLine().toLowerCase();
            String b = in.nextLine().toLowerCase();//一定要加toLowerCase(),否则会出现字符为空客的情况,结果为0;
            //s=s.toLowerCase();

            System.out.println(s.length()-s.replaceAll(b,"").length());
        }
    }
}

编辑于 2024-03-18 00:17:31 回复(0)
就没有人是用replaceAll的嘛....

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine().toUpperCase();
        String s = in.nextLine().toUpperCase();
        int i = str.length();
        int j = str.replaceAll(s,"").length();
        System.out.println(i - j);
    }
}
编辑于 2024-03-11 13:18:44 回复(0)
这个哪里有问题啊,在idea里面都能运行
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner a = new Scanner(System.in);

        Scanner b = new Scanner(System.in);

        String str1 = a.nextLine();
        String str2 = b.nextLine();

        if(!str1.isEmpty() && str1.length()<=1000){
            String[] str3 = str1.split("");
            int count = 0;
            for(int i = 0;i < str3.length-1;i++){
                if(str3[i].equals(str2) || str3[i].toUpperCase().equals(str2.toUpperCase())){
                    count++;
                }

            }
            System.out.println(count);
        }

    }
}

编辑于 2024-03-07 14:56:56 回复(1)
虽然做出来了,但感觉用MAP有点多余了。。
import java.util.Scanner; 
import java.util.HashMap;
import java.util.Map;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
 Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        String ch = in.next();
        Map<String, Integer> map = new HashMap<>();
        map.put(ch, 0);
        for(int i = 0; i < str.length(); i++){
            if(map.containsKey(String.valueOf(str.charAt(i)).toLowerCase()) || map.containsKey(String.valueOf(str.charAt(i)).toUpperCase())){
                map.put(ch,map.get(ch) + 1);
            }
        }
        System.out.println(map.get(ch));
    }
}

编辑于 2024-03-07 10:46:03 回复(0)
import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        List<String> strArr = new ArrayList<String>();
        while (in.hasNext()) { // 注意 while 处理多个 case
            String line = in.nextLine();
            strArr.add(line);
        }
        System.out.println(getCodeRepeatNum(strArr));
    }

    /**
    *  将两个 字符串转换为 字符,遍历 第一行字符,看第二行中出现了 几次,之后返回 结果
    */
    public static int getCodeRepeatNum(List<String> list) {
        char [] chars = list.get(0).toUpperCase().toCharArray();
        char code = list.get(1).toUpperCase().toCharArray()[0];
        int num = 0;
        for (char c : chars) {
            if (c == code) {
                num++;
            }
        }
        return num;
    }
}

编辑于 2024-01-27 22:30:38 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        String s = in.nextLine();
        System.out.println(str.replaceAll("(?i)[^"+s+"]", "").length());
    }
}

发表于 2023-11-28 21:05:37 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str =sc.nextLine().toLowerCase();
        String s = sc.nextLine().toLowerCase();
        System.out.print(str.length()-str.replaceAll(s,"").length());
    }
}
发表于 2023-10-29 16:20:51 回复(0)
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
String s = ""; // 全部字符串
while(in.hasNext()) {
s += in.next();
}
char sSecond = s.charAt(s.length() - 1);
String sFirst = ""; // 第一行字符
sFirst = s.substring(0, s.length() - 1);
int i = 0; // 循环下标计数
int j = 0; // 字符重复次数
while(i < sFirst.length()) {
char cFirst = sFirst.charAt(i);
if(cFirst == sSecond ||
(isZimu(sSecond) && (cFirst == sSecond + 32 || cFirst + 32 == sSecond))){
j++;
}
i++;
}
System.out.print(j);
}

public static boolean isZimu(char c) {
return (c >= 65 && c < 90) || (c >= 97 && c <= 122);
}
}
发表于 2023-09-02 02:11:03 回复(0)
public class HJ2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String firstWold = scanner.nextLine();
        String secondWold = scanner.nextLine();

        System.out.println(statisticalFrequency(firstWold, secondWold));
    }

    public static Integer statisticalFrequency(String firstWold, String secondWold) {
        Integer count = 0;
        char[] chars = firstWold.toCharArray();
        for (char aChar : chars) {
            if (secondWold.equalsIgnoreCase(String.valueOf(aChar))) {
                count++;
            }
        }
        return count;
    }
}

发表于 2023-08-28 14:16:01 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        int count = 0;
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        char ch = sc.nextLine().charAt(0);
        char[] array = str.toCharArray();
        for (char c : array) {
            if (c == ch) {
                count++;
            }
        }
        System.out.println(count);
    }
}
发表于 2023-08-18 18:08:07 回复(1)
   public static void main(String[] args) {

    Scanner in = new Scanner(System.in);

    String source_v = in.nextLine().toUpperCase();

    String char_v = in.next().toUpperCase();

    int sum = 0;

    for (int i = 0; i source_v.length(); i++) {

        if (source_v.charAt(i) == char_v.charAt(0)) {

            sum++;

        }

    }

    System.out.println(sum);

}
发表于 2023-08-08 17:00:52 回复(0)
import java.util.Scanner;
import java.util.stream.Stream;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            String[] line1 = scanner.nextLine().split("");
            String line2 = scanner.nextLine();
            long count = Stream.of(line1).filter(t -> t.equalsIgnoreCase(line2)).count();
            System.out.println(count);
    }
}
发表于 2023-08-06 10:15:17 回复(0)
	
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        //输入一个由字母、数字和空格组成的字符串
        //输入一个字符
        Scanner in = new Scanner(System.in);
        String inFistString = in.nextLine();
        if (inFistString.length() < 1) {
            System.out.println("长度小于1,结果!");
        }
        String inTwoString = in.nextLine();
        int cout = 0;
        for (int i = 0; i <= inFistString.length() - 1; i++) {
            char at = inFistString.charAt(i);
            // if (StringUtils.containsIgnoreCase(String.valueOf(at), inTwoString)) {
            //     cout = cout + 1;
            // }
            if (String.valueOf(at).equalsIgnoreCase(inTwoString)) {
                cout = cout + 1;
            }
        }
        System.out.println(cout);
    }
}


发表于 2023-06-27 13:46:42 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String a = in.nextLine();
        String b = in.nextLine();
        int rtn = 0;
        for (int i=0;i<a.length();i++){
            if(a.toLowerCase().charAt(i) == b.toLowerCase().charAt(0)){
                rtn += 1;
            }
        }
        System.out.println(rtn);
    }
}


发表于 2023-05-23 19:13:33 回复(0)
public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String a = in.nextLine();
        String b = in.nextLine();
        int count = 0;
        for (char c : a.toCharArray()){
            if (b.equalsIgnoreCase(String.valueOf(c))){
                count++;
            }
        }
        System.out.println(count);
    }

发表于 2023-05-16 11:05:21 回复(0)
/**
思路很简单,没用到jdk提供的String类的API,在utf-8编码环境下进行的比较
1. 得到输入的字符串的byte数组,进行遍历
2. 每遍历一次就比较两个byte数组中的字符编码值  
2.1 如果两个byte数组中的当前索引对应的字符都是英文字母,则对同一个字母忽略大小写之后再比较它们的字符编码值是否相等,相等就对计数器累加1;
2.2 如果两个byte数组中当前索引对应的字符其中有一个不是英文字母,则直接判断它们两个的字符编码值是否相等,相等就对计数器累加1;
**/


import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String a = in.nextLine();
        String b = in.nextLine();
        if (b.getBytes().length > 1) {
            throw new RuntimeException("第二行中只能输入单个字符");
        }
        int counter = 0;

        byte [] firstStr = a.getBytes();

        //遍历字符串,匹配第二行中输入的字符,每匹配到一次counter+1
        for (int i = 0; i < firstStr.length; i++) {
            byte secondStr = b.getBytes()[0];
            byte firstStrVal = firstStr[i];

            boolean isEquals = (firstStrVal == secondStr);
            //判断第一行的输入和第二行的输入是否都是字母A-z
            boolean isLetter = (firstStrVal >= 65 && firstStrVal <= 122) &&
                               (secondStr >= 65 && secondStr <= 122);
            if (isLetter) {
                //相同的字母忽略大小写进行比较
                boolean isTrue =
                    isEquals || (firstStrVal - secondStr == 32) || (firstStrVal - secondStr == -32);
                if (isTrue) {
                    counter++;
                }
            }
            //第一行和第二行的输入中,其中一行中有非字母的情况
            else {
                if (isEquals) {
                    counter++;
                }
            }
        }

        System.out.println(counter);
    }
}

发表于 2023-05-03 13:46:44 回复(0)