1. Implement a java program to illustrate the use of different types of string class constructors.
public class Constructors
{
public static void main(String[ ] args)
{
//Default Constructor
String s1=new String();
[Link](s1+"Default constructor");
//Parameterised Constructor by passing strings
String s2 = new String("Hello Java");
[Link](s2);
//String object using character arrays
char chars[] = {'s', 'c', 'i', 'e', 'n', 'c', 'e'};
String s3 = new String(chars);
[Link](s3);
//constructor with substring
char chs[] = { 'w', 'i', 'n', 'd', 'o', 'w', 's' };
String s4 = new String(chs, 0,4);
[Link](s4);
//Copy constructor
char ch[] = { 'F', 'A', 'N' };
String s5 = new String(ch);
String s6 = new String(s5);
[Link](s5);
[Link](s6);
//Example for String(byte byteArr[ ])
byte b[] = { 97, 98, 99, 100 }; // Range of bytes: -128 to 127
//. These byte values will be converted into
corresponding//characters.
String s7 = new String(b);
[Link](s7);
// Example for String(byte byteArr[ ], int startIndex, int count)
byte b1[] = { 65, 66, 67, 68, 69, 70 };
String s8 = new String(b1, 2, 4);
[Link](s8);
}
}
2. Implement a java program to illustrate the use of
a. different types of character extraction,
b. string comparison and string search
c. string modification methods.
Character Extraction Methods
class charExtraction
{
public static void main(String args[])
{
String str="Hello";
char ch=[Link](2);
[Link](ch);
String strr="Good Luck dear students";
String substr1=[Link](5);
[Link](substr1);
String subStr2 = [Link](4, 11);
[Link](subStr2);
String str2 = "welcome to mysuru";
char[] charArray = [Link]();
for (int i = 0; i < [Link]; i++) {
[Link](charArray[i]);
}
String str3="We cannotsolve problems with the kind of thinking";
char[] dst = new char[6];
[Link](3,9,dst,0);
[Link](dst);
}
}
b.) string comparison and string search
class Teststringcomparison1{
public static void main(String args[]){
String s1="Sachin";
String s2="Sachin";
String s3=new String("Sachin");
String s4="Saurav";
[Link]([Link](s2));//true
[Link]([Link](s3));//true
[Link]([Link](s4));//false
[Link]([Link](s2));
String Sc1 = new String("This is an example");
String Sc2 = new String("isan");
System. out. println("Result of comparing Sc1 with Sc2: ");
System. out. println(Sc1. regionMatches(true, 2, Sc2, 0, 2));
System. out. println(Sc1. regionMatches(true, 5, Sc2, 0, 2));
System. out. println(Sc1. regionMatches(true, 15, Sc2, 0, 2));
[Link]([Link]("This"));
[Link]([Link]("his",1));
[Link]([Link]("is",1));
[Link]([Link]("example"));
[Link]([Link]("is"));
String ss1="Sachin";
String ss2="Mithun";
String ss3="Ratan";
[Link]([Link](s2));//s1==s2 returns 0
[Link]([Link](ss2));//1(because ss3>ss2)
[Link]([Link](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 [Link].*;
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 = [Link]();
int len_p = [Link]();
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 ([Link](i + j) != [Link](j))
break;
}
if (j == len_p)
{
k++;
[Link]("Pattern Found at Position: " + i);
}
}
if (k == 0)
[Link]("No Match Found!");
else
[Link]("Total Instances Found = " + k);
}
}
Java program to sort Strings using Bubble sort
public class BubbleSortStrings {
public static void bubbleSort(String[] arr) {
int n = [Link];
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j].compareTo(arr[j + 1]) > 0) {
// Swap arr[j] and arr[j+1]
String temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
public static void main(String[] args) {
String[] arr = {"banana", "apple", "cherry", "date", "fig"};
[Link]("Original array:");
for (String s : arr) {
[Link](s + " ");
}
bubbleSort(arr);
[Link]("\nSorted array:");
for (String s : arr) {
[Link](s + " ");
}
}
}