For the program segment below, select the values for WWW, XXX, YYY and ZZZ that will give a program segment that will print every character of str1 matched up with every character of str2 to give output:A1A2A3B1B2B3C1C2C3D1D2D3E1E2E3String str1 = "ABCDE";String str2 = "123";int num = 0;while ( WWW ) { int val = 0; while ( XXX ) { System.out.print(str1.charAt(num)); System.out.print(str2.charAt(val)); YYY; } ZZZ;}System.out.println();
Question
For the program segment below, select the values for WWW, XXX, YYY and ZZZ that will give a program segment that will print every character of str1 matched up with every character of str2 to give output:A1A2A3B1B2B3C1C2C3D1D2D3E1E2E3String str1 = "ABCDE";String str2 = "123";int num = 0;while ( WWW ) { int val = 0; while ( XXX ) { System.out.print(str1.charAt(num)); System.out.print(str2.charAt(val)); YYY; } ZZZ;}System.out.println();
Solution 1
The values for WWW, XXX, YYY, and ZZZ that will give the desired output are:
WWW: num < str1.length() XXX: val < str2.length() YYY: val++ ZZZ: num++
So the final code will look like this:
String str1 = "ABCDE";
String str2 = "123";
int num = 0;
while ( num < str1.length() ) {
int val = 0;
while ( val < str2.length() ) {
System.out.print(str1.charAt(num));
System.out.print(str2.charAt(val));
val++;
}
num++;
}
System.out.println();
This code works by using two nested while loops. The outer loop iterates over each character in str1, and the inner loop iterates over each character in str2. For each character in str1, it prints that character followed by each character in str2, resulting in the desired output.
Solution 2
The values for WWW, XXX, YYY, and ZZZ that will give the desired output are:
WWW: num < str1.length() XXX: val < str2.length() YYY: val++ ZZZ: num++
So the complete program segment would be:
String str1 = "ABCDE";
String str2 = "123";
int num = 0;
while ( num < str1.length() ) {
int val = 0;
while ( val < str2.length() ) {
System.out.print(str1.charAt(num));
System.out.print(str2.charAt(val));
val++;
}
num++;
}
System.out.println();
This program works by using two nested while loops. The outer loop iterates over each character in str1, and the inner loop iterates over each character in str2. For each character in str1, it prints that character followed by each character in str2. The variables num and val are used to keep track of the current position in str1 and str2, respectively. After all characters in str2 have been printed for a given character in str1, num is incremented to move to the next character in str1. This process repeats until all characters in str1 have been used.
Similar Questions
What will be the output of the following code?string str = "abcdef";cout << str.substr(2, 3);"abc""def""cde""bcd"
In the below code snippet, fill the code to get the character array for a given string, str.Code : Scanner sc=new Scanner(System.in); String str=sc.next(); char c[] = ; for(char x : c ) System.out.print(x+" ");
Find output of the below program:int main(){ char str[]={'e','a','r','t','h'}; printf("%s", str); return 0;}
ect the correct answerWhat will be the output of the following Java program?import java.io.*;class Chararrayinput { public static void main(String[] args) { String obj = "abcdefgh"; int length = obj.length(); char c[] = new char[length]; obj.getChars(0, length, c, 0); CharArrayReader input1 = new CharArrayReader(c); CharArrayReader input2 = new CharArrayReader(c, 1, 4); int i, j; try { while ((i = input1.read()) == (j = input2.read())) { System.out.print((char) i); } } catch (IOException e) { e.printStackTrace(); } }}
#include <stdio.h> #include <string.h> int main() { char *str = "hello, world"; char str1[9]; strncpy(str1, str, 9); printf("%s %d", str1, strlen(str1)); }
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.