0% found this document useful (0 votes)
0 views

Assignment 5 Java 57

The document contains a series of Java programming assignments focusing on string manipulation techniques. It includes tasks such as finding string length, counting substrings, converting cases, comparing strings, and removing characters. Each assignment is accompanied by code snippets and expected output for clarity.

Uploaded by

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

Assignment 5 Java 57

The document contains a series of Java programming assignments focusing on string manipulation techniques. It includes tasks such as finding string length, counting substrings, converting cases, comparing strings, and removing characters. Each assignment is accompanied by code snippets and expected output for clarity.

Uploaded by

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

ASSIGNMENT NO.

OBJECT ORIENTED PROGRAMMING


NAME-SHREEYA JOSHI
ROLLNO.-57
PRN-2122000723

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;

Scanner SC=new Scanner(System.in);

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();

System.out.println("full name :" + first_name+ last_name);


System.out.println("my gender is:-" +gender)

System.out.println("full name :" + first_name+ last_name);


System.out.println("my gender is:-" +gender);

}
}
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";

System.out.println("String 1: " + str1);


System.out.println("String 2: " + str2);

int result = str1.compareTo(str2);


if (result < 0)
{
System.out.println("\"" + str1 + "\"" +" is less than " + "\"" + str2 + "\"");
}
else if (result == 0)
{
System.out.println("\"" + str1 + "\"" + " is equal to " +"\"" + str2 + "\"");
}
else
{
System.out.println("\"" + str1 + "\"" + " is greater than " +"\"" + str2 + "\"");
}
}
}
Output:-
String 1: This is Exercise 1
String 2: This is Exercise 2
"This is Exercise 1" is less than "This is Exercise 2"

Q4 Write a Java program to compare two strings lexicographically, ignoring case


differences
String 1: This is exercise 1
String 2: This is Exercise 1
public class Exercise5 {
public static void main(String[] args)
{
String str1 = "This is exercise 1";
String str2 = "This is Exercise 1";
System.out.println("String 1: " + str1);
System.out.println("String 2: " + str2);
int result = str1.compareTo(str2)
if (result < 0)
{
System.out.println("\"" + str1 + "\"" + " is less than " +"\"" + str2 + "\"");
}
else if (result == 0)
{
System.out.println("\"" + str1 + "\"" +" is equal to " + "\"" + str2 + "\"");
}
else
{
System.out.println("\"" + str1 + "\"" +" is greater than " + "\"" + str2 + "\"");
}
}
}
Output:- String 1: This is exercise 1
String 2: This is Exercise 1
"This is exercise 1" is greater than "This is Exercise 1"

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

Q6 Write a Java program to reverse words in a given string


String Input: Reverse words in a given string
import java.util.regex.Pattern;
public class Exp {
static String reverseWords(String str)
{
Pattern pattern = Pattern.compile("\\s");
String[] temp = pattern.split(str);
String result = "";
for (int i = 0; i < temp.length; i++) {
if (i == temp.length - 1)
result = temp[i] + result;
else
result = " " + temp[i] + result;
}
return result;
}
public static void main(String[] args)
{
String s1 = ": Reverse words in a given string";
System.out.println(reverseWords(s1));
}
}
Output:- string given a in words Reverse :

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

Q9 Write a Java Program to remove double sequence characters from given i


nput String Input: bbthhfccuukkllrr
public class removeduplicatecharacter{
public static void main(String s[]){
String str=": bbthhfccuukkllrr";
System.out.println("old string is :"+str);
System.out.println("new string is :"+removeduplicate(str));
}
public static String removeduplicate(String str){
String newstr="";
for(int i=0;i<str.length();i++)
{
char ch =str.charAt(i);
if(newstr.indexOf(ch) ==-1)
{
newstr+=ch;
}
}
return newstr;
}
}
OUTPUT:- old string is :: bbthhfccuukkllrr
new string is :: bthfcuklr

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");

for(String splits2: splits) {


System.out.println(splits2);
}
}
}
Output is:- Hellothisisa
book
on
java

You might also like