Assignment 5 Java 57
Assignment 5 Java 57
1. Write Java Program for the String input = My College Name is KIT is in Kolhapur
Perform this task
on given String input.
1. Find the length of the String.
2. Find the Count of substring KIT
3 Display the input string to UpperCase
4. Display the input string to lowercase
class demo{
public static void main(String[] args) {
String a = "My College is KIT KIT is in Kolhapur";
System.out.println(a.length());
System.out.println(a.toLowerCase());
System.out.println(a.toUpperCase()
int count = StringUtils.countMatches(a, "kit");
System.out.println("Total occurrences: " + count);
}
}
Output:- 36
my college is kit kit is in kolhapur
MY COLLEGE IS KIT KIT IS IN KOLHAPUR
Total occurrences:-2
Q2.Write Java Program to Display the Users full name. Ask User to Enter First name
and Last Name.
Ask User to Enter Gender of the User. Display Users Full name with Mr Full Name /
Miss Full Name
// Online Java Compiler
// Use this editor to write, compile and run your Java code onlin
import java.util.Scanner;
class demo{
public static void main(String[] args) {
String first_name;
String last_name ;
String gender;
System.out.println("enter first_name:");
first_name=SC.nextLine();
System.out.println("enter last_name:");
last_name=SC.nextLine();
System.out.println("enter gender:");
gender= SC.nextLine();
}
}
Output is:-
enter first_name:
parth
enter last_name:
bagal
enter gender:male
full name :parth bagal
my gender is:-male
Q3 Write a Java program to compare two strings lexicographically. Two strings are
lexicographically
equal if they are the same length and contain the same characters in the same
positions.
String 1: This is Exercise 1
String 2: This is Exercise 2
public class Exercise5 {
public static void main(String[] args)
{
String str1 = "This is Exercise 1";
String str2 = "This is Exercise 2";
Q5. Write a Java program to test if a given string contains the specified sequence
of char values.
Original String: PHP Exercises and Python Exercises. Check Exercises is present and
count the number
of Exercises present in above input.
public class Exercise1 {
public static void main(String[] args)
{
String str1 = "PHP Exercises and Python Exercises";
String str2 = "and";
System.out.println("Original String: " + str1);
System.out.println("Specified sequence of char values: " + str2);
System.out.println(str1.contains(str2));
}
}
Output:- Original String: PHP Exercises and Python Exercises
Specified sequence of char values: and
True
Q7. Write a Java program to remove "b" and "ac" from a given string String Input -
The given string
is: abrambabasc
import java.util.*;
public class Main {
public static void main(String[] args) {
String strg = "abrambabasc";
System.out.println("The given string is: " + strg);
System.out.print("After removing the new string is: ");
removeSetofCharacters(strg, "ac", "b");
}
public static void removeSetofCharacters(String str, String ptn1, String ptn2) {
int n = str.length(), i;
int ptr = 0;
char[] arr1 = str.toCharArray();
for (i = 0; i < n; ++i) {
if (arr1[i] == 'b') {
continue;
} else if (i + 1 < n && arr1[i] == 'a' && arr1[i + 1] == 'c') {
++i;
} else {
arr1[ptr++] = arr1[i];
}
}
char[] ret = Arrays.copyOfRange(arr1, 0, ptr);
System.out.println(new String(ret));
}
}
OUTPUT;- The given string is: abrambabasc
After removing the new string is: aramaasc
Q8. Write a Java program to count and print all the duplicates in the input string
String Input:
w3resource
import java.util.*;
public class Main
{
static final int MAX_CHARS = 256;
static void CountCharacters(String str1, int[] ctr)
{
for (int i = 0; i < str1.length(); i++)
ctr[str1.charAt(i)]++;
}
static void showDuplicates(String str1)
{
int ctr[] = new int[MAX_CHARS];
CountCharacters(str1, ctr);
for (int i = 0; i < MAX_CHARS; i++)
if(ctr[i] > 1)
System.out.printf("%c appears %d times\n", i, ctr[i]);
}
public static void main(String[] args)
{
String str1 = "w3resource";
System.out.println("The given string is: "+str1);
System.out.println("The duplicate characters and counts are: ");
showDuplicates(str1);
}
}
OUTPUT;- The given string is: w3resource
The duplicate characters and counts are:
e appears 2 times
r appears 2 times
Q10. Write a program for splitting a sting into pieces whenever a space is found
String input = Hello
this is a book on java
public class StringSplit {
public static void main(String[] args) {
String str = "Hello this is a book on java";
String[] splits = str.split("\\s");