0% found this document useful (0 votes)
17 views

Ist Lab Java Pgms

The document discusses implementing different string class constructors in Java by creating a class with a main method. It demonstrates the default, parameterized, and copy constructors. It also covers extracting characters and substrings from strings, comparing strings, and sorting strings.

Uploaded by

padma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Ist Lab Java Pgms

The document discusses implementing different string class constructors in Java by creating a class with a main method. It demonstrates the default, parameterized, and copy constructors. It also covers extracting characters and substrings from strings, comparing strings, and sorting strings.

Uploaded by

padma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

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();
System.out.println(s1+"Default constructor");

//Parameterised Constructor by passing strings


String s2 = new String("Hello Java");
System.out.println(s2);

//String object using character arrays


char chars[] = {'s', 'c', 'i', 'e', 'n', 'c', 'e'};
String s3 = new String(chars);
System.out.println(s3);

//constructor with substring


char chs[] = { 'w', 'i', 'n', 'd', 'o', 'w', 's' };
String s4 = new String(chs, 0,4);
System.out.println(s4);
//Copy constructor
char ch[] = { 'F', 'A', 'N' };
String s5 = new String(ch);
String s6 = new String(s5);
System.out.println(s5);
System.out.println(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);
System.out.println(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);
System.out.println(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=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);
}
}

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";
System.out.println(s1.equals(s2));//true
System.out.println(s1.equals(s3));//true
System.out.println(s1.equals(s4));//false
System.out.println(s1.equalsIgnoreCase(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));

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);
}
}

Java program to sort Strings using Bubble sort

public class BubbleSortStrings {


public static void bubbleSort(String[] arr) {
int n = arr.length;
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"};
System.out.println("Original array:");
for (String s : arr) {
System.out.print(s + " ");
}

bubbleSort(arr);

System.out.println("\nSorted array:");
for (String s : arr) {
System.out.print(s + " ");
}
}
}

You might also like