Given an alphabet, print all the alphabets up to and including the given alphabet.

Write a function:
void printAlphabet(char ch)

that accepts a character ch, and prints alphabets up to the given character.

Input
d

Output
a b c d

Where,

There must be a single space between consecutive alphabets.

There should be no spaces after the last alphabet.

ch is a character within the range [{'a'...'z'}, {'A'...'Z'} ].

Alphabet(s) in input & output are case-sensitive.

There should beNO output for any other input.

NOTE: Please use System.out.print instead of System.out.println

This is a Java program

Respuesta :

Answer:

Explanation:

public void printAlphabets(char c){

    String capitals = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    String small = "abcdefghijklmnopqrstuvwxyz";

    if(capitals.contains(""+c)){

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

            if (capitals.charAt(i)!=c)

                System.out.print(capitals.charAt(i)+" ");

            else

                break;

        }// end for

        System.out.print(c);

    }else if (small.contains(""+c)){

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

            if (small.charAt(i)!=c)

                System.out.print(small.charAt(i)+" ");

            else

                break;

        }// end for

        System.out.print(c);

    }// end else if

}// end printAlphabets method

Ver imagen dayanandghelaro

Answer:

  //Write the printAlphaber method header;

   //It takes a char as parameter and has a return type of void

   public static void printAlphabet(char ch){

       

       //Every char has its ASCII number representation

       //For example, char 'A' = 65, 'B' = 66, 'a' = 97, 'c' = 99

       //In essence, A to Z = 65 to 90 and a to z = 97 to 122

       //Also, if comparing a char with an int will compare the ASCII representation of the char with the int

       //For example, 'A' == 65 will return true.

       //Using this technique, let's write an if..else statement that checks

       //if char ch is between 65 and 90 both inclusive.  

       //If ch is between 65 and 90, then it is an uppercase letter  

       if(ch >= 65 && ch <=90){

           

           //Therefore, write a for loop to print all capital letters from 65(which is A) to char

           //starting from i=65 and ending at i=ch

           for(int i = 65; i <= ch; i++){

               //print the char representation of the ASCII number

               //by type casting the ASCII number to a char

               System.out.print((char)i);

               

               //attempt to print a space char if i is not the last letter in the sequence

               if (i != ch){

                   System.out.print(" ");

               }

           }    

       }

                 

       //If ch is between 97 and 122 both inclusive, then it is a lowercase letter

       else if(ch >= 97 && ch <= 122){

           

           //Therefore, write a for loop to print all capital letters from 97(which is a) to char

           //starting from i=97 and ending at i=ch

           for(int i = 97; i <= ch; i++){

               

               //print the char representation of the ASCII number

               //by type casting the ASCII number to a char

               System.out.print((char)i);

               if (i != ch){

                   System.out.print(" ");

               }

           }  

       }

       

       else{

           System.out.println("");

       }

   }        // End of method printAlphabet

Sample Output:

When printAlphabet is called with d i.e printAlphabet('d')

>> a b c d

When printAlphabet is called with K i.e printAlphabet('K')

>> A B C D E F G H I J K

Explanation:

The code above has been written in Java and it contains comments explaining every segment of the code. Please kindly read the comments carefully. The source code file for the complete application has been attached to this response. Kindly download it.

The code without comments

   public static void printAlphabet(char ch){

       if(ch >= 65 && ch <=90){

           for(int i = 65; i <= ch; i++){

               System.out.print((char)i);

               if (i != ch){

                   System.out.print(" ");

               }

           }    

       }

                 

       else if(ch >= 97 && ch <= 122){

             for(int i = 97; i <= ch; i++){

               System.out.print((char)i);

                if (i != ch){

                   System.out.print(" ");

                }

           }  

       }

       

       else{

           System.out.println("");

       }

   }        // End of method printAlphabet

Ver imagen stigawithfun