Q1)Check the String is empty or not?
Ans:
public class MainClass{
public static void main(String []args){
String s = "";
[Link]("String is empty: " + [Link]());
}
}
===================================================================================
================
Q2)Finding length of String?
Ans:
public class MainClass{
public static void main(String []args){
String s = "Developer";
[Link]("String length: " + [Link]());
}
}
===================================================================================
================
Q3)Printing String object?
Ans:
public class MainClass{
public static void main(String []args){
String s = "Developer";
[Link]("String is: " + s);
}
}
===================================================================================
================
Q4)Comparing String objects?
Ans:
public class MainClass{
public static void main(String []args){
String s1 = "developer";
String s2 = "Developer";
[Link]("Both string are same: " + [Link](s2));
}
}
===================================================================================
================
Q5)Reading character at the given index
Ans:
public class MainClass{
public static void main(String []args){
String s = "Developer";
[Link]("char at given index: " + [Link](3));
}
}
===================================================================================
================
Q6)Finding index of given character
Ans:
public class MainClass{
public static void main(String []args){
String s = "Developer";
[Link]("index of given char: " + [Link]('p'));
}
}
===================================================================================
================
Q7)Check the String starts with given character.
Ans:
public class MainClass{
public static void main(String []args){
String s = "Developer";
[Link]("String starts with given char: " +
[Link]("d"));
}
}
===================================================================================
================
Q8)Check the given String ends with given character.
Ans:
public class MainClass{
public static void main(String []args){
String s = "Developer";
[Link]("String ends with given char: " + [Link]("r"));
}
}
===================================================================================
================
Q9)Concatenating two different string.
Ans:
public class MainClass{
public static void main(String []args){
String s1 = "Java";
String s2 = " Developer";
[Link]("String after Concatenating: " + [Link](s2));
}
}
===================================================================================
================
Q10)Replacing old character with new character.
Ans:
public class MainClass{
public static void main(String []args){
String s = "java";
[Link]("String after replacing char: " + [Link]("j",
"J"));
}
}
===================================================================================
================
Q. Reverse the String and print it.
Input as: Apple
Output as: elppa
Ans:
public class MainClass{
public static String stringReverse(String s){
char[] ch = [Link]();
for(int i=0; i<[Link]/2; i++){
char t = ch[i];
ch[i] = ch[[Link]-i-1];
ch[[Link]-i-1] = t;
}
return [Link](ch);
}
public static void main(String []args){
String s = "java";
[Link]("String after reversed: " + stringReverse(s));
}
}
===================================================================================
================
Q. Check the String is palindrome or not.
Input as : madam
Output as: true
Ans:
public class MainClass{
public static boolean isPalindrome(String s){
char[] ch = [Link]();
for(int i=0; i<[Link]/2; i++) if(ch[i] != ch[[Link]-i-1]) return
false;
return true;
}
public static void main(String []args){
String s = "madam";
[Link]("String is palindrome: " + isPalindrome(s));
}
}
===================================================================================
================
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
Ans:
public class MainClass{
public static void findOccurrence(String s){
char[] ch = [Link]();
for(int i=0; i<[Link]; i++) {
int c = 1;
for(int j=i+1; j<[Link]; j++) if(ch[i]!='*' && ch[i]==ch[j]) {
c++;
ch[j] = '*';
}
if(ch[i]!='*') [Link](ch[i] + " Occurrence: " + c);
}
}
public static void main(String []args){
String s = "Developer";
findOccurrence(s);
}
}
===================================================================================
================
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
Ans:
import [Link];
public class MainClass{
public static void convertFirstLetterToUpperCase(String s){
String []sa = [Link]("\\s");
for(int i=0; i<[Link]; i++)
sa[i] = [Link](sa[i].charAt(0)) + sa[i].substring(1);
[Link]([Link](" ", sa));
}
public static void main(String []args){
String s = "india is a great nation";
convertFirstLetterToUpperCase(s);
}
}
===================================================================================
================
Q. Remove all whitespaces from the string.
Input as: Everyone is feeling sleepy.
Output as: Everyoneisfeelingsleepy.
Ans:
public class MainClass{
public static void removeWhiteSpaces(String s){
// [Link]([Link](" ", ""));
char []ch = [Link]();
for(int i=0; i<[Link]; i++) if(ch[i]!=' ') [Link](ch[i]);
}
public static void main(String []args){
String s = "Everyone is feeling sleepy";
removeWhiteSpaces(s);
}
}
===================================================================================
================
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
Ans:
public class MainClass{
public static void splitBasedOnSpecialChar(String s){
String []sa = [Link]("\\p{Punct}");
for(int i=0; i<[Link]; i++) [Link](sa[i]);
}
public static void main(String []args){
String s = "Complete#all!the@question";
splitBasedOnSpecialChar(s);
}
}
===================================================================================
================
Q. Remove all digit from a given String.
Ans:
public class MainClass{
public static void removeNumbers(String s){
[Link]([Link]("\\d", ""));
}
public static void main(String []args){
String s = "21B65A0452";
removeNumbers(s);
}
}
===================================================================================
================
Q. Count number of vowel, consonant, digit and whitespace.
Ans:
public class MainClass{
public static void countAll(String s){
char[] ch = [Link]();
int vc = 0, cc = 0, dc= 0, space = 0;
for(int i=0; i<[Link]; i++){
if([Link](ch[i])){
char t = [Link](ch[i]);
if(t=='a'|| t=='e'|| t=='i'|| t=='o'|| t=='u') vc++;
else cc++;
}
else if([Link](ch[i])) dc++;
else if([Link](ch[i])) space++;
}
[Link]("Vowels : "+vc+"\nConsonant : "+cc+"\nSpace :
"+space+"\nDigit : "+dc);
}
public static void main(String []args){
String s = "Java Batch35";
countAll(s);
}
}
===================================================================================
================
Q. Sort the String in ascending and descending order.
Ans:
import [Link];
public class MainClass{
public static void sortString(String s){
char[] ch = [Link]();
[Link](ch);
[Link]("String in ascending order: "+ [Link](ch));
for(int i=0; i<[Link]/2; i++){
char t = ch[i];
ch[i] = ch[[Link]-i-1];
ch[[Link]-i-1] = t;
}
[Link]("String in descending order: "+ [Link](ch));
}
public static void main(String []args){
String s = "Java BAtch35";
sortString(s);
}
}
===================================================================================
================
Q. Print all unique characters from a given String
Ans:
public class MainClass{
public static void printUniqueCharacters(String s){
char[] ch = [Link]();
for(int i=0; i<[Link]; i++) {
int j=0;
for(; j<[Link]; j++) if(i!=j && ch[i]==ch[j]) break;
if(j == [Link]) [Link](ch[i]+" ");
}
}
public static void main(String []args){
String s = "Developer";
printUniqueCharacters(s);
}
}
===================================================================================
================
Q. Print all duplicate characters from a String.
Ans:
public class MainClass{
public static void printduplicateCharacters(String s){
char[] ch = [Link]();
for(int i=0; i<[Link]; i++) {
int c = 0;
for(int j=i+1; j<[Link]; j++) if(ch[i]==ch[j]) c++;
if(c == 1) [Link](ch[i]+" ");
}
}
public static void main(String []args){
String s = "Java Developer";
printduplicateCharacters(s);
}
}
===================================================================================
================
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
Ans:
public class MainClass{
public static void replaceSpace(String s){
[Link]([Link](" ", ","));
}
public static void main(String []args){
String s = "Everyone is feeling sleepy";
replaceSpace(s);
}
}