How to count vowels and consonants and capitalizing first letter in a string while output using a method and return result

前端 未结 9 2418

If a user enters a string: hello there

it should output

Hello has 2 vowels
There has 3 consonants.

I know this is a f

相关标签:
9条回答
  • 2020-12-07 00:01

    Made something like this hope this helps, this will give vowels,consonants of each word

    public static void main (String args[]) 
            {
                    Scanner stdin = new Scanner(System.in);
                    String string1;
                    System.out.println("Enter a string");
                    string1 = stdin.nextLine();
                    string1 = string1.toLowerCase();
                    int count = 0;
                    int vowels = 0;
                    int consonants = 0;
                    for (String retval: string1.split(" ")){
                         for (int i = 0; i < retval.length(); i++)
                    {
                            char ch = retval.charAt(i);
                            if (ch == 'a' || ch == 'e' || ch == 'i' || 
                                            ch == 'o' || ch == 'u')
                            {
                                    vowels++;
                            }
                            else
                            { 
                                    consonants++;
                            }
                    }
                System.out.println(retval.substring(0, 1).toUpperCase() + retval.substring(1)+" has "+vowels+" vowels and "+consonants+" cosonants");
             vowels=0;
             consonants=0;
          }
    
            }
    
    0 讨论(0)
  • 2020-12-07 00:05

    Here is a simpler way of doing this, hope this helps:

    public static void checkVowels(String s){
        System.out.println("Vowel Count: " + (s.length() - s.toLowerCase().replaceAll("a|e|i|o|u|", "").length()));
        //Also eliminating spaces, if any for the consonant count
        System.out.println("Consonant Count: " + (s.toLowerCase().replaceAll("a|e|i|o| |u", "").length()));
    }
    
    0 讨论(0)
  • 2020-12-07 00:10

    My code without scanner and maybe not very simple, but:

    public class Main{
    public static void main(String[] args) {
    
        String test = "qwertyYVTA";
        test = test.trim();
        int vowels = 0;
        int consonants = 0;
    
        Pattern patternVow = Pattern.compile("[eyuioa]", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
        Matcher matcherVow = patternVow.matcher(test);
        while (matcherVow.find()) {
            vowels++;
        }
    
        Pattern patternCons = Pattern.compile("[wrtpsdfghjklzxcvbnm]", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
        Matcher matcherCons = patternCons.matcher(test);
        while (matcherCons.find()) {
            consonants++;
        }
        System.out.println("Vowels in test String is " + vowels);
        System.out.println("Consonants in test String is " + consonants);
        }
    }
    
    0 讨论(0)
提交回复
热议问题