You are on page 1of 19

String

Advanced programs
Question 1
Write a program to Reverse the given String

Sample Input: Sample Output:


+-*/% %/*-+
1 public class Test
2 {
3 public static void main(String[] args)
4 {
5 reverseInputString("+-*/%");
6 }
7 private static void reverseInputString(String input)
8 {
9 StringBuilder sb = new StringBuilder(input);
10 String result = sb.reverse().toString();
11 System.out.println(result);
12 }
13 }
14
15
16
17
18
19
20
21
22
Question 2
Write a program to check whether the given string is a palindrome or not

Sample Input: Sample Output:


abcba abcba is a palindrome
1 public class Test {
2 public static void main(String[] args){
3 checkPalindromeString("abcba");
4 }
5 private static void checkPalindromeString(String input)
6 {
7 boolean result = true;
8 int length = input.length();
9 for(int i=0; i < length/2; i++) {
10 if(input.charAt(i) != input.charAt(length-i-1))
11 {
12 result = false;
13 break;
14 }
15 }
16 if(result)
17 System.out.println(input + " is a palindrome");
18 else
19 System.out.println(input + " is not a palindrome");
20 }
21 }
22
Question 3
Write a program to remove the occurrence of a particular character from the
string

Sample Input: Sample Output:


Vit University Vt Unversty
i
1 import java.util.Scanner;
2 public class Test
3 {
4 public static void main(String[] args)
5 {
6 Scanner scan = new Scanner(System.in);
7 String s = scan.nextLine();
8 char a = scan.next().charAt(0);
9 removeCharFromString(s,a);
10 }
11 private static void removeCharFromString(String input, char c)
12 {
13 String result = input.replaceAll(String.valueOf(c), "");
14 System.out.println(result);
15 }
16 }
17
18
19
20
21
22
Question 4
Write a program to count the number of words in a given sentence

Sample Input: Sample Output:


I ate an Apple 4
1 public class Test
2 {
3 public static void main(String[] args)
4 {
5 countNumberOfWords("I ate an Apple");
6 }
7 private static void countNumberOfWords(String line)
8 {
9 String str = line.trim();
10 int count = str.isEmpty() ? 0 : str.split("\\s+").length;
11 System.out.println(count);
12 }
13 }
14
15
16
17
18
19
20
21
22
Question 5
Write a program to read two input from the user and check whether the
second string is a substring of first.

Sample Input: Sample Output:


Enter First String: velocity contains city = true
velocity
Enter Second String:
city
1 import java.util.Scanner;
2 public class Test{
3 public static void main(String[] args) {
4 Scanner scanner = new Scanner(System.in);
5 System.out.println("Enter First String:");
6 String s1 = scanner.nextLine();
7
8 System.out.println("Enter Second String:");
9 String s2 = scanner.nextLine();
10 scanner.close();
11
12 boolean result = Substring(s1, s2);
13 System.out.println(s1+" contains "+s2+" = "+result);
14 }
15 private static boolean Substring(String string, String substring) {
16 boolean result = false;
17 result = string.contains(substring);
18 return result;
19 }
20 }
21
22
Question 6
Write a program to move all the special characters to the end of the string

Sample Input: Sample Output:


V%^i&t *Uni@ver&sity *ve^llo#re Vit University vellore%^&*@&*^#
1 class Test {
2 static String movestring(String str)
3 {
4 int len = str.length();
5 String regx = "[a-zA-Z0-9\\s+]";
6 String res1 = "", res2 = "";
7 for (int i = 0; i < len; i++) {
8 char c = str.charAt(i);
9 if (String.valueOf(c).matches(regx))
10 res1 = res1 + c;
11 else
12 res2 = res2 + c;
13 }
14 return res1 + res2;
15 }
16 public static void main(String args[])
17 {
18 String str = "V%^i&t *Uni@ver&sity *ve^llo#re ";
19 System.out.println(movestring(str));
20 }
21 }
22
Question 7
Write a program to count the total number of occurrences of a given
character in a string without using any loop

Sample Input: Sample Output:


Enter a string: Character a is repeated 10 times
Java is java again java again
Enter a character:
a
1 Import java.util.Scanner;
2 class Test
3 {
4 public static void main(String[] args)
5 {
6 Scanner scan = new Scanner(System.in);
7 System.out.println(“Enter a String:”);
8 String s = scan.nextLine();
9 System.out.println(“Enter a character:”);
10 String c = scan.next();
11
12 int count = s.length() - s.replace(c, "").length();
13
14 System.out.println("Character "+c+" is repeated "+count+" times");
15 }
16 }
17
18
19
20
21
22
Question 8
Write a program to get two string inputs from the user and print the index
of the substring

Sample Input: Sample Output:


Welcome to Face Face found at index 11
1 import java.util.Scanner;
2 public class Test
3 {
4 public static void main(String[] args)
5 {
6 Scanner scan = new Scanner(System.in);
7 String str = scan.nextLine();
8 String s = scan.next();
9 int index = str.indexOf(s);
10
11 if(index == - 1)
12 System.out.println("Given word not found");
13 else
14 System.out.println(s+" found at index "+index);
15 }
16 }
17
18
19
20
21
22
THANK YOU

You might also like