Q1)Check the String is empty or not?
Q2)Finding length of String?
Q3)Printing String object?
Q4)Comparing String objects?
Q5)Reading character at the given index
Q6)Finding index of given character
Q7)Check the String starts with given character.
Q8)Check the given String ends with given character.
Q9)Concatenating two different string.
Q10)Replacing old character with new character.
=================================================
/* Q. Reverse the String and print it.
Input as: Apple
Output as: elppa
//Ques - 1(Reverse)
String s = "Apple";
//Approach - 1 (print from last index)
for(int i = s.length()-1;i>=0;i--) {
System.out.print(s.charAt(i));
}
System.out.println();
//Approach - 2(char[]) 0 1 2 3 4
char[] ch = s.toCharArray();// e l p p A
for(int i = 0;i<ch.length/2;i++) { // i = 2
char temp= ch[i]; // p
ch[i] = ch[ch.length-1-i];
ch[ch.length-1-i] = temp;
}
//String res = new String(ch);
String res = String.valueOf(ch);
System.out.println(res);
//Approach(Predefined method and class)
StringBuilder sb = new StringBuilder(s);
sb.reverse();
//String res2 = new String(sb);
//String res2 = String.valueOf(sb);
String res2 = sb.toString();
System.out.println(res2);
/* Q. Check the String is palindrome or not.
Input as : madam
Output as: true
public class Interview_01 {
public static boolean isPalindrome(String s ) {
//Approach #2
char[] ch = s.toCharArray();
//0 1 2 3 4 m a l a y a l a m
//m a d a m
for(int i = 0;i<=ch.length/2;i++) {
if(ch[i]!=ch[ch.length-1-i]) {
return false;
}
}
return true;
}
public static void main(String[] args) {
//Ques - 2
//Approach #1
String s = "madam";
StringBuilder sb = new StringBuilder(s);
sb.reverse();
System.out.println(sb);
if(s.contentEquals(sb)) {
System.out.println(s+" is pallindrome");
}else {
System.out.println(s+" is not palindrome");
}
//Approach #2
boolean res = isPalindrome(s);
if(res) {
System.out.println(s+" is pallindrome");
}else {
System.out.println(s+" is not palindrome");
}
}
}
/* Q. Print the occurrence/frequency of each character from a given string.
Input as: Hello world
Output as:
H: 1
e: 1
l: 3
o: 2
r: 1
d: 1
public static void main(String[] args) {
String s = "Hello world";
/*
//Approach #1(by replacing the duplicate char)
// 0 1 2 3 4 5 6 7 8 9 10
// H e l * o w o r * d
char[] ch = s.toCharArray();
for(int i = 0;i<ch.length;i++) { // i = 3
int c = 1; // 3
for(int j = i+1;j<ch.length;j++) {// j = 11
if(ch[i]==ch[j] && ch[i]!='#') {
c++;
ch[j] = '#';
}
}
if(ch[i]!='#')
System.out.println(ch[i] +" = "+c);
}
*/
/*
//Approach #2: (Creating a boolean[])
// 0 1 2 3 4 5 6 7 8 9 10 -> char[]
// H e l l o w o r l d
// 0 1 2 3 4 5 6 7 8 9 10 -> boolean[]
// F F F T F F F F F T F
char[] ch = s.toCharArray();
boolean[] found = new boolean[ch.length];
for(int i = 0;i<ch.length;i++) {
int c = 1;
for(int j = i+1;j<ch.length;j++) {
if(ch[i]==ch[j] && found[i]!=true) {
c++;
found[j] = true;
}
}
if(found[i]!=true) {
System.out.println(ch[i]+" = "+c);
}
}
*/
/*
//Approach #3
// 0 1 2 3 4 5 6 7 8 9 10 -> char[]
// H e l l o w o r l d
char[] ch = s.toCharArray();
for(int i = 0;i<ch.length;i++) { // i = 3
int c = 1, found = 0;// c = 1, f = 1
for(int j = 0;j<ch.length;j++) { // j = 2
if(ch[i]==ch[j] && i<j) c++;
if(ch[i]==ch[j] && i>j) {
found = 1; break;}
}
if(found==0 ) {
System.out.println(ch[i] +" = "+ c);//l = 3
}
}
*/
//Approach #4
// 0 1 2 3 4 5 6 7 8 9 10 -> char[]
// A B A C a B C a a a C
int[] count = new int[256]; // 65 : 2 66: 2 67 : 3 97: 4
for(char character : s.toCharArray()) {
count[character]++;
}
for(int i = 0;i<count.length;i++) {
if(count[i]!=0)
System.out.println((char)i +" = "+count[i]);
}
}
Q. Convert each word first letter to uppercase of a String and print the String.
Input as: India is a great nation.
Output as: India Is A Great Nation
Q. Remove all whitespaces from the string.
Input as: Everyone is feeling sleepy.
Output as: Everyoneisfeelingsleepy.
Q. Split the string based on special characters and print every word separately.
Input as: Complete#all!the@question
Output as: Complete
all
the
question
Q. Remove all digit from a given String.
Q. Count number of vowel, consonant, digit and whitespace.
Q. Sort the String in ascending and descending order.
Q. Print all unique characters from a given String
Q. Print all duplicate characters from a String.
Q. You have a given sentence as a String, separated by spaces.
Remove the spaces and separate it by ',' and print the String.
Input as : India is a country
Output as : India,is,a,country
*/
====================================
package com.nit.String;