This is IntelliJ Java Write a program to test if a sentence

This is IntelliJ Java Write a program to test if a sentence contains all of the letters of the alphabet, A through Z. “A quick brown fox jumps over the lazy dog” is an example. Write a program that uses a loop to decide if a sentence contains all of the letters or not. Your code should NOT use 26 if statements! There are several completely different, ways of doing this. It might be helpful to learn about the char data type. One thing that might help is to turn a String into an array of char values like this:

Solution

PROGRAM CODE:

package sample;

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be \"Main\" only if the class is public. */
public class Alphabets
{
   public static void main (String[] args) throws java.lang.Exception
   {
       String alphabets = \"abcdefghijklmnopqrstuvwxyz\";
       String input;
       int counter = 0;
       Scanner reader = new Scanner(System.in);
       System.out.print(\"Enter the string: \");
       //Reading the input and converting it to lowercase
       input = reader.nextLine();
       input = input.toLowerCase();
       //Making the input sentence into a character array
       char[] inputLetters = input.toCharArray();
       //boolean flag to mark if a letter is present in the input or not
       boolean isPresent = false;
       //Loop 1: for all the alphabets
       while(counter<alphabets.length())
       {
           //Loop 2: for all the letters in the input
           for(int i=0; i<inputLetters.length; i++)
           {
               //checking if a letter is present, if so , mark the flag as true and break the loop
               if(inputLetters[i] == alphabets.charAt(counter))
               {
                   isPresent = true;
                   break;
               }
           }
           //if the value is not present, then the flag will be false
           //in that case, output the message and end the program
           if(!isPresent)
           {
               System.out.println(\"The sentence does not contain all the letters\");
               System.exit(0);
           }
           //At this point, it means that the letter is present, hence mark the flag back to false for the next letter
           isPresent = false;
           //counter for each alphabet in the string \'alphabets\'
           counter++;
       }
       //If control successfully came out of the loop, it means that all the 26 letters are present
       System.out.println(\"The sentence contains all the letters\");
   }
}

OUTPUT:

Run 1:

Run 2:

This is IntelliJ Java Write a program to test if a sentence contains all of the letters of the alphabet, A through Z. “A quick brown fox jumps over the lazy dog
This is IntelliJ Java Write a program to test if a sentence contains all of the letters of the alphabet, A through Z. “A quick brown fox jumps over the lazy dog

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site