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

前端 未结 9 2090
青春惊慌失措
青春惊慌失措 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:41

    Using Collections:

         String word = "busiunesuse";
         char[] wordchar= word.toCharArray();
         Character allvowes[] ={'a','e','i','o','u'};
         Map mymap = new HashMap();
    
         for(Character ch: wordchar)
         {
             for(Character vowels : allvowes)
             {
                if(vowels.equals(ch))
                {
                 if(mymap.get(ch)== null)
                 {
                     mymap.put(ch, 1);
                 }
                 else
                 {
                     int val = mymap.get(ch);
                     mymap.put(ch, ++val);
                 }
                }
    
             }
         }
    
         Set  myset = mymap.keySet();
         Iterator myitr = myset.iterator();
    
         while(myitr.hasNext())
         {
             Character key = (Character) myitr.next();
             int value = mymap.get(key);
             System.out.println("word:"+key+"repetiotions:"+value);
    
    
         }
    
    
    }
    

    }

提交回复
热议问题