5. A String variable aStr contains a given String. Write a segment of Java code
ID: 3630907 • Letter: 5
Question
5. A String variable aStr contains a given String.
Write a segment of Java code that will compute
another String, bStr, that consists of every second
character from aStr, starting from the second character.
That is, bStr will consist of characters number
1, 3, 5, 7,... from aStr. Remember that the index
for the characters in a String starts from 0, not 1.
For example, if aStr contains "nonprocrastination",
then after your code runs, bStr should contain "oporsiain"
because "oporsiain" consists of characters
number 1, 3, 5, 7, 9, 11, 13, 15, and 17 from
the String "nonprocrastination".
Note: This is only an example. Your code must be general.
Hint:
You might want to start with an empty String variable similar to:
String bStr = "";
and add characters to this String in the loop similar to the following:
bStr = bStr + char;
where char is the char you want added to the String.
Explanation / Answer
package ca.ehsan;import java.util.Scanner; public class Test_Program_Eight{ public static void main(String[] args) { Scanner scan = new Scanner(System.in); { String aStr = scan.nextLine(); String bStr = ""; for (int i = 0; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.