Interview Question
Country: Bangladesh
Interview Type: Written Test
suggest me if i can make my code better. Can i do this using enum?
public static void countvowel(String query)
{
String result="";
String[] word=query.split(" ");
for(int i=0;i<word.length;i++)
{
int c=count(word[i]);
if(c>=2)
{
System.out.println(word[i]);
result=result+" "+word[i];
}
}
String[] num=result.split(" ");
int val=num.length-1;
System.out.println("Total number of words which contain more than one vowel : "+val);
}
public static int count(String query)
{
Vowel v = null;
int count=0;
for(int i=0;i<query.length();i++)
{
switch(query.charAt(i))
{
case 'a' :
case 'e' :
case 'i' :
case 'o' :
case 'u' : {count++;
break;}
}
}
return count;
}
package com.test.me;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class FindVowels {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Character> vovelList = new ArrayList<>();
vovelList.add('a');
vovelList.add('e');
vovelList.add('i');
vovelList.add('o');
vovelList.add('u');
ArrayList<String> finalStrin = new ArrayList<>();
BufferedReader bufRead = new BufferedReader(new InputStreamReader(System.in));
try {
String s = bufRead.readLine();
String[] str = s.split(" ");
for(int i=0;i<str.length;i++){
char[] ch =str[i].toCharArray();
int count = 0;
for(int j = 0;j<ch.length;j++){
if(vovelList.contains((Character)ch[j])){
count++;
}
if(count>1){
finalStrin.add(str[i]);
break;
}
}
}
for(String strl: finalStrin){
System.out.print(strl);
System.out.print("\n");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.util.*;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Vowels {
public static void main(String[] args) {
String s;
String n;
char c;
char sp[]={'!','@','#','$','%','^','&','*','(',')'};
char ow[]={'a','e','i','o','u'};
char ch[]=new char[10];
char msp[]=new char[10];
int l,oc=0,nlo=0,mspc=0;
Scanner sc=new Scanner(System.in);
System.out.print("enter the given string");
s=sc.nextLine();
StringTokenizer st=new StringTokenizer(s," ");
while(st.hasMoreElements())
{
n=st.nextElement().toString();
ch=n.toCharArray();
oc=0;
for(int i=0;i<ch.length;i++)
{
for(int j=0;j<5;j++)
{
if(ch[i]==ow[j])
{
oc++;
}
}
for(int k=0;k<sp.length;k++)
{
if(ch[i]==sp[k])
{
msp[mspc]=sp[k];
mspc++;
}
}
}
if(oc>=2)
nlo++;
}
System.out.printf("no of lines contians more than 2 vowels"+nlo);
for(int k=0;k<mspc;k++)
System.out.println("no of special matching characters"+msp[k]);
}
}
sample o/p:enter the given stringhello ram charan@ raki# gani%
no of lines contians more than 2 vowels4no of special matching characters@
no of special matching characters#
no of special matching characters%
package com.test;
- Rajkumari November 25, 2012import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class VowelSpecialChar {
public static void main(String args[]){
String testStr="This is a test sentence, gb good luck.";
String [] arrStr= testStr.split(" ");
Pattern p= Pattern.compile("[aeiou]");
Pattern p2= Pattern.compile("[^a-zA-Z0-9]");
int count=0;
for (int i=0;i<arrStr.length;i++){
Matcher match = p.matcher(arrStr[i]);
if(match.find())
{
count++;
System.out.println(arrStr[i]);
}
}
for (int i=0;i<arrStr.length;i++){
Matcher match2 = p2.matcher(arrStr[i]);
if (match2.find()){
System.out.println("found character: "+match2.group());
}
}
System.out.println("Count words of having vowels is: "+count);
}
}