Count letters in a string Java

前端 未结 5 1328
一向
一向 2020-12-10 09:33

I\'m doing an assignment where I\'ll have to code a program to read in a string from user and print out the letters in the string with number of occurrences. E.g. \"Hell

相关标签:
5条回答
  • 2020-12-10 09:56

    No you should not create an array of 26. This will break if the string contains unexpected characters. (ä, ö, ü anyone?) As I pointed out im my comment use a Map. This will work forr all posible characters out there.

    0 讨论(0)
  • 2020-12-10 09:57

    You don't need 26 switch cases. Just use simple code to count letter:

        String input = userInput.toLowerCase();// Make your input toLowerCase.
        int[] alphabetArray = new int[26];
        for ( int i = 0; i < input.length(); i++ ) {
             char ch=  input.charAt(i);
             int value = (int) ch;
             if (value >= 97 && value <= 122){
             alphabetArray[ch-'a']++;
            }
        }
    

    After done count operation, than show your result as:

     for (int i = 0; i < alphabetArray.length; i++) {
          if(alphabetArray[i]>0){
            char ch = (char) (i+97);
            System.out.println(ch +"  : "+alphabetArray[i]);   //Show the result.
          }         
     }
    
    0 讨论(0)
  • 2020-12-10 09:58

    You can create an Array which first element will represent 'a', second 'b', etc. If you need distinction between lower and upper cases than you can add it at the end. This array will have all values equals 0 at the beginning. Then you iterate through your sentence and you increment required values on the array. At the end you print all values that are > 0. Simple?

    Let me know if you need more help

    0 讨论(0)
  • 2020-12-10 10:01

    import java.io.*;

    public class CharCount {

    public static void main(String[] args) throws IOException
    {
        int i,j=0,repeat=0;
        String output="",input;
        char c=' ';
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("enter name ");
        input=br.readLine();
        System.out.println("entered String ->\""+input+"\"");
        input=input.toLowerCase();
    
        for(i=0;i<input.length();i++)
        {
            for(j=0;j<output.length();j++)
            {
                if(input.charAt(i)==output.charAt(j) || input.charAt(i)==c)
                {
                    repeat=1;
                    break;
                }
            }
            if(repeat!=1)
            {
                output=output+input.charAt(i);
            }
            repeat=0;
        }
    
        System.out.println("non-reepeated chars in name ->\""+output+"\"");
    
        int count[]=new int[output.length()];
        for(i=0;i<output.length();i++)
        {
            for(j=0;j<input.length();j++)
            {
                if(output.charAt(i)==input.charAt(j))
                    count[i]=count[i]+1;
            }
        }
        for(i=0;i<output.length();i++)
            System.out.println(output.charAt(i)+"- "+count[i]);
    }
    

    }

    0 讨论(0)
  • 2020-12-10 10:04
    • Create an integer array of length 26.
    • Iterate each character of the string, incrementing the value stored in the array associated with each character.
    • The index in the array for each character is calculated by x - 'a' for lower case characters and x - 'A' for upper case characters, where x is the particular character.
    0 讨论(0)
提交回复
热议问题