Use string methods to find and count vowels in a string?

前端 未结 9 2061
青春惊慌失措
青春惊慌失措 2020-12-02 02:04

I have this problem for homework (I\'m being honest, not trying to hide it at least) And I\'m having problems figuring out how to do it.

Given the following declarat

相关标签:
9条回答
  • 2020-12-02 02:21

    To Count number of Vowels in a String:

    class Test {  
    
          static int a;
    
          public static String countVowel(String strName) { 
    
              char ch[] = strName.toCharArray();
    
    
              for(int i=0; i<ch.length; i++) {
    
                switch(ch[i]) {
    
                case 'a':
    
                 a++;   
    
                break;      
    
                case 'e':
                    a++;    
    
                    break;
    
                case 'i':
                     a++;   
    
                    break;
    
                case 'o':
                     a++;   
    
                    break;
    
                case 'u':
                     a++;   
    
                    break;
    
                }
    
              }
    
              strName = String.valueOf(a);
    
              return strName;       
          } 
    
          public static void main(String[] args) {  
    
            Scanner s = new Scanner(System.in);
            System.out.print(countVowel(s.nextLine())); 
         }   
    
      } 
    
    0 讨论(0)
  • 2020-12-02 02:26

    I know that's an old edit , but i think it's better if you use and array for the vowels :

    public static void main(String[] args) {
    
        String phrase = " WazzUp ? - Who's On FIRST ??? - IDUNNO".toLowerCase();
        int vowels = 0;
        String[] array = {"a", "e", "i", "o", "u"};
        for (int i = 0; i < phrase.length(); i++) {
            String j = phrase.substring(i, i + 1);
            System.out.println(j);
            for (int n = 0; n < array.length; n++) {
                if (j.equals(array[n])) {
                    vowels++;
                }
            }
        }
        System.out.println("Number of vowels: " + vowels);
    }
    
    0 讨论(0)
  • 2020-12-02 02:28

    Improving on an above answer to consider vowels in caps:

    System.out.println(s.length() - s.toLowerCase().replaceAll("a|e|i|o|u|", "").length());
    
    0 讨论(0)
  • 2020-12-02 02:30

    I don't see the need to have a print statement in the loop.

    String s = "Whatever you want it to be.".toLowerCase();
    int vowelCount = 0;
    for (int i = 0, i < s.length(); ++i) {
        switch(s.charAt(i)) {
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':
                vowelCount++;
                break;
            default:
                // do nothing
        }
    }
    

    This converts the string to lowercase, and checks all the characters in the string for vowels.

    0 讨论(0)
  • 2020-12-02 02:30

    The i++ increments i after is has been used, so you're essentially saying string.substring(i,i). Since the ending mark is exclusive, this will always return an empty string. An easy fix would be to just change it to

    j = phrase.substring(i, ++i);
    

    Hope that helps!

    0 讨论(0)
  • 2020-12-02 02:33

    public class JavaApplication2 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        // TODO code application logic here
        System.out.println("Enter some text");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input = br.readLine().toLowerCase();
    
        char[] vowel = new char[]{'a', 'e', 'i', 'o', 'u'};
        int[] countVowel = new int[5];
        for (int j = 0; j < input.length(); j++) {
            char c =input.charAt(j);
            if(c=='a')
                countVowel[0]++;
            else if(c=='e')
                countVowel[1]++;
            else if(c=='i')
                countVowel[2]++;
            else if(c=='o')
                countVowel[3]++;
            else if(c=='u')
                countVowel[4]++;
    
    
        }
         for (int i = 0; i <countVowel.length; i++) {
                System.out.println("Count of vowel " + vowel[i] + "=" + countVowel[i]);
            }
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题