Implementation Java program to illustrate the use of different types of string class constructor
default constructor
Parameterized constructor by passing string
Constructor with substring
Copy constructor
Code:
Package lab1;
Public class stringClassConstructors {
Public static void main(String[] args) {
String defaultString = new String();
System.out.println("Default Constructor: " + defaultString);
String paramString = new String("Hello World");
System.out.println("Parameterized Constructor: " + paramString);
String originalString = "Hello World";
String substring = originalString.substring(6, 11);
System.out.println("Constructor with Substring: " + substring);
String copyString = new String(paramString);
System.out.println("Copy Constructor: " + copyString);
Byte[] byteArray = {72, 101, 108, 108, 111};
String byteString = new String(byteArray);
System.out.println("String from Byte Array: " + byteString);
Byte[] anotherByteArray = {72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100};
String partialByteString = new String(anotherByteArray, 6, 5);
System.out.println("String from Byte Array with Offset and Count: " + partialByteString);
Output:
Default Constructor:
Parameterized Constructor: Hello World
Constructor with Substring: World
Copy Constructor: Hello World
String from Byte Array: Hello
String from Byte Array with Offset and Count: World
write a program to demonstrate Java strings are immutable
Code:
Package lab1;
Public class immutableString {
Public static void main(String[] args) {
String original = "Hello";
String modified = original.concat(" World"); //modifying
System.out.println("Original String: " + original); //printing original
System.out.println("Modified String: " + modified); //printing modified
If (original.equals("Hello")) { //checking
System.out.println("The original string remains unchanged.");
} else {
System.out.println("The original string has changed.");
Output:
Original String: Hello
Modified String: Hello World
The original string remains unchanged.
write a program to concatenate two strings using user defined functions
Code :
Package lab1;
Public class concatination {
Public static String concatenate(String str1, String str2) {
Return str1 + str2;
Public static void main(String[] args) {
String string1 = "Hello";
String string2 = " World";
String result = concatenate(string1, string2);
System.out.println("Concatenated String: " + result);
Output: Concatenated String: Hello World
4)Write a program to compare two strings using == , equals(), CompareTo()
Package lab1;
Public class StringComparison {
Public static void main(String[] args) {
String str1 = "Hello";
String str2 = "Hello";
String str3 = new String("Hello");
System.out.println("Using == :");
System.out.println("str1 == str2: " + (str1 == str2));
System.out.println("str1 == str3: " + (str1 == str3));
System.out.println("\nUsing equals():");
System.out.println("str1.equals(str2): " + str1.equals(str2));
System.out.println("str1.equals(str3): " + str1.equals(str3));
System.out.println("\nUsing compareTo():");
System.out.println("str1.compareTo(str2): " + str1.compareTo(str2));
System.out.println("str1.compareTo(\"World\"): " + str1.compareTo("World"));
System.out.println("str1.compareTo(\"Apple\"): " + str1.compareTo("Apple"));
Output:
Using == :
Str1 == str2: true
Str1 == str3: false
Using equals():
Str1.equals(str2): true
Str1.equals(str3): true
Using compareTo():
Str1.compareTo(str2): 0
Str1.compareTo("World"): -15
Str1.compareTo("Apple"): 7
5)Write a program to replace the contents of a array with the substring using getChar() method
Code:
Package lab1;
Public class ReplaceArrayWithSubstring {
Public static void main(String[] args) {
String str = "Hello World";
Char[] charArray = new char[5];
Str.getChars(6, 11, charArray, 0);
System.out.println("Updated Array Contents: " + new String(charArray));
Output:
Updated Array Contents: World
6)Write a program to display the alternate characters from a given string
Code:
Package lab1;
Public class AlternateCharacters {
Public static void main(String[] args) {
String str = "Hello World";
For (int I = 0; I < str.length(); I += 2) {
System.out.print(str.charAt(i));
Output:
HloWrd
7)Write a program to extract sum digits in a string class
Code:
Package lab1;
Public class SumDigitsInString {
Public static void main(String[] args) {
String str = "abc123xyz45";
Int sum = 0;
For (int I = 0; I < str.length(); i++) {
Char ch = str.charAt(i);
If (Character.isDigit(ch)) {
Sum += Character.getNumericValue(ch);
}
}
System.out.println("Sum of digits: " + sum);
Output:
Sum of digits: 15
8)Write a program that takes a sentence and splitted into words using split method
Code:
Package lab1;
Public class SplitSentence {
Public static void main(String[] args) {
String sentence = "Java is a powerful language";
String[] words = sentence.split(" ");
For (String word : words) {
System.out.println(word);
Output:
Java
Is
Powerful
Language
9)Write a program to check if two strings are anagrams
Code:
Package lab1;
Import java.util.Arrays;
Public class AnagramCheck {
Public static boolean areAnagrams(String str1, String str2) {
If (str1.length() != str2.length()) {
Return false;
Char[] arr1 = str1.toCharArray();
Char[] arr2 = str2.toCharArray();
Arrays.sort(arr1);
Arrays.sort(arr2);
Return Arrays.equals(arr1, arr2);
}
Public static void main(String[] args) {
String str1 = "listen";
String str2 = "silent";
If (areAnagrams(str1, str2)) {
System.out.println("The strings are anagrams.");
} else {
System.out.println("The strings are not anagrams.");
Output:
The strings are anagrams.
10) write a program that counts the number of vowels and consonants in a string
Code:
Package lab1;
Public class CountVowelsConsonants {
Public static void main(String[] args) {
String str = "Hello World";
Str = str.toLowerCase();
Int vowels = 0, consonants = 0;
For (char ch : str.toCharArray()) {
If (ch >= 'a' && ch <= 'z') {
If ("aeiou".indexOf(ch) != -1) {
Vowels++;
} else {
Consonants++;
System.out.println("Number of vowels: " + vowels);
System.out.println("Number of consonants: " + consonants);
}
Output:
Number of vowels: 3
Number of consonants: 7