Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Sunday, January 10, 2016

How do I find vowels in a string, and print the word with the most vowels on the screen?

How do I find vowels in a string, and print the word with the most vowels on the screen?


I need to find all vowels in a user inputted string, and then print the word with the most vowels on the screen.
The program uses user input.
The user types in a string of words in lowercase.
e.g.

"I like java programming"

and it should read out:

programming

I tried splitting the string into different words, and that works.
I only don't know how to apply a "for" loop to search in the different words. I need to work in methods, so this is the method I used to find vowels in the string:

public void findvowel(){      for(int index = 0;index < word.length();index++){      char vowel = word.charAt(index);      if( (vowel == 'a')||          (vowel == 'e')||          (vowel == 'i')||          (vowel == 'o')||          (vowel == 'u')){              System.out.println(vowel);              }          }      }  

But I know this doesn't work. Can you people help me?

Answer by Elliott Frisch for How do I find vowels in a string, and print the word with the most vowels on the screen?


This should be a good starting point; note that the method name now really says what it does -

// public static int findvowel(String word) {  public static int getVowelCount(String word) {    int count = 0;    if (word != null) {      word = word.trim().toLowerCase();    }    if (word == null || word.length() < 1) {      return count;    }    for (int index = 0; index < word.length(); index++) {      // That Fred he's a      char fred = word.charAt(index);      if ((fred == 'a') || (fred == 'e')          || (fred == 'i') || (fred == 'o')          || (fred == 'u')) {        ++count;        }    }    System.out.println("For the word \"" + word        + "\" there are " + count + " vowels");    return count;  }  

Answer by Little Child for How do I find vowels in a string, and print the word with the most vowels on the screen?


public class MaxVowels {      public static void main(String[] args) {          String sentence = "This is a loooooooooong sentence";          int maxVowelCount = 0;          String wordsWithMostVowels = null;          String[] words = sentence.split(" ");            for(String word : words){              int vowelCount = 0;              word = word.toLowerCase();              for(int i = 0; i < word.length() ; i++){                  char x = word.charAt(i);                  if(x == 'a' || x == 'e' || x == 'i' ||                     x == 'o' || x == 'u'){                      vowelCount++;                  }              }              if(vowelCount > maxVowelCount){                  maxVowelCount = vowelCount;                  wordsWithMostVowels = word;              }          }          System.out.println("Word with most vowels is: " + wordsWithMostVowels);      }  }    

The code is fairly straightforward and needs no explanation =)
The code ignores the case where two words have the same number of vowels. In this case, the first word will be used as the word with most vowels.

Answer by Nishant Shreshth for How do I find vowels in a string, and print the word with the most vowels on the screen?


You're going in the right direction. Few things:

You don't need to print vowels. You will be counting number of vowels in all the words. As you will be doing it one word at a time, you want to remember counts for words

that came earlier. A better strategy is to remember only the word having maximum number of vowels. Whenever you find a word with more vowels, you update your result.

You can use fields to remember the word with maximum number of vowels along with the number:

String wordWithMaxVowels;  int maxNumberOfVowels;  

Suppose at this instance you're working on a single word. You need a local variable to keep a count of vowels in this word.

int vowelCount = 0;  // Your code to loop over the word but remember  // to increase vowelCount if you find a vowel:  // vowelCount++;  

Finally the check to see if this number is greater than the maximum we have so far. Update the fields if that's the case:

if(vowelCount > maxNumberOfVowels) {      wordWithMaxVowels = word;      maxNumberOfVowels = vowelCount;  }  

Another tip follows. To check if a character c is a vowel, you can:

if ("aeiouAEIOU".indexOf(c) != -1) {      vowelCount++;  }  

Answer by frankie liuzzi for How do I find vowels in a string, and print the word with the most vowels on the screen?


Your findVowel() method is almost there. Why are you outputting the vowels when you're supposed to count them instead? Instead of findVowel(), i think you want something called countVowels() that returns the amount of vowels in a word. Something like this:

    public int countVowels(String word){        int count = 0;        for(int index = 0;index < word.length();index++){          char vowel = word.charAt(index);          if( (vowel == 'a')||                  (vowel == 'e')||                  (vowel == 'i')||                  (vowel == 'o')||                  (vowel == 'u')){              count++;          }        }        return count;      }  

this way, you can call countVowels() on every word in the sentence, and keep track of the word with the most vowels so far. Ex:

String sentence = "This is a sentence.";  String[] words = sentence.split(" ");  //splits sentence into string array by spaces    String maxStringSoFar = "";  int maxStringVowelCount = 0;  for(String s : words)  {       int currentWordVowelCount = countVowels(s);       if(currentWordVowelCount > maxStringVowelCount)       {            maxStringSoFar = s;            maxStringVowelCount = currentWordVowelCount;       }  }  

Answer by Nitika Goswami for How do I find vowels in a string, and print the word with the most vowels on the screen?


Hey I have some Different answer-----

class strDemo2  {      public static void main(String args[])      {      String s1=new String("The Ghost of The Arabean Sea");              char c1[]=new char[30];          char c2[]={'a','e','i','o','u'};            s1.getChars(0,28,c1,0);           int pf=0,pl=0;          for(int i=0;i "+(i+1)+" which is "+c2[j]);                }                 }            }                      }  }  

Answer by Harsh Raj for How do I find vowels in a string, and print the word with the most vowels on the screen?


public class Test2{  public static void main(String[] args) {      String message = "words containig mooooost vowels";      String wordWithMostVowel = null;      int maxVowelCount = 0;      String tests[] = message.split(" ");      int totalVowel = 0;      for(String test : tests){          int vowelCount = 0;          test = test.toLowerCase();          for(int i=0;i maxVowelCount){                  maxVowelCount = vowelCount;                  wordWithMostVowel = test;              }          }          totalVowel = totalVowel+vowelCount;      }      System.out.println("total vowels "+totalVowel+" word with max vowel is "+wordWithMostVowel);  }  

}


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

Related Posts:

0 comments:

Post a Comment

Popular Posts

Fun Page

Powered by Blogger.