Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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#

 

1

 

2

import java.util.Scanner;

3

 

4

public class Lab4 {

5

 public static void main(String args[]) {

6

 

7

 // Create a scanner to read from keyboard

8

 Scanner kbd = new Scanner (System.in);

9

 

10

 System.out.print("Enter 1st string: ");

11

 

12

 String str1 = kbd.nextLine(); 

13

 

14

 System.out.print("Enter 2nd string: ");

15

 

16

 String str2 = kbd.nextLine();

17

 

18

 //YOU MUST NOT WRITE ANY CODE ABOVE THIS LINE

19

 

20

 // TODO: Write your code here

21

 

22

 

23

 

24

 //YOU MUST NOT WRITE ANY CODE BELOW THIS LINE

25

 }

26

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Databases Object Oriented Deductive Hypermedia Technologies

Authors: Kamran Parsaye, Mark Chignell, Setrag Khoshafian, Harry Wong

1st Edition

0471503452, 978-0471503453

More Books

Students also viewed these Databases questions