创建一个数字来计算字符串中的元音

时间:2015-10-29 18:21:06

标签: java string for-loop

我想创建一个数组来搜索字符串中的元音 像umm这样的东西

public class letstrythis{
    public static void main(string[]arg){
        String[] myStringArray = [1];
        String[] myStringArray = {"iwanttotryifthisworks"};
        String myStringArray = new String[] {"iwanttotryifthisworks};

        if(string=a,e,i,o,u;
                for (int i=0;i< string.length; i++){
                int intvalue = string[i];
                system.out.println(i);
                )
    }

}

问题,在字符串中搜索元音的功能是什么?

4 个答案:

答案 0 :(得分:1)

迭代数组并在看到元音时增加计数。你可以将所有元音保存在一个集合中。没有内置功能可以做到这一点。

Set<Character> vowels = new HashSet<Character>();

vowels.add('a');vowels.add('e');vowels.add('i');vowels.add('o');vowels.add('u');

String input = new String("any input string");
int count=0;

for (int i=0 ; i<input.size() ; i++)
{
    if(vowels.contains(input.charAt(i)))
      count++;
}

System.out.println(count);

或使用char数组

Set<Character> vowels = new HashSet<Character>();

vowels.add('a');vowels.add('e');vowels.add('i');vowels.add('o');vowels.add('u');

String inp = new String("any input string");

char[] input = inp.toCharArray();

int count=0;

for (int i=0 ; i<input.length ; i++)
{
    if(vowels.contains(input[i]))
      count++;
}

System.out.println(count);

答案 1 :(得分:1)

import java.util.Scanner;

public class Countingvowel {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        char letter;
        String sentence = "";
        System.out.println("Enter a character for which to search");
        letter = in.next().charAt(0);
        System.out.println("Enter the string to search");
        sentence = in.next();

        int count = 0;
        for (int i = 0; i < sentence.length(); i++) {
            char ch = sentence.charAt(i);
            if (ch == letter) {
                count++;
            }
        }
        System.out.printf("There are %d occurrences of %s in %s", count, letter, sentence);

    }
}

答案 2 :(得分:1)

    public class CountVowels {
            public static void main(String[] args) {
                    String string = "this is a test string for counting vowels";
                    int count = 0;
                    for (int i = 0;i < string.length();i++) {
                            if (isVowel(string.charAt(i)))
                                    count++;
                    }
                    System.out.println(count);
            }
            public static boolean isVowel(char ch) {
                    return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');
            }
    }

答案 3 :(得分:1)

如果您不想循环,可以随时使用常规快递。

import java.util.regex.Matcher; import java.util.regex.Pattern;

public class Regex {

    public static void main(String[] args) {
        Pattern p = Pattern.compile("[aeiou]", Pattern.CASE_INSENSITIVE);

        String s = "iwanttotryifthisworks";

        Matcher m = p.matcher(s);

        int i = 0;
        while (m.find()) i++;

        System.out.println("Matches: " + i);
    }

}

m.find()有一个循环,但它不是一堆循环。