Java String Programs – Interview & Practice Sheet
1. Check if a given string is palindrome or not
public class Palindrome {
public static void main(String[] args) {
String str = "madam";
String rev = new StringBuilder(str).reverse().toString();
if([Link](rev))
[Link]("Palindrome");
else
[Link]("Not Palindrome");
}
}
2. Count number of vowels, consonants and spaces
String str = "Hello World";
int v=0,c=0,s=0;
for(char ch: [Link]()){
if("aeiouAEIOU".indexOf(ch)!=-1) v++;
else if(ch==' ') s++;
else if([Link](ch)) c++;
}
[Link]("Vowels="+v+" Consonants="+c+" Spaces="+s);
3. Find ASCII value of character
char ch = 'A';
[Link]((int)ch);
4. Remove all vowels from string
String str="Hello World";
[Link]([Link]("[aeiouAEIOU]",""));
5. Remove spaces from string
String str="Hello World";
[Link]([Link](" ",""));
6. Remove characters except alphabets
String str="H3llo@123";
[Link]([Link]("[^a-zA-Z]",""));
7. Reverse a string
String str="Hello";
String rev=new StringBuilder(str).reverse().toString();
[Link](rev);
8. Remove brackets from expression
String str="(a+b)*(c+d)";
[Link]([Link]("[(){}\[\]]",""));
9. Sum of numbers in string
String str="a1b2c3";
int sum=0;
for(char ch: [Link]()){
if([Link](ch))
sum+=ch-'0';
}
[Link](sum);
10. Capitalize first and last letter of each word
String str="hello world";
String[] words=[Link](" ");
for(String w:words){
String res=[Link]([Link](0))
+[Link](1,[Link]()-1)
+[Link]([Link]([Link]()-1));
[Link](res+" ");
}
11. Frequency of characters
String str="hello";
int freq[]=new int[256];
for(char ch:[Link]())
freq[ch]++;
for(int i=0;i<256;i++)
if(freq[i]>0)
[Link]((char)i+" = "+freq[i]);
12. Find non■repeating characters
String str="hello";
for(int i=0;i<[Link]();i++){
if([Link]([Link](i))==[Link]([Link](i)))
[Link]([Link](i)+" ");
}
13. Check anagram
import [Link];
String s1="listen";
String s2="silent";
char a[]=[Link]();
char b[]=[Link]();
[Link](a);
[Link](b);
[Link]([Link](a,b));
14. Maximum occurring character
String str="banana";
int freq[]=new int[256];
for(char ch:[Link]())
freq[ch]++;
int max=0;
char res=' ';
for(int i=0;i<256;i++){
if(freq[i]>max){
max=freq[i];
res=(char)i;
}
}
[Link](res);
15. Remove duplicates
String str="programming";
String result="";
for(char ch:[Link]()){
if([Link](ch)==-1)
result+=ch;
}
[Link](result);
16. Print duplicates
String str="programming";
for(int i=0;i<[Link]();i++){
if([Link]([Link](i))!=[Link]([Link](i)))
[Link]([Link](i)+" ");
}
17. Remove characters of second string from first
String s1="abcdef";
String s2="bd";
for(char ch:[Link]())
s1=[Link]([Link](ch),"");
[Link](s1);
18. Next lexicographic character
String str="abc";
for(char ch:[Link]())
[Link]((char)(ch+1));
19. Largest word in string
String str="Java is powerful language";
String words[]=[Link](" ");
String max="";
for(String w:words){
if([Link]()>[Link]())
max=w;
}
[Link](max);
20. Sort characters in string
import [Link];
char arr[]="dcba".toCharArray();
[Link](arr);
[Link](new String(arr));
21. Count number of words
String str="Hello world Java";
[Link]([Link](" ").length);
22. Word with highest repeated letters
String str="hello apple banana";
String words[]=[Link](" ");
int max=0;
String res="";
for(String w:words){
int freq[]=new int[256];
for(char ch:[Link]())
freq[ch]++;
int local=0;
for(int f:freq)
if(f>local) local=f;
if(local>max){
max=local;
res=w;
}
}
[Link](res);
23. Change case of each character
String str="HeLLo";
for(char ch:[Link]()){
if([Link](ch))
[Link]([Link](ch));
else
[Link]([Link](ch));
}
24. Concatenate two strings
String s1="Hello";
String s2="World";
[Link](s1+s2);
25. Find substring position
String str="hello world";
String sub="world";
[Link]([Link](sub));
26. Reverse words in string
String str="Java is awesome";
String words[]=[Link](" ");
for(int i=[Link]-1;i>=0;i--)
[Link](words[i]+" ");