0% found this document useful (0 votes)
58 views25 pages

Questions With Solutions - String Level 2

Uploaded by

Manas Debnath
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)
58 views25 pages

Questions With Solutions - String Level 2

Uploaded by

Manas Debnath
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/ 25

Problem 1:

Write a java Program to Remove brackets from an algebraic expression.

import java.util.*;

public class Algebric {

public static void main(String[] args) {

try (Scanner sc = new Scanner(System.in))


{ System.out.println("Enter Algebraic expression bracket: ");
with String str = sc.nextLine();
String result = str.replaceAll("[(){}]", "");
System.out.println("Expression without brackets : " + result);
}
}
}
output:

Enter Algebraic expression with bracket:


[a+{b*c}/(d-e)]
Expression without brackets : [a+b*c/d-e]
Problem 2:
Write a java program to Permutations of a given string.
import java.util.*;

public class Permutation {

private void permute(String str, int l, int r) {


if (l == r) {
System.out.println(str);
} else {
for (int i = l; i <= r; i++) {
str = swap(str, l, i);
permute(str, l + 1, r);
str = swap(str, l, i);
}
}
}

public String swap(String a, int i, int j) {


char temp;
char[] charArray = a.toCharArray();
temp = charArray[i];
charArray[i] = charArray[j];
charArray[j] = temp;
Solution:

return String.valueOf(charArray);
}

public static void main(String[] args) {


try (Scanner sc = new Scanner(System.in)) {
System.out.println("Enter a String: ");
String str = sc.nextLine();
int n = str.length();
Permutation permutation = new Permutation();
System.out.println("After Permutation is: ");
permutation.permute(str, 0, n - 1);
}
}

}
output:

Enter a String:

RIP
After Permutation is:
RIP
RPI
IRP
IPR
PIR
PRI
Problem 3:
Write a java programming to find the longest palindromic substring of a given
string.Maximum length of the given string is 1000.

import java.util.*;

public class LongestPallindrom {


static int longestPalSubstr(String str) {
int n = str.length();
if (n < 2)
return n;
int maxLength = 1, start = 0;
int low, high;
for (int i = 0; i < n; i++) {
low = i - 1;
high = i + 1;
while (high < n && str.charAt(high) == str.charAt(i))
high++;

while (low >= 0 && str.charAt(low) == str.charAt(i))


low--;
Solution:
while (low >= 0 && high < n && str.charAt(low) == str.charAt(high)) {
low--;
high++;
}

int length = high - low - 1;


if (maxLength < length) {
maxLength = length;
start = low + 1;
}
}
System.out.print("Longest palindrome substring is: ");
System.out.println(str.substring(start, start + maxLength));
return maxLength;
}

public static void main(String[] args) {


try (Scanner sc = new Scanner(System.in))
{ System.out.println("Enter the String:
"); String str = sc.nextLine();
System.out.println("Length is: " + longestPalSubstr(str));
}
}
}
Solution:

Enter the String:


abbbaaccabaabcraaddaa
Longest palindrome substring is: aaddaa
Length is: 6
Problem 4:
Write a java program to check if string is rotated by two places.

import java.util.*;

public class RotatetwoPlace {


static boolean checkRotated(String str1, String str2) {
String s1 = "";
String s2 = "";
int len = str2.length();
if (str1.length() != str2.length())
return false;
s1 = str2.substring(len - 2, len) + str2.substring(0, len - 2);
s2 = str2.substring(0, 2) + str2.substring(0, 2);
return (str1.equals(s1) || str1.equals(s2));
}
Solution:

public static void main(String[] args) {


try (Scanner sc = new Scanner(System.in))
{ System.out.println("Enter 1st string
"); String str1 = sc.nextLine();

System.out.println("Enter 2nd string ");


String str2 = sc.nextLine();

System.out.println(checkRotated(str1, str2) ? "True" : "False");

}
}
output:

Enter 1st string

tut

Enter 2nd string

ttu

True

Enter 1st string

google

Enter 2nd string

legoog

False
Problem 5:
Write a program in java to split string by space into words.

import java.util.*;

public class Split {


public static void main(String args[]) {
try (Scanner sc = new Scanner(System.in))
{ System.out.println("Enter the String:
"); String s1 = sc.nextLine();
String[] words = s1.split("\\s");// splits the string
based on whitespace
// using java foreach loop to print elements of string
array
for (String w : words)
{ System.out.println(w
);
}
}
}
}
output:

Enter the String:


this is java split example
this
is
java
split
example
Problem 6:
Write a program in java to check whether a character is Hexadecimal Digit or not.

import java.util.*;

public class HexaChk {


public static void checkHex(String s) {
int n = s.length();
for (int i = 0; i < n; i++) {
char ch = s.charAt(i);
if ((ch < '0' || ch > '9') && (ch < 'A' || ch > 'F')) {
System.out.println("No");
return;
}
}
System.out.println("Yes");
}
Solution:

// Driver Code
public static void main(String[] args) {
try (// Given string
Scanner sc = new Scanner(System.in)) {
System.out.println("Enter the string: ");
String s = sc.nextLine();
checkHex(s);
}
}
}
output:

Enter the string:

BF57C
Yes
Enter the string:
58GK
No
Problem 7:
Write a program in java to find the largest and smallest word in a string.
import java.util.*;

public class LargestAndSmallestWord {


public static void main(String[] args) {
try (Scanner sc = new Scanner(System.in))
{ System.out.println("Enter the String:
"); String string = sc.nextLine();
String word = "", small = "", large = "";
String[] words = new String[100];
int length = 0;
string = string + " ";
for (int i = 0; i < string.length(); i++) {
if (string.charAt(i) != ' ') {
word = word + string.charAt(i);
} else {
words[length] = word;
length++;
word =
""; }
}
small = large = words[0];
for (int k = 0; k < length; k++) {
Solution:
if (small.length() > words[k].length())
small = words[k];
if (large.length() < words[k].length())
large = words[k];
}
System.out.println("Smallest word: " + small);
System.out.println("Largest word: " + large);
}

}
}
output:

Enter the String:


Hardships often prepare ordinary people for an extraordinary destiny
Smallest word: an
Largest word: extraordinary
Problem 8:
Write a program in java to find the number of times a given word 'the' appears in
the given string.
import java.util.*;
public class WordOccurance {
public static void main(String args[]) {
try (Scanner sc = new Scanner(System.in))
{ System.out.println("Enter the String:
"); // Spring is beautiful but so is
winter String string = sc.nextLine();
System.out.println("Enter the word: ");
String word = sc.nextLine();
String temp[] = string.split(" ");
int count = 0;
for (int i = 0; i < temp.length; i++) {
if (word.equals(temp[i]))
count++;
}
System.out.println("The word " + word + " occurs " + count + " times in the above
string");
}
}
}
output:

Enter the String:


the quick brown fox jumps over the lazy dog
Enter the word:
the
The word the occurs 2 times in the above string
Problem 9:
Write a program in java to read a string through keyboard and sort it using bubble
sort.
public class bubbleSort {

public static void main(String[] args) {


String str[] = { "Ajeet", "Steve", "Rick", "Becky",
"Mohan" };
String temp;
System.out.println("Strings in sorted order:");
for (int j = 0; j < str.length; j++) {
for (int i = j + 1; i < str.length; i++) {
// comparing adjacent strings
if (str[i].compareTo(str[j]) < 0) {
temp = str[j];
str[j] = str[i];
str[i] = temp;
}
output:

Strings in sorted order:


Ajeet
Becky
Mohan
Rick
Steve

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy