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

Design a Java program that assigns the string values \"Hi\" and \"Hello World\"

ID: 3927210 • Letter: D

Question

Design a Java program that assigns the string values "Hi" and "Hello World" to String variables. The program calculates and displays the following information:

Length of the tow strings

Index of the first character 'o'

Index of the next character'o'

Concatenation of the variable containig "Hi" with a substring of the variabe containing "Hello World"; the substring begins at the position og he space immediately preceding the character 'W' and extends through the end of the string

The second String variable in all lowercase characters

The second String variable in all uppercase characters

Explanation / Answer


public class learnStrings {
   public static void main(String[] args) {
       String s1 = "Hi";
       String s2 = "Hello World";
       int len1 = s1.length();
       int len2 = s2.length();
       System.out.println("The length of first string is "+len1);
       System.out.println("The length of second string is "+len2);
       int index1 = s1.indexOf('o');
       int index2 = s2.indexOf('o');
       System.out.println("The index of first 'o' in first string is "+index1);
       System.out.println("The index of first 'o' in second string is "+index2);
      
       System.out.println("The index of next 'o' in first string is "+s1.indexOf('o', index1+1));
       System.out.println("The index of next 'o' in second string is "+s2.indexOf('o', index2+1));
      
       int sindex = s2.indexOf(' ');
      
       String s3 = s1+s2.substring(sindex, len2);
      
       System.out.println("The concatenated string is "+s3);
      
       s2 = s2.toLowerCase();
       System.out.println("The second string in lowercase is "+s2);
      
       s2 = s2.toUpperCase();
       System.out.println("The second string in uppercase is "+s2);
      
      
   }

}