String programs in Java

Nethula GaneshNethula Ganesh
4 min read

Comprehensive List of String Programming Questions


Basic Questions

  1. Reverse a String

  2. Check if a String is a Palindrome

  3. Count the Number of Vowels and Consonants

  4. Count the Occurrence of Each Character in a String

  5. Find the First Non-Repeating Character in a String

  6. Remove All Whitespaces from a String

  7. Check if Two Strings are Anagrams

Intermediate Questions

  1. Find the Longest Word in a Sentence

  2. Count Occurrences of a Substring

  3. Replace All Vowels in a String with a Specific Character

  4. Check if a String Contains Only Digits

  5. Check if a String is a Rotation of Another String

  6. Convert a Sentence to Title Case

  7. Find the Most Frequent Word in a Sentence

Advanced Questions

  1. Implement Basic String Compression (e.g., "aaabb" becomes "a3b2")

  2. Find the Longest Palindromic Substring

  3. Find the Longest Common Prefix Among a Set of Strings

  4. Implement a Basic Pattern Matching Algorithm (Substring Search)

  5. Generate All Permutations of a String

  6. Implement the KMP Algorithm for Pattern Matching

  7. Find the Smallest Window in a String Containing All Characters of Another String

public class StringExample {
    public static void main ( String[] args ) {

    }

    public static String reverse ( String string) {
//        Null checking
        if(string == null){
            return string;
        }
        String reverse = "";
        for ( int i = string.length () - 1; i >= 0 ; i-- ) {
            reverse += string.charAt ( i );
        }
        return reverse;
    }
    //Count the vowels in the given string
    public static int countVowels(String str) {
        int count = 0;
        for (int i = 0; i< str.length () -1 ; i++) {
            if (str.charAt ( i ) == 'a' || str.charAt ( i ) == 'e' || str.charAt ( i ) == 'i' || str.charAt ( i ) == 'o' || str.charAt ( i ) == 'u') {
                count++;
            }
        }
        return count;
    }
    // 6. Count the occurrences of a character in the given string
    public static int countOccurrences(String str, char ch) {
        int count = 0;
        for ( int i = 0; i < str.length () - 1; i++ ) {
            if ( str.charAt ( i ) == ch ){
                count++;
            }
        }
        return count;
    }
    public static String expandString(String input) {
        if (input == null || input.isEmpty()) {
            return input;
        }

        String expanded = "";

        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            int count = input.charAt(++i) - '0';

            for (int j = 0; j < count; j++) {
                expanded += c;
            }
        }

        return expanded;
    }
//    6. Write a program that compresses a string by replacing consecutive repeating characters with the character followed by the count. For example, "aaabbcddd" should become "a3b2c1d3". If the "compressed" string is longer than the original, return the original string.
    public static String compressString(String string){
        if(string == null){
            return string;
        }
        String compressedString = "";
        for ( int i = 0; i < string.length ( ); i++ ) {
            char ch = string.charAt ( i );
            int count = 1;
            for ( int j = i+1; j < string.length (); j++ ) {
                if(string.charAt ( j ) == ch){
                    count++;
                    i = j;
                }else {
                    break;
                }
            }
            compressedString += String.valueOf ( ch ) + count;
        }
        return compressedString;
    }
    public static String decodeString(String string){
        if(string == null){
            return string;
        }
        String decodedString = "";
        for ( int i = 0; i < string.length (); i++ ) {
            char character = string.charAt ( i );
            int count = string.charAt ( ++i ) - '0';
            for ( int j = 0; j < count; j++ ) {
                decodedString += character;
            }
        }
        return decodedString;
    }
    public static char findMostFrequentCharacter(String  string){
        if(string == null){
            return 0;
        }
        int mostFreqCount = 0;
        char mostFreqCharacter = '\0';
        for ( int i = 0; i < string.length (); i++ ) {
            int charCount = 0;
            for ( int j = 0; j < string.length (); j++ ) {
                if ( string.charAt ( i ) == string.charAt ( j ) ){
                    charCount++;
                }
            }
            if(charCount > mostFreqCount){
                mostFreqCount = charCount;
                mostFreqCharacter = string.charAt ( i );
            }

        }
        return mostFreqCharacter;
    }
}
1
Subscribe to my newsletter

Read articles from Nethula Ganesh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Nethula Ganesh
Nethula Ganesh