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

Write a Java program that accepts three command line arguments: First, a String,

ID: 669764 • Letter: W

Question

Write a Java program that accepts three command line arguments:

First, a String, str

Second, an integer corresponding to the position of a letter in str. Parse this into an int position1.

Third, an integer corresponding to the position of a later letter in str. Parse this into an int position2.

For example, the position of the letter 3 in the String 123456789 is 3. [Hint: The so-called 'index' of each letter in a String is 1 smaller than its position.]

Now your program should use methods in the String() class to transpose the position1 and position2 letters, and print the original and modified Strings at the command line in the following format (the java example is given in the first line:

bash$ java SwapLetters "123456789" 2 8
Original String: 123456789; Swap Letters 2 and 8; New String: 183456729

Note: There are definitely multiple ways to do this.

Explanation / Answer

class comline
{
   public static void main(String args[])
   {
       int position1, position2;

       String s,s1,s2;
       s=args[1];

       position1 = Integer.parseInt(args[2]);
       position2 = Integer.parseInt(args[3]);
      
       s1 = s[position1+1];
       s2 = s[position2+1];

       s.setCharAt(position1+1, s2);
       s.setCharAt(position2+1, s1);


       System.out.println("New String is " +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