Write a program using Java: accepts a string of letters and numbers, and a character, the number of input and output of the character string contained. not case sensitive.

import java.util.Scanner;
public class Main{
    
    public static int char_length(String s, String c){
        int count = 0;
        for(int i=0; i<s.length(); i++){
            if(s.substring(i,i+1).equalsIgnoreCase(c)){
                count++;
            }
        }
        return count;
    }
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        String s = in.nextLine();
        String c = in.nextLine();
        System.out.println(char_length(s,c));
    }
}

The main methods used in the examples are:
the substring (the beginIndex int, int endIndex) :
This is a method to take a substring of a string, and if the beginindex endindex pitch is set to 1, that can take only a character.
equalsIgnoreCase (String anotherString) :
This is the equals () method compares the character case of neglect;

只要每天积累,一定会有收获!
Released three original articles · won praise 0 · Views 21

Guess you like

Origin blog.csdn.net/weixin_44355975/article/details/104610346