Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program using Java in a single class called DoubleLetters that does the

ID: 3669843 • Letter: W

Question

Write a program using Java in a single class called DoubleLetters that does the following:

The program should read in a single line of text from the keyboard. It should then print to the console the same line of text, only now with every letter doubled. Other characters, such as numbers, spaces, and punctuation in the line should be left alone, except for exclamation points (!): These should be tripled.

The objectives of this assignment are to:

Be able to read text input from the keyboard using a Scanner object.

Be able to traverse the characters in a String using String class methods and a for loop.

Be able to use an if statement in a loop.

Example 1: if the input text is:

Then your program should print:

Example 2: if the input text is:

Then your program should print:

Hint:
When is a character a letter? There is more than one way to tell. Look up the Character class in the Java API. There is a method that would be very helpful.

Explanation / Answer

import java.util.*;

class DoubleLetters
{
   public static void main(String args[])
   {
       String s;
       int b;
       Scanner scan = new Scanner(System.in);

       System.out.println("Enter String");
       s=scan.nextLine();
       char c;
       for(int i=0; i<s.length();i++)
       {
           c=s.charAt(i);
           if(Character.isLetter(c))
           {
              
               s=s.substring(0, i)+c+s.substring(i+1, s.length());
              
           }

       }
       System.out.println(s);
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote