Java Practice Set 3
QUESTIONS -
SOLUTIONS/CODE ---
package com.company;
public class PS3 {
public static void main(String[] args) {
// Q1 - Write a JAVA program to covert a string to lowercase
String str = "String";
str = str.toLowerCase();
System.out.println(str);
// Q2 - Write a JAVA program to replace spaces with underscore
String text = "Write a JAVA program to replace spaces with underscore";
text = text.replaceAll(" ", "_");
System.out.println(text);
/* Q3 - Write a JAVA program to fill in a template which looks like below
template = "Dear<|name|>, Thanks a lot"
Replace 'name' with someone's name*/
String template = "Dear <|name|>, Thanks a lot";
template = template.replace("<|name|>", "Saswata");
System.out.println(template);
// Q4 - Write a JAVA program to detect double or triple spaces in a string
String mystr = "This string contains double and triple spaces";
System.out.println(mystr.indexOf(" "));
System.out.println(mystr.indexOf(" "));
/* Q5 - Write a program to format the following letter using escape sequence characters
letter = "Dear Harry,This JAVA course is so nice, thanks." */
System.out.println("\tDear Coder,\n \t \t This JAVA course is so nice.\n\t \t Thanks.");
}
}
Comments
Post a Comment