-1

My input here need to be not case-sensitive. I'll show you my code please help, I have zero idea how to do it. Do I need to re write my code for it to be not case-sensitive?

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

        String s, x;

        System.out.print("Enter a sentence: ");
        s = sc.nextLine();

        char[] c = s.toCharArray();
        int vowel=0;

        for (int i = 0; i < s.length(); i++) {

            if(s.charAt(i)=='a' || s.charAt(i)=='e' || s.charAt(i)=='i' || s.charAt(i)=='o' || s.charAt(i)=='u')
                vowel++;
        }

        System.out.println("There are "+ vowel + " vowels in the sentence");

        System.out.print("\nEnter a substring that you want to search: ");
        x = sc.nextLine();
        boolean check = s.contains(x);

        if (check){
            System.out.println("There is a substring '" + (x.toUpperCase()) + "' in the sentence");

        }
        else
            System.out.println("There is no substring '" + x + "' in the sentence");
        
        System.out.println("\nThe entered sentence in uppercase characters.");
        System.out.println(s.toUpperCase());
    }
xKD
  • 5
  • 2
  • 3
    just call either `toLowerCase()` or `toUpperCase()` on both strings you want to compare. – OH GOD SPIDERS Mar 04 '21 at 13:43
  • Dupe of: [How to check if a String contains another String in a case insensitive manner in Java?](https://stackoverflow.com/q/86780/7939871) – Léa Gris Mar 05 '21 at 16:11

1 Answers1

0

Should make the string entirely to lower case or upper case. Use this while taking input:

s = sc.nextLine().toLowerCase();

and also:

x = sc.nextLine().toLowerCase();

The complete main method:

public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        String s, x;

        System.out.print("Enter a sentence: ");

        s = sc.nextLine().toLowerCase();

        int vowel=0;

        for (int i = 0; i < s.length(); i++) {

            if(s.charAt(i)=='a' || s.charAt(i)=='e' || s.charAt(i)=='i' || s.charAt(i)=='o' || s.charAt(i)=='u')
                vowel++;
        }

        System.out.println("There are "+ vowel + " vowels in the sentence");

        System.out.print("\nEnter a substring that you want to search: ");

        x = sc.nextLine().toLowerCase();

        boolean check = s.contains(x);

        if (check){
            System.out.println("There is a substring '" + (x.toUpperCase()) + "' in the sentence");

        }
        else
            System.out.println("There is no substring '" + x + "' in the sentence");

        System.out.println("\nThe entered sentence in uppercase characters.");
        System.out.println(s.toUpperCase());
    }