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 = [Link]().toString();
11 [Link](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 = [Link]();
9 for(int i=0; i < length/2; i++) {
10 if([Link](i) != [Link](length-i-1))
11 {
12 result = false;
13 break;
14 }
15 }
16 if(result)
17 [Link](input + " is a palindrome");
18 else
19 [Link](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 [Link];
2 public class Test
3 {
4 public static void main(String[] args)
5 {
6 Scanner scan = new Scanner([Link]);
7 String s = [Link]();
8 char a = [Link]().charAt(0);
9 removeCharFromString(s,a);
10 }
11 private static void removeCharFromString(String input, char c)
12 {
13 String result = [Link]([Link](c), "");
14 [Link](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 = [Link]();
10 int count = [Link]() ? 0 : [Link]("\\s+").length;
11 [Link](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 [Link];
2 public class Test{
3 public static void main(String[] args) {
4 Scanner scanner = new Scanner([Link]);
5 [Link]("Enter First String:");
6 String s1 = [Link]();
7
8 [Link]("Enter Second String:");
9 String s2 = [Link]();
10 [Link]();
11
12 boolean result = Substring(s1, s2);
13 [Link](s1+" contains "+s2+" = "+result);
14 }
15 private static boolean Substring(String string, String substring) {
16 boolean result = false;
17 result = [Link](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 = [Link]();
5 String regx = "[a-zA-Z0-9\\s+]";
6 String res1 = "", res2 = "";
7 for (int i = 0; i < len; i++) {
8 char c = [Link](i);
9 if ([Link](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 [Link](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 [Link];
2 class Test
3 {
4 public static void main(String[] args)
5 {
6 Scanner scan = new Scanner([Link]);
7 [Link](“Enter a String:”);
8 String s = [Link]();
9 [Link](“Enter a character:”);
10 String c = [Link]();
11
12 int count = [Link]() - [Link](c, "").length();
13
14 [Link]("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 [Link];
2 public class Test
3 {
4 public static void main(String[] args)
5 {
6 Scanner scan = new Scanner([Link]);
7 String str = [Link]();
8 String s = [Link]();
9 int index = [Link](s);
10
11 if(index == - 1)
12 [Link]("Given word not found");
13 else
14 [Link](s+" found at index "+index);
15 }
16 }
17
18
19
20
21
22
THANK YOU