Ist Lab Java Pgms
Ist Lab Java Pgms
Ist Lab Java Pgms
class charExtraction
{
public static void main(String args[])
{
String str="Hello";
char ch=str.charAt(2);
System.out.println(ch);
String strr="Good Luck dear students";
String substr1=strr.substring(5);
System.out.println(substr1);
String subStr2 = strr.substring(4, 11);
System.out.println(subStr2);
String str2 = "welcome to mysuru";
char[] charArray = str2.toCharArray();
for (int i = 0; i < charArray.length; i++) {
System.out.println(charArray[i]);
}
String str3="We cannotsolve problems with the kind of thinking";
char[] dst = new char[6];
str3.getChars(3,9,dst,0);
System.out.println(dst);
}
}
System.out.println(Sc1.startsWith("This"));
System.out.println(Sc1.startsWith("his",1));
System.out.println(Sc1.startsWith("is",1));
System.out.println(Sc1.endsWith("example"));
System.out.println(Sc1.endsWith("is"));
String ss1="Sachin";
String ss2="Mithun";
String ss3="Ratan";
System.out.println(s1.compareTo(s2));//s1==s2 returns 0
System.out.println(ss3.compareTo(ss2));//1(because ss3>ss2)
System.out.println(ss3.compareTo(ss1));//-1(because ss3<ss1)
}
}
Note: Complete the above program by adding string search and string modification methods
------------------------------------------------------------------------------------------------------------------
// Java Program to Implement the String Search Algorithm for Short Text Sizes
import java.io.*;
class patternSearch {
public static void main(String[] args)
{
String text = "Advanced java concepts are used for developing websites and software solutions";
String pattern = "websites";
// calling the method that is designed for printing the instances of pattern found in the text string
stringMatch(text, pattern);
}
public static void stringMatch(String text, String pattern)
{
int len_t = text.length();
int len_p = pattern.length();
int k = 0, i = 0, j = 0;
// loop to find out the position Of searched pattern
for (i = 0; i <= (len_t - len_p); i++) {
for (j = 0; j < len_p; j++)
{
if (text.charAt(i + j) != pattern.charAt(j))
break;
}
if (j == len_p)
{
k++;
System.out.println("Pattern Found at Position: " + i);
}
}
if (k == 0)
System.out.println("No Match Found!");
else
System.out.println("Total Instances Found = " + k);
}
}
bubbleSort(arr);
System.out.println("\nSorted array:");
for (String s : arr) {
System.out.print(s + " ");
}
}
}