NAME – ISHAAN SINGH CHAUHAN
CLASS – 2BCA B
REG NO – 24215118
Java Lab Prog 7(String)
Task - 1
WAP to find the character of a string at the specific location.
Develop a program to demonstrate the various index methods
such as indexOf() and lastIndexOf() to search inside strings.
Develop a program in java using length() , trim() , split() and
compareTO() methods.
Develop a program to create a array of string and display the
array items on the screen.
CODE:
package ASSIGNMENTS;
import [Link];
public class CharacterAtSpecificLocation {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String inputString = [Link]();
[Link]("Enter the index of the character to retrieve: ");
int index = [Link]();
if (index >= 0 && index < [Link]()) {
char character = [Link](index);
[Link]("Character at index " + index + ": " + character);
} else {
[Link]("Invalid index.");
package ASSIGNMENTS;
public class IndexMethodsDemo {
public static void main(String[] args) {
String str = "Hello, welcome to the world of Java programming";
int firstOccurrence = [Link]("welcome");
[Link]("First occurrence of 'welcome': " + firstOccurrence);
int lastOccurrence = [Link]("welcome");
[Link]("Last occurrence of 'welcome': " + lastOccurrence);
package ASSIGNMENTS;
public class StringMethodsDemo {
public static void main(String[] args) {
String str = " Hello, Java Wassup! ";
int length = [Link]();
[Link]("Length of the string: " + length);
String trimmedString = [Link]();
[Link]("Trimmed string: '" + trimmedString + "'");
String[] words = [Link](" ");
[Link]("Words in the string:");
for (String word : words) {
[Link](word);
String anotherString = "Hello, Java Wassup!";
int comparisonResult = [Link](anotherString);
if (comparisonResult == 0) {
[Link]("The strings are equal.");
} else if (comparisonResult < 0) {
[Link]("The first string is lexicographically smaller.");
} else {
[Link]("The first string is lexicographically larger.");
package ASSIGNMENTS;
public class StringArrayDemo {
public static void main(String[] args) {
String[] fruits = {"Apple", "Banana", "Cherry", "Date", "Elderberry"};
[Link]("Items in the fruits array:");
for (String fruit : fruits) {
[Link](fruit);
OUTPUT:
Identify the output of this program
import [Link];
public class dataprog{
public static void main(String[] args) {
String[] sa = new String[6];
sa[0] = "C";
sa[1] = "H";
sa[2] = "R";
sa[3] = "I";
[Link]("Original Array Elements:"+
[Link](sa));
int totalitem = 4;
String newItem1 = "S";
String newItem2 ="T";
sa[totalitem++] = newItem1;
sa[totalitem++] = newItem2;
[Link]("Array after adding two elements:" +
[Link](sa));
OUTPUT: