String Interview Question Coding With Solution Of problems

  • By
  • December 5, 2022
  • JAVA
Java String

String Interview Question Coding With Solution  Of problems

Reverse String 

public class A { 

public static void main(String[] args) { 

//reverse string  

 String nm=”madam”; 

 String rev=””; 

 String temp; 

 System.out.println(“origial string :-“+ nm); 

 for(int i=nm.length(); i>0; –i ) 

 { 

// System.out.print(nm.charAt(i-1)); 

 rev=rev+nm.charAt(i-1); 

  

 } 

 temp=rev; 

 System.out.println(“reverse String:”+rev); 

 if(temp.equals(nm)) 

 { 

 System.out.println(“string are equal”); 

 }else 

 { 

 System.out.println(“string are not equal”); 

 } 

  

}

Example2 

public class s8 { 

public static void main(String[] args) { 

//reverse string  

 String nm=”mam”; 

 System.out.println(“origial string :-“+ nm); 

 for(int i=nm.length(); i>0; –i ) 

 { 

 System.out.print(nm.charAt(i-1)); 

 } 

======================================================================== Equality 

public class Equality { 

public static void main(String[] args) { 

String str=”meena”;

if(str==”MEENA”) 

System.out.println(“equal”); 

}else 

System.out.println(“not equal”); 

// equal function 

if(str.equalsIgnoreCase(“MEENA”))// meena==MEENA 

System.out.println(“equal”); 

}else 

System.out.println(“not equal”); 

============================================================================== Length Of String 

public class s7len { 

void fun() 

int i=0; 

try { 

String s1=”SevenMentore”;

for( i=0;;i++) 

s1.charAt(i); 

System.out.print(s1.charAt(i)); 

System.out.println( 

i); 

}catch (Exception e) { 

// TODO: handle exception 

System.out.println(i);  

public static void main(String[] args) { 

s7len s1=new s7len(); 

s1.fun(); 

 

For Free, Demo classes Call: 020-71173125
Registration Link: Click Here!

 

Example 2 

public class sample6Length { 

public static void main(String[] args) { 

 

 String s1=”SevenMentore”; 

 int i = 0; 

 for(char c : s1.toCharArray()) 

 { 

 System.out.println(c); 

 i++; 

 } 

 System.out.println(“legth :”+i); 

Expand your skills by joining SevenMentor for Java Training in Pune by the IT experts.

================================================================================= Palimdrom Number 

public class s9PalimdromString { 

public static void main(String[] args) { 

//reverse string  

 String nm=”madam”; 

 String rev=””; 

 String temp; 

 System.out.println(“origial string :-“+ nm); 

 for(int i=nm.length(); i>0; –i ) 

 { 

// System.out.print(nm.charAt(i-1)); 

 rev=rev+nm.charAt(i-1); 

  

 } 

 temp=rev;

 System.out.println(“reverse String:”+rev); 

 if(temp.equals(nm)) 

 { 

 System.out.println(“string are equal palimdrom”); 

 }else 

 { 

 System.out.println(“string are not equal”); 

 } 

  

 

For Free, Demo classes Call: 020-71173125
Registration Link: Click Here!

 

====================================================================== endWith() 

public class sample10 { 

public static void main(String[] args) { 

// endWith() 

String n=”Seven MEntore”; 

System.out.println(n.endsWith(“tore”)); 

System.out.println(n.endsWith(“n”)); 

System.out.println(n.endsWith(” “)); 

System.out.println(n.endsWith(“E”)); 

=======================================================

indexOf() 

public class sample11 { 

public static void main(String[] args) { 

// TODO Auto-generated method stub 

 //indexOf() 

String n=”Seven is M is Entore is”; 

System.out.println(n.indexOf(‘v’));//2 

System.out.println(n.indexOf(“ven”));//2 

System.out.println(n.indexOf(“is”));//6 

System.out.println(n.indexOf(“is”,8)); 

=================================================================== contains() 

public class sample12 { 

public static void main(String[] args) { 

//contains()  

String nm=”welcome to my java class”; 

System.out.println(nm.contains(“java”)); 

=============================================================================

compareTo 

public class sample13 { 

public static void main(String[] args) { 

//if n1>n2 it retruns positiv enum 

 //if n1<n2 it return negative number 

 //if n1==n2 0 equal 

 String n1=”neeta”; 

 String n2=”neeta”; 

 String n3=”mira”; 

 String n4=”himanshi “; 

 System.out.println(n1.compareTo(n2)); 

 System.out.println(n1.compareTo(n3)); 

 System.out.println(n4.compareTo(n1)); 

// n1.compareToIgnoreCase(n4);   

================================================================================== replaceAll 

public class sample14 { 

public static void main(String[] args) { 

//replace 

String s1=”welcome to my class…”; 

String s2=s1.replace(“class”, “seven mentore”); 

System.out.println(s2);

String s3=”i like to learn java…i want to learn java”; 

String s4=s3.replaceAll(“java”, “html”); 

System.out.println(s4); 

================================================================================= split 

public class sample15 { 

public static void main(String[] args) { 

//split 

String s1=”welcome to my class”; 

String word[]=s1.split(“\\s”); 

for(String s : word) 

System.out.println(s); 

=============================================================================== toUpperCase() toLowerCase() 

public class Sample16 {

public static void main(String[] args) { 

String n=”meena”; 

String n1=”SEVEN”; 

System.out.println(n.toUpperCase()); 

System.out.println(n1.toLowerCase()); 

================================================================================== Trim() 

public class sample17Trim { 

public static void main(String[] args) { 

//trim 

//the unicode value of space charecter is ‘\u0020’ 

String s1=” Welcome “; 

System.out.println(“legth of string: “+s1.length()); 

// System.out.println(s1.trim().length()); 

System.out.println(s1+”to my class”); 

String s2 = s1+”to my class”; 

System.out.println(“legth of string: “+s2.length()); 

// 

System.out.println(s1.trim()+”to my class”);

String s3=s1.trim()+”to my class”; 

System.out.println(“legth of string: “+s3.length()); 

//  

// 

================================================================================== Join() 

public class StringJoin { 

public static void main(String[] args) { 

String s1=String.join(“$”, “welcome”,”to”,”my”,”class”); 

System.out.println(s1); 

// 

 String date=String.join(“-“, “29”,”08″,”2022″); 

 String time=String.join(“:”,”06″,”17pm”); 

//  

 System.out.println(time); 

 System.out.println(date); 

================================================================================== public class sample1 {

public static void main(String[] args) { 

String s1=”Seven mentore”; 

System.out.println(s1); 

String s2=new String(“heena”); 

System.out.println(s2); 

char nm[]= {‘h’,’i’}; 

System.out.println(nm); 

================================================================================== Immutable String 

public class sample2 { 

public static void main(String[] args) { 

 String s1=”Seven”; 

 System.out.println(s1);//seven 

 s1.concat(“Mentor”); 

 System.out.println(s1);// seven Mentor 

// immmutable 

  

 String s2= s1.concat(“Mentor”); 

 System.out.println(s2); 

  

 s1=s1.concat(“Mentor”); 

 System.out.println(s1); 

  

 

For Free, Demo classes Call: 020-71173125
Registration Link: Click Here!

 

========================================================================= To Remove White Spaces Between String 

public class RemoveAllSpace {  

 public static void main(String[] args) {  

 String str = “welcom to my class…i m going to teach you string…..”;  

 String n1 = str.replaceAll(“\\s”, “”); 

 System.out.println(n1 );  

  

 char[] str11 = str.toCharArray();  

 StringBuffer stringBuffer = new StringBuffer();  

int j; 

 for ( j = 0; j< str11.length; j++) {  

 if ((str11[j] != ‘ ‘) && (str11[j] != ‘\t’)) {  

 stringBuffer.append(strArray[i]);  

 }  

 }  

 String n2= stringBuffer.toString();  

 System.out.println(n2);  

 }  

=================================================================================

To reverse each word in string 

public class Formatter {  

public static String reverseWord11(){ 

 String str=”welcome to seven mentore.”;  

 String words[]=str.split(“\\s”);  

 String revWord=””;  

 for(String w:words){  

 StringBuilder sb=new StringBuilder(w);  

 sb.reverse();  

 revWord+=sb.toString()+” “;  

 }  

 return revWord.trim();  

}  

}  

================================================================================== to check whether one string is a rotation of another string or not? 

public class RotatString11 {  

 public static boolean checkRotation11(String st11, String st22) {  

 if (st11.length() != st22.length()) {  

 return false;  

 }  

 String st33 = st11 + st11;  

 if (st33.contains(st22))  

 return true;  

 else  

 return false;  

 }  

  

 public static void main(String[] args) { 

 String str11 = “avajava”;  

 String str22 = “javaava”;  

  

 if (checkRotation11(str11, str22)) {  

 System.out.println( str22 + ” it is rotation of ” + str11);   } else {  

 System.out.println(str22+ ” it is not rotation of ” + str11);   }  

 }  

}

 

Author:-

Pooja Ghodekar

Call the Trainer and Book your free demo Class  Call now!!!

| SevenMentor Pvt Ltd.

© Copyright 2021 | Sevenmentor Pvt Ltd.

Submit Comment

Your email address will not be published. Required fields are marked *

*
*