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

前端 未结 9 2417

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-06 23:43

    Here is the simple code for counting the number of vowels using recursion

     public static int vowels(String s){
            int count =0;
            char c;
            if(s.length()==0){
                return 0;
            }
            else{
                c =s.charAt(0);
                if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'){
                    count++;
                }
                return count+vowels(s.substring(1));
    
    
            }
        }
    
    0 讨论(0)
  • 2020-12-06 23:44

    This looks way simple than above answers. It gets the input, converts it to lowercase then to an array of characters. A simple for loop will do the trick onwards.

    import java.util.*;
    
    public class FindVowelsConsonents {
    
      public static void main(String[] args) {
        int vowels_count = 0;
        int consonents_count = 0;
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        String str2 = str.toLowerCase();
        char[] chr = str2.toCharArray();
    
        for(int i=0;i<chr.length;i++){
            if(chr[i] == 'a' || chr[i]== 'e' || chr[i] == 'i' || 
                                  chr[i] == 'o' || chr[i] == 'u')
                vowels_count++;
            else
                consonents_count++;
        }
    
        System.out.println(vowels_count+ " "+consonents_count); 
        sc.close();
       }
    }
    
    0 讨论(0)
  • 2020-12-06 23:44
    import java.util.Scanner;
    
    //don't use space in between the input string;
    
     class StrRev {
    
        static String Vowels ="aeiouAEIOU";
    
        public static void main(String[] args){
             @SuppressWarnings("resource")
            Scanner name = new Scanner(System.in);
             String nm = name.nextLine();
    
             System.out.println();
             int vowel=0;
             int consonant=0;
            for(int i=0;i<nm.length();i++){
                for(int j=0;j<Vowels.length();j++){
                    if(Vowels.charAt(j)==nm.charAt(i)){
                        vowel++;
                        break;
                    }
                }
            }
            consonant = nm.length()-vowel;
            System.out.println("no of Vowels :"+vowel+"\nno of consonant :"+consonant);
         }
    
    }
    
    0 讨论(0)
  • 2020-12-06 23:45

    I suggest you;

    • to create a final vowels array,
    • define a bool isVowel(char c) function and use it in your if condition.
    0 讨论(0)
  • 2020-12-06 23:48

    following code will give you vowel and Constonent count

    static String VOWEL_GROUP = "AEIOUaeiou";
    static String testString = "AAAASHMAIOUAXCCDIOUGGGGA"; // say this is your text
    
    public static void main(String[] args) {
        int vovelCount = 0;
         int consonantCount = 0;
        for (int j = testString.length() - 1; j >= 0; j--) {//outer loop 
            for (int i = 0; i < VOWEL_GROUP.length(); i++) { //inner loop
                if (VOWEL_GROUP.charAt(i) == testString.charAt(j)) {
                    vovelCount++; //vowel count in text
                    break;
                }else{
                    consonantCount ++;
                }
            }
        }
        System.out.println(vovelCount+" "+ consonantCount);
    }
    
    0 讨论(0)
  • 2020-12-06 23:52

    As far as I am concerned you can use StringTokenizer:

        String text = "dupalo twoja mama";
        StringTokenizer tokenizer = new StringTokenizer(text,"aeiuo",false);
        int vowels = tokenizer.countTokens();
        System.out.println(vowels);
    

    In this case of "text" it will print out 7.

    0 讨论(0)
提交回复
热议问题