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

Questions With Solutions - String Level 1

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)
39 views

Questions With Solutions - String Level 1

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/ 27

Problem 1:

Write a java Program to Replace a sub-string in a string.

import java.util.*;

class Replacestring {
static void modifyString(String s, String s1, String s2) {
String ans = "";
for (int i = 0; i < s.length(); i++) {
int k = 0;
if (s.charAt(i) == s1.charAt(k) && i + s1.length() <= s.length()) {
int j;
for (j = i; j < i + s1.length(); j++) {
if (s.charAt(j) != s1.charAt(k)) {
break;
} else {
k = k + 1;
}
}
if (j == i + s1.length()) {
ans += (s2);
i = j - 1;
} else {
Solution:
ans += (s.charAt(i));
}
} else {
ans += (s.charAt(i));
}
}
System.out.print(ans);
}

// Driver Code
public static void main(String[] args) {
try (Scanner sc = new Scanner(System.in))
{ System.out.print("Enter a string:
"); String S = sc.nextLine();

System.out.print("Enter the string to be replaced: ");


String S1 = sc.nextLine();

System.out.print("Enter the replacing string: ");


String S2 = sc.nextLine();

modifyString(S, S1, S2);


}
}
}
output:

Enter a string: Calcutta the city of joy


Enter the string to be replaced: Calcutta
Enter the replacing string: kolkata
kolkata the city of joy
Problem 2:
Write a java programming to convert vowels into upper case character in a given
string.

import java.util.*;

public class Lowertoupper {


static void conVowUpp(char[] str) {

int N = str.length;
for (int i = 0; i < N; i++) {
if (str[i] == 'a' || str[i] == 'e' ||
str[i] == 'i' || str[i] == 'o' ||
str[i] == 'u') {
char c = Character.toUpperCase(str[i]);
str[i] = c;
}
}
for (char c : str)
System.out.print(c);
}
Solution:

// Driver Code
public static void main(String[]
args) {

try (Scanner sc = new


Scanner(System.in)) {
System.out.print("Enter a
string: ");
String str =
sc.nextLine();

conVowUpp(str.toCharArray());
}
}
}
output:

Enter a string: constellation


cOnstEllAtIOn
Problem 3:
Write a java programming to convert a given roman number to an integer.

import java.util.*;

public class RomantoDecimal


{ // This function returns
// value of a Roman symbol
int value(char r) {
if (r == 'I')
return 1;
if (r == 'V')
return 5;
if (r == 'X')
return 10;
if (r == 'L')
return 50;
if (r == 'C')
return 100;
if (r == 'D')
return 500;
if (r == 'M')
return 1000;
return -1;
Solution:
}

int romanToDecimal(String str) {


// Initialize result
int res = 0;
for (int i = 0; i < str.length();
i++) {
int s1 =
value(str.charAt(i));
if (i + 1 < str.length()) {
int s2 =
value(str.charAt(i + 1));
if (s1 >= s2) {
res = res + s1;
} else {
res = res + s2 - s1;
i++;
}
} else {
res = res + s1;
}
}
return res;
}
Solution:

// Driver Code
public static void main(String args[])
{ RomantoDecimal ob = new RomantoDecimal();
try (Scanner sc = new Scanner(System.in)) {

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


String str = sc.nextLine();
System.out.println("Integer form of Roman
Numeral" + " is " + ob.romanToDecimal(str));
}

}
}
output:

Enter a string: XXVIIM


Integer form of Roman Numeral is 1025
Problem 4:

import java.util.*;

public class StrStr {

// Recursive function to implement `strstr()` function


public static String strstr(String X, String Y) {
if (Y == null || Y.length() == 0) {
return X;
}

for (int i = 0; i < X.length(); i++) {


if (X.charAt(i) == Y.charAt(0)) {
String s = strstr(X.substring(i + 1), Y.substring(1));
return (s != null) ? X.charAt(i) + s : null;
}
}

return null;
}
Solution:
public static void main(String[] args) {

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


{ System.out.print("Enter a string: ");
String X = sc.nextLine();
System.out.print("Enter the sub string: ");
String Y = sc.nextLine();
System.out.println(strstr(X, Y));
}
}
}
output:

Enter a string: The quick brown fox jumps


over the lazy dog
Enter the sub string: jumps
jumps over the lazy dog
Problem 5:
Write a program in java to count the number of punctuation characters exists in a
string.

import java.util.*;

public class punctuation {

public static void main(String[] args) {


// Stores the count of punctuation marks
int countPuncMarks = 0;
try (Scanner sc = new Scanner(System.in)) {
System.out.print("Enter a string: ");
String str = sc.nextLine();
for (int i = 0; i < str.length(); i++) {
// Checks whether given character is punctuatio nmark
if (str.charAt(i) == '!' || str.charAt(i) == ',' || str.charAt(i) == ';' ||
str.charAt(i) == '.'
|| str.charAt(i) == '?' || str.charAt(i) == '-' ||
str.charAt(i) == '\'' || str.charAt(i) == '\"' || str.charAt(i) == ':') {
countPuncMarks++;
}
Solution:
}
}
System.out.println("Total number of punctuation
characters exists in string:" + countPuncMarks);
}
}
Output:

Enter a string: In the year 2022, Argentina Won the World Cup 3rd time:
Messi is a great Player.
Total number of punctuation characters exists in string:3
Problem 6:
Write a program in java to check whether a character is digit or not.

import java.util.*;

public class chkDigit {


public static void main(String[] args) {
try (Scanner sc = new Scanner(System.in))
{ System.out.println("Checking for
digit..."); System.out.println("Enter: ");
String str = sc.nextLine();
for (int i = 0; i < str.length(); i++) {
Boolean flag = Character.isDigit(str.charAt(i));
if (flag) {
System.out.println("'" + str.charAt(i) + "' is a Digit");
} else {
System.out.println("'" + str.charAt(i) + "' is a Letter");
}
}
}
}
}
output:

Checking for digit...


Enter:
Supramyc1@n
'S' is a Letter
'u' is a Letter
'p' is a Letter
'r' is a Letter
'a' is a Letter
'm' is a Letter
'y' is a Letter
'c' is a Letter
'1' is a Digit
'@' is a Letter
'n' is a Letter
.
Problem 7:
Write a program in java to check whether a letter is lowercase or not.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ChkLowercase {


public static void main(String args[]) throws IOException {
char m;
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter any alphabet:");
m = (char) bf.read();
if (m >= 97 && m <= 123)
{ System.out.println("Lower
Case");
} else if (m >= 65 && m <= 96)
{ System.out.println("Upper
Case");
} else if (m >= 48 && m <= 57) {
System.out.println("Digit");
}
}
}
output:

Enter any alphabet:C


Upper Case
Enter any alphabet:c
Lower Case
Enter any alphabet:2
Digit
Problem 8:
Write a program in java to replace the spaces of a string with a specific character.

Solution:

import java.util.*;

public class SpaceReplace {


// Function to replace Space with -
static String replaceStr(String str) {
String s = "";
for (int i = 0; i < str.length(); ++i) {
if (str.charAt(i) == ' ')
s += '*';
else
s += str.charAt(i);
}
return
s; }
Solution:
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("String before replacing


spaces with given character: ");
System.out.println(str);

// Print the modified string


System.out.println("String after replacing
spaces with given character: ");
System.out.println(replaceStr(str));
}
}
}
output:

Enter The String:


The quick brown fox jumps over the lazy dog
String before replacing spaces with given character:
The quick brown fox jumps over the lazy dog
String after replacing spaces with given character:
The*quick*brown*fox*jumps*over*the*lazy*dog

You might also like