Java considers the String data type as non-primitive, because it contains a sequence of characters and hence there is a predefined class called String dedicated to String processing. The String class has a lot of functions and continues to grow. See https://www.tutorialspoint.com/java/java_strings.htm for more on strings. Your task is to then create a java program that when give a sentence will replace the first occurrence of NUST with Namibia University of Science and Technology.
Sample run 1: Enter a sentence: Welcome to my NUST.
Output: Welcome to my Namibia University of Science and Technology.
Sample run 2: Enter a sentence: The nust is a great institution of high learning.
Output: The Namibia University of Science and Technology is a great institution of high learning.
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
String org_str=in.nextLine();
String rep_str="Namibia University of Science and Technology";
String str=org_str.replace("NUST",rep_str);
System.out.println(str);
}
}
Comments
Leave a comment