0% found this document useful (0 votes)
3 views6 pages

Java Lab Prog 7(String).24215118

Uploaded by

ishaan.singh
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)
3 views6 pages

Java Lab Prog 7(String).24215118

Uploaded by

ishaan.singh
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/ 6

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 java.util.Scanner;

public class CharacterAtSpecificLocation {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter a string: ");


String inputString = scanner.nextLine();

System.out.print("Enter the index of the character to retrieve: ");

int index = scanner.nextInt();

if (index >= 0 && index < inputString.length()) {

char character = inputString.charAt(index);

System.out.println("Character at index " + index + ": " + character);

} else {

System.out.println("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 = str.indexOf("welcome");

System.out.println("First occurrence of 'welcome': " + firstOccurrence);

int lastOccurrence = str.lastIndexOf("welcome");

System.out.println("Last occurrence of 'welcome': " + lastOccurrence);

package ASSIGNMENTS;

public class StringMethodsDemo {


public static void main(String[] args) {

String str = " Hello, Java Wassup! ";

int length = str.length();

System.out.println("Length of the string: " + length);

String trimmedString = str.trim();

System.out.println("Trimmed string: '" + trimmedString + "'");

String[] words = str.split(" ");

System.out.println("Words in the string:");

for (String word : words) {

System.out.println(word);

String anotherString = "Hello, Java Wassup!";

int comparisonResult = str.compareTo(anotherString);

if (comparisonResult == 0) {

System.out.println("The strings are equal.");

} else if (comparisonResult < 0) {

System.out.println("The first string is lexicographically smaller.");

} else {

System.out.println("The first string is lexicographically larger.");

package ASSIGNMENTS;

public class StringArrayDemo {

public static void main(String[] args) {

String[] fruits = {"Apple", "Banana", "Cherry", "Date", "Elderberry"};

System.out.println("Items in the fruits array:");

for (String fruit : fruits) {


System.out.println(fruit);

OUTPUT:
Identify the output of this program
import java.util.Arrays;

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";
System.out.println("Original Array Elements:"+

Arrays.toString(sa));

int totalitem = 4;

String newItem1 = "S";

String newItem2 ="T";

sa[totalitem++] = newItem1;

sa[totalitem++] = newItem2;

System.out.println("Array after adding two elements:" +

Arrays.toString(sa));

OUTPUT:

You might also like