Question
Java !!! (Loops) Swapping even-positioned characters between two strings Complete the following code to swap the characters between 2 strings entered by the user (str1
Java !!!
(Loops) Swapping even-positioned characters between two strings
Complete the following code to swap the characters between 2 strings entered by the user (str1 and str2 respectively), but only the ones positioned in even indexes (e.g. 0, 2, etc.). Characters in odd positions must remain the same. In case one of the strings is longer than the another one, after performing all the possible swaps, the remaining characters in the longer string have to be replaced by the character '#'. Assume that both strings (str1 and str2) will have at least 1 character each (the empty string will not be used as input here). The new strings derived from the swapping procedure must be printed in console, one of them in a different line. E.g. Input: Enter the 1st string: "apple" Enter the 2nd string: "orange" Output: opalg arpne#
typing here:
import java.util.Scanner;
public class Lab4 { public static void main(String args[]) {
// Create a scanner to read from keyboard Scanner kbd = new Scanner (System.in); System.out.print("Enter 1st string: "); String str1 = kbd.nextLine(); System.out.print("Enter 2nd string: "); String str2 = kbd.nextLine(); //YOU MUST NOT WRITE ANY CODE ABOVE THIS LINE // TODO: Write your code here
//YOU MUST NOT WRITE ANY CODE BELOW THIS LINE } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started