$29.99
Lab Assignment 8
1) Objective: Comparing Strings and their References. (Click Here)
Problem Statement:- A Java program to find any character in a string using some index position.
Code:-
package demo1;
public class stringmethod {
private String var;
public String getVar() {
return var;
}
public void setVar(String var) {
this.var = var;
}
public void charAtFunc(String var1, int i) {
System.out.println(var1.charAt(i));
}
}
package demo1;
import java.util.Scanner;
public class stringmain {
public static void main(String[] args) {
stringmethod stringMethod = new stringmethod();
String var1;
int index;
stringMethod.setVar("Bennett");
Scanner sc = new Scanner(System.in);
System.out.println("Enter the new string");
var1 = sc.nextLine();
System.out.println("Enter the index for which you need the character");
index = sc.nextInt();
stringMethod.charAtFunc(var1, index);
}
}
Screenshot:-
Explanation:- In the class named “stringmethod” method is defined named “charAtFunc” in
which two arguments named string and index position are passed.
In the main class named “stringmain”, call the method with proper arguments or parameters (with the
input string and index position). charAtFunc is called in this class and get the proper output.
Lab Practice
1) Objective: Use of String Class Methods and String Programs in Java.
Problem Statement:- Write a java program for find the length of a string.
Enter the input string:
bennett
The length of the input string is: 7
Note: Method for finding the string should be called in the main class.
2) Objective: Use of String Class Methods and String Programs in Java.
Problem Statement:- Write a java program for find all substring of a given string.
Enter a string to find its sub-strings
shore
Sub-strings of the string "shore" are
s
sh
sho
shor
shore
h
ho
hor
hore
o
or
ore
r
re
e
Note: Method for finding the substring should be called in the main class.
3) Objective: Use of String Class Methods and String Programs in Java.
Problem Statement:- Write a java program that will read a string and makes first alphabet capital
of each word in given string.
Input string: we are looking for good players.
Output string: We Are Looking For Good Players.
Note: Method for making first alphabet capital should be called in the main class.
4) Objective: Use of String Class Methods and String Programs in Java.
Problem Statement:- Write a java program for getting the last index of any given character in
a string.
Input:
Enter string: IncludeHelpme
Enter character: e
Output:
Last index of l is: 12
Note: Method for getting the last index should be called in the main class.
5) Objective: Use of String Class Methods and String Programs in Java.
Problem Statement:- Write a Java program to concatenate two strings.
Input:
String 1: "Bennett"
String 2: "University"
Output:
Final string after concatenating: " Bennett University "
Note: Method for concatenate two strings should be called in the main class.
6) Objective: Use of String Class Methods and String Programs in Java.
Problem Statement:- Write a Java program to find occurrences of each character in string
Input: Save water save earth
Output:
S 1 Times
a 4 Times
v 2 Times
e 4 Times
w 1 Times
t 2 Times
r 2 Times
s 1 Times
h 1 Times
Note: Method for finding the occurrences of each character should be called in the main class.
7) Objective: Use of String Class Methods and String Programs in Java.
Problem Statement:- Write a Java program to check Given String is Palindrome String or not.
Enter String : bennett
bennett is not a Palindrom String
Enter String : abcba
abcba is a Palindrom String
Note: Method for to check Given String is Palindrome ,should be called in the main class.
8) Objective: Use of String Class Methods and String Programs in Java.
Problem Statement:- Write a Java program to input a string from user and reverse each word of
given string.
Input String : Hello Welcome in Bennett
String with Reverese Word : olleH emocleW ni ttenneB
Note: Method for reverse each word should be called in the main class.
9) Objective: Use of String Class Methods and String Programs in Java.
Problem Statement:- Write a Java program to convert string into Lowercase and Uppercase
using String.toLowerCase() and String.toUpperCase() methods in java.
Original String: Hello Guys.
Lower Case String: hello guys.
Upper Case String: HELLO GUYS.
Note: Method for finding the substring should be called in the main class.
10) Objective: Use of String Class Methods and String Programs in Java.
Problem Statement:- Write a Java program to get sub string from a given string using index
position.
Enter the string: www.bennett.com
Enter start index: 2
Enter end index: 6
Substring is: .ben
Note: Method for finding the substring should be called in the main class.
11) Objective: Use of String Class Methods and String Programs in Java.
Problem Statement:- Write a Java program to how to replace a string with another string using
String.replace() method of String class in Java.
Strings before replacing:
str1: www.bennett.com
str2: Hello World
Strings after replacing:
str1: Hello World
str2: Hello World
Note: Method for replace a string should be called in the main class.
12) Objective: Use of String Class Methods and String Programs in Java.
Problem Statement:- Write a Java program to how to trim a given string using String.trim()
method in Java.
Input string: “ www.bennett.com “
Output string: “www.bennett.com”
Note: Method for finding the trim should be called in the main class.
13) Objective: Use of String Class Methods and String Programs in Java.
Problem Statement:- Write a Java program to replace a character in a string.
The input string is:
This is a Java Dutorial
After replacement(D with T), the string is:
This is a Java Tutorial
Note: Method for replace character should be called in the main class.
14) Objective: Use of String Class Methods and String Programs in Java.
Problem Statement:- Write a Java program to find the first repeated character in a string.
Output is:
Please enter a string:
java
The character 'a' is repeated
Note: Method find the first repeated character should be called in the main class.