Write programs that read a line of input as a string and print:
a) Only the uppercase letters in the string.
b) Every second letter of the string, ignoring other characters such as spaces and symbols. For example, if the string is "abc, defg", then the program should print out a c e g.
c) The string, with all vowels replaced by an underscore.
d) The number of vowels in the string.
e) The positions of all vowels in the string.

Respuesta :

import java.util.Scanner;

public class ReadLine {

   public static boolean isUpper(char c) {

       if (c >= 'A' && c <= 'Z') {

           return true;

       } else return false;

   }

   public static void printUpper(String s) {

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

           if (isUpper(s.charAt(i))) {

               System.out.print(s.charAt(i));

           }

       }

   }

   public static char toLowerCase(char c) {

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

           c += 32;

       }

       return c;

   }

   public static boolean isLetter(char c) {

       return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');

   }

   public static void printSecondLetter(String s) {

       int n = 0;

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

           if (isLetter(s.charAt(i))) {

               if (n % 2 == 1)

                   System.out.print(s.charAt(i));

               n++;

           }

       }

   }

   public static boolean isVowel(char c) {

       char a = toLowerCase(c);

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

           return true;

       } else return false;

   }

   public static void replaceUnderscore(String s) {

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

           if (isVowel(s.charAt(i))) {

               System.out.print("_");

           } else {

               System.out.print(s.charAt(i));

           }

       }

   }

   public static int countVowel(String s) {

       int count = 0;

       s = s.toLowerCase();

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

           if (isVowel(s.charAt(i))) {

               count++;

           }

       }

       return count;

   }

   public static void printVowelPosition(String s) {

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

           if (isVowel(s.charAt(i))) {

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

           }

       }

   }

   public static void main(String[] args) {

       Scanner scan = new Scanner(System.in);

       System.out.print("Input your text: ");

       String text = scan.nextLine();

       System.out.println();

       System.out.print("The uppercase letters in the string: ");

       printUpper(text);

       System.out.println();

       System.out.print("Every second letter of the string: ");

       printSecondLetter(text);

       System.out.println();

       System.out.print("Vowels replaced by an underscore: ");

       replaceUnderscore(text);

       System.out.println();

       System.out.println("The number of vowels in the string: " + countVowel(text));

       System.out.print("The positions of all vowels in the string: ");

       printVowelPosition(text);

       scan.close();

   }

Ê ông dân hanu phải khônggg

The program is an illustration of loops.

Loops are used to perform repetitive operations

The program in Java, where comments are used to explain each line is as follows:

import java.lang.*;

import java.util.*;

public class Main {

  public static void main(String[] args) {

      //This creates a scanner object

      Scanner input = new Scanner(System.in);

      //This prompts the user for input

      System.out.print("Input your text: ");

      //This gets the input

      String str = input.nextLine();

      //The following loop prints the upper case letters

      System.out.print("Uppercase letters: ");

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

          if (Character.isUpperCase(str.charAt(i))) {

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

          }

      }

      //The following loop prints every second letters

      System.out.print("\nEvery second letters: ");

      int n = 0;

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

          if (Character.isLetter(str.charAt(i))) {

              if (n % 2 == 1){

                  System.out.print(str.charAt(i));}

              n++;

          }

      }

      char a; int countVowel = 0; String vowelPosition = "";

      //The following loop prints the string after the vowels are replaced by underscores

      System.out.print("\nThe new string: ");

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

          a = Character.toLowerCase(str.charAt(i));

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

              System.out.print("_");

              //This counts the number of vowels

              countVowel++;

              //This notes the positions of vowels

              vowelPosition+=Integer.toString(i+1)+" ";

          }

          else {

              System.out.print(str.charAt(i));

          }

      }

      //This prints the number of vowels

      System.out.print("\nThe number of vowels in the string: " + countVowel);

      //This prints the positions of vowels

      System.out.print("\nThe positions of all vowels in the string: "+vowelPosition);

  }

}

Read more about similar programs at:

https://brainly.com/question/6858475