Java Strings- Practice Programs with Solutions

 


//1. Reverse a String


import java.util.Arrays;


public class StringReverse {


    public static void main(String[] args) {

        String original = "welcome";

        

        // Method 1 - Using length() and charAt()

        String rev1 = "";

        for (int i = original.length() - 1; i >= 0; i--) {

            rev1 += original.charAt(i);

        }

        System.out.println("Method 1 - Reverse string: " + rev1);

        

        // Method 2 - By converting string to char array

        String rev2 = "";

        char[] charArray = original.toCharArray(); // Convert string to character array

        System.out.println("Character array: " + Arrays.toString(charArray)); // Display the character array

        

        for (int i = charArray.length - 1; i >= 0; i--) {

            rev2 += charArray[i];

        }

        System.out.println("Method 2 - Reverse string: " + rev2);

        

        // Method 3 - Using StringBuffer

        StringBuffer sb = new StringBuffer(original);

        String rev3 = sb.reverse().toString();

        System.out.println("Method 3 - Reverse string: " + rev3);

        

        // Method 4 - Using StringBuilder

        StringBuilder sbuilder = new StringBuilder(original);

        String rev4 = sbuilder.reverse().toString();

        System.out.println("Method 4 - Reverse string: " + rev4);

    }

}



//2. Check if a String is Palindrome


public class PalindromeCheck {

    public static void main(String[] args) {

        String str = "madam";

        String reversed = new StringBuilder(str).reverse().toString();

        if (str.equals(reversed)) {

            System.out.println(str + " is a palindrome.");

        } else {

            System.out.println(str + " is not a palindrome.");

        }

    }

}


//3. Count Vowels and Consonants


public class VowelConsonantCount {

    public static void main(String[] args) {

        String str = "hello";

        int vowels = 0, consonants = 0;

        str = str.toLowerCase();

        for (char c : str.toCharArray()) {

            if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {

                vowels++;

            } else {

                consonants++;

            }

        }

        System.out.println("Vowels: " + vowels);

        System.out.println("Consonants: " + consonants);

    }

}


//4. Find the Length of a String Without Using length() Method


public class StringLength {

    public static void main(String[] args) {

        String str = "hello";

        int length = 0;

        for (char c : str.toCharArray()) {

            length++;

        }

        System.out.println("Length of String: " + length);

    }

}


//5. Convert a String to Upper Case


public class ToUpperCase {

    public static void main(String[] args) {

        String str = "hello";

        String upperCaseStr = str.toUpperCase();

        System.out.println("Uppercase: " + upperCaseStr);

    }

}


//6. Convert a String to Lower Case


public class ToLowerCase {

    public static void main(String[] args) {

        String str = "HELLO";

        String lowerCaseStr = str.toLowerCase();

        System.out.println("Lowercase: " + lowerCaseStr);

    }

}


//7. Replace a Character in a String


public class ReplaceCharacter {

    public static void main(String[] args) {

        String str = "hello";

        String replacedStr = str.replace('l', 'y');

        System.out.println("Replaced String: " + replacedStr);

    }

}


//8. Remove All White Spaces from a String


public class RemoveSpaces {

    public static void main(String[] args) {

        String str = "he llo world";

        String noSpacesStr = str.replaceAll("\\s", "");

        System.out.println("String without spaces: " + noSpacesStr);

    }

}



//9. Check if Two Strings are Anagrams


import java.util.Arrays;

public class AnagramCheck {

    public static void main(String[] args) {

        String str1 = "listen";

        String str2 = "silent";

        

        char[] arr1 = str1.toCharArray();

        char[] arr2 = str2.toCharArray();

        

        Arrays.sort(arr1);

        Arrays.sort(arr2);

        

        if (Arrays.equals(arr1, arr2)) {

            System.out.println(str1 + " and " + str2 + " are anagrams.");

        } else {

            System.out.println(str1 + " and " + str2 + " are not anagrams.");

        }

    }

}




//10. Split a String into an Array


import java.util.Arrays;


public class SplitString {

    public static void main(String[] args) {

        String str = "hello world";

        String[] words = str.split(" ");

        System.out.println(Arrays.toString(words));

    }

}


//11. Remove a Specific Character from a String


public class RemoveCharacter {

    public static void main(String[] args) {

        String str = "hello";

        String removedStr = str.replace("l", "");

        System.out.println("String after removing 'l': " + removedStr);

    }

}




//12. Concatenate Two Strings Without Using + Operator


public class ConcatenateStrings {

    public static void main(String[] args) {

        String str1 = "hello";

        String str2 = "world";

        String concatenatedStr = str1.concat(str2);

        System.out.println("Concatenated String: " + concatenatedStr);

    }

}



//13. Reverse Words in a Sentence


public class ReverseWords {

    public static void main(String[] args) {

        String str = "hello world";

        String[] words = str.split(" ");

        StringBuilder reversedStr = new StringBuilder();

        

        for (int i = words.length - 1; i >= 0; i--) {

            reversedStr.append(words[i]).append(" ");

        }

        

        System.out.println("Reversed Sentence: " + reversedStr.toString().trim());

    }

}


Followers