0% found this document useful (0 votes)
10 views9 pages

23ID01CE020 ( Practical - 5 )

Uploaded by

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

23ID01CE020 ( Practical - 5 )

Uploaded by

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

Practical - 5

Concatenation and Conversion


1. Concatenate two or more strings using the + operator and the concat()
method.
 Using ‘+’ Operator:
 public class String_Example
{
public static void main(String[] args)
{
String str1 = "Swapnil ";
String str2 = "Beladiya";
System.out.println("Given String is: " + str1 +
str2);
}
}

 Using ‘concat()’ Method:


 public class String_Example
{
public static void main(String[] args)
{
String str1 = "Swapnil";
String str2 = " Beladiya";
System.out.println("Given String is: " +
str1.concat(str2));
}
}

2. Convert a numeric string to an integer and a floating-point number


using Integer.parseInt() and Double.parseDouble().

Name :- PURU JANGID


Enrollment no.: 23ID01CE020
Practical - 5
 public class StringToNumber
{
public static void main(String[] args)
{
String numericString = "24578";
String floatingString = "24578.64";

int number = Integer.parseInt(numericString);


double floatingNumber =
Double.parseDouble(floatingString);
System.out.println("String as Integer: " +
number);
System.out.println("String as Double: " +
floatingNumber);
}
}

3. Convert an integer to a string using Integer.toString() and demonstrate


its usage.

 public class IntegerToStringExample


{
public static void main(String[] args)
{
int number = 12345;
String strNumber = Integer.toString(number);

System.out.println("The string representation of


the number is: " + strNumber);
}

Name :- PURU JANGID


Enrollment no.: 23ID01CE020
Practical - 5
}

Changing Case of String


1. Convert a string to uppercase and lowercase using toUpperCase()
and toLowerCase() methods.

 public class StringCaseExample


{
public static void main(String[] args)
{
String originalString = "Hello, World!";

String upperCaseString =
originalString.toUpperCase();
String lowerCaseString =
originalString.toLowerCase();

System.out.println("Uppercase: " +
upperCaseString);
System.out.println("Lowercase: " +
lowerCaseString);
}
}

2. Experiment with different case conversion scenarios (e.g.,


capitalizing the first letter of each word).

 public class CaseConversionExample


{
public static void main(String[] args)
{
3

Name :- PURU JANGID


Enrollment no.: 23ID01CE020
Practical - 5
String originalString = "hello, world! java
programming.";

String capitalizeFirstLetter =
capitalizeWords(originalString);
System.out.println("Original: " +
originalString);
System.out.println("Capitalized: " +
capitalizeFirstLetter);
}

public static String capitalizeWords(String str)


{
String[] words = str.split("\\s+");
StringBuilder capitalized = new StringBuilder();

for (String word : words)


{
if (word.length() > 0)
{

capitalized.append(Character.toUpperCase(word.charAt(0)))

.append(word.substring(1).toLowerCase())
.append(" ");
}
}
return capitalized.toString().trim();
}
}

Character Extraction

Name :- PURU JANGID


Enrollment no.: 23ID01CE020
Practical - 5
1. Extract a specific character from a string using charAt() and
substring().

 public class CharacterExtractionExample


{
public static void main(String[] args)
{
String originalString = "Sumit Patel";

char charAtPosition = originalString.charAt(7);


String substringCharacter =
originalString.substring(7, 8);

System.out.println("Character at position 7 using


charAt(): " + charAtPosition);
System.out.println("Character at position 7 using
substring(): " + substringCharacter);
}
}

2. Determine the length of a string using length().

 public class StringLengthExample


{
public static void main(String[] args)
{
String originalString = "Sumit Patel";
int lengthOfString = originalString.length();
System.out.println("Length of the string: " +
lengthOfString);
}

Name :- PURU JANGID


Enrollment no.: 23ID01CE020
Practical - 5
}

String Comparison
1. Compare two strings using equals() and equalsIgnoreCase() methods.
 public class StringComparisonExample
{
public static void main(String[] args)
{
String str1 = "Alice";
String str2 = "alice";
boolean isEqual = str1.equals(str2);
boolean isEqualIgnoreCase =
str1.equalsIgnoreCase(str2);
System.out.println("Using equals(): " + isEqual);
System.out.println("Using equalsIgnoreCase(): " +
isEqualIgnoreCase);
}
}

2. Find the index of a substring within a string using indexOf() and


lastIndexOf().
 public class SubstringIndexExample
{
public static void main(String[] args)
{
String originalString = "Hello, World! Welcome to
the World of Java.";
int indexOfSubstring =
originalString.indexOf("World");

Name :- PURU JANGID


Enrollment no.: 23ID01CE020
Practical - 5
int lastIndexOfSubstring =
originalString.lastIndexOf("World");
System.out.println("Index of 'World': " +
indexOfSubstring);
System.out.println("Last index of 'World': " +
lastIndexOfSubstring);
}
}

3. Check if a string starts with or ends with a specific substring using


startsWith() and endsWith().
 public class StringStartEndExample
{
public static void main(String[] args)
{
String originalString = "Hello, World!";
boolean startsWith =
originalString.startsWith("Hello");
boolean endsWith =
originalString.endsWith("World!");
System.out.println("Starts with 'Hello': " +
startsWith);
System.out.println("Ends with 'World!': " +
endsWith);
}
}

Introduction to Scanner Class


1. Create a Scanner object to read user input from the console.

Name :- PURU JANGID


Enrollment no.: 23ID01CE020
Practical - 5
 import java.util.Scanner;
public class ScannerExample
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.println("Hello, " + name + "!");
}
}

2. Read different data types (int, double, String) from the user using the
Scanner class.
 import java.util.Scanner;
public class ScannerDataTypesExample
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an integer: ");
int intValue = scanner.nextInt();
System.out.print("Enter a double: ");
double doubleValue = scanner.nextDouble();
scanner.nextLine();
System.out.print("Enter a string: ");
String stringValue = scanner.nextLine();
System.out.println("Integer value: " + intValue);
System.out.println("Double value: " +
doubleValue);

Name :- PURU JANGID


Enrollment no.: 23ID01CE020
Practical - 5
System.out.println("String value: " +
stringValue);
}
}

3. Demonstrate how to use nextLine() to read an entire line of input.


 import java.util.Scanner;
public class NextLineExample
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a full sentence: ");
String sentence = scanner.nextLine();
System.out.println("You entered: " + sentence);
}
}

Name :- PURU JANGID


Enrollment no.: 23ID01CE020

You might also like