Skip to main content

Command Palette

Search for a command to run...

Java String Coding problems

Published
1 min readView as Markdown
R
Enthusiatic Software Developer with over 4.11 years of experience in building and optimizing Web applications. Proficient in backend development using Java, Spring Boot, Monolithic and Microservices architecture within agile methodologies.

Problem 1: Count Vowels and Consonants in given string

package org.example.stropts;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class CountVowelsAndConsonantsOfString {
    public static void main(String[] args) {
        String str = "Hello world";
        Set<Character> vowelSet = new HashSet<>(Arrays.asList('a','e','i','o','u'));
        str = str.toLowerCase();
        int vowelCount = 0;
        int consCount = 0;
        for (int i = 0; i < str.length(); i++){
            if(vowelSet.contains(str.charAt(i))){
                vowelCount++;
            }
            else {
                consCount++;
            }
        }
        System.out.println("vowels: " + vowelCount);
        System.out.println("Consonants: " + consCount);
    }
}
package org.example.stropts;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class CountVowelsAndConsonantsOfString {
    public static void main(String[] args) {
        String str = "Hello world";
        Set<Character> vowelSet = new HashSet<>(Arrays.asList('a','e','i','o','u'));
        str = str.toLowerCase();
        Predicate<Character> isVowel = ch -> vowelSet.contains(ch);
        long vowelCount = str.chars().filter();;
        int consCount = 0;

        System.out.println("vowels: " + vowelCount);
        System.out.println("Consonants: " + consCount);
    }
}