This site "java learning concept" is very useful for programming languages like JAVA,PYTHON,C,C++, Data Structures etc.Java learning Concept also provides technical materials , which are related to latest technologies. We provides very easy lectures to our viewers from beginning to advance level.We focus logical content instead of theoretical content.Our coding lecture are just fun and hope you will enjoy.

Thursday, 9 January 2020

String important interview questions in java


1. Write a program to find duplicate characters in java(using Mapping).


import java.util.*;
class Duplicate
{
public static void main(String[] args)
{
System.out.println("Dulicate char in java::");
findDuplicates("java");
System.out.println("Dulicate char is Programming::");
findDuplicates("programming");
}
public static void findDuplicates(String str)
{
HashMap<Character,Integer> m=new HashMap<>();
char[] ch=str.toCharArray();
for(char c1:ch)
{
if(m.containsKey(c1))
{
m.put(c1,m.get(c1)+1);
}
else
{
m.put(c1,1);
}
}
Iterator<Character> itr=m.keySet().iterator();
while(itr.hasNext())
{
char ch1=itr.next();
if(m.get(ch1)>1)
{
System.out.println(ch1+": " + m.get(ch1));
}
}
}
}

Output:
Duplicate char in java:a: 2
Duplicate char in Programming:r:2 g:2 m:2

2.Write a program to find duplicate words in java.


import java.util.*;
class Test
{
public static void main(String[] args)
{
findDuplicatesWords("My java java oh my java oh my java");
}
public static void findDuplicatesWords(String s)
{
String[] str=s.split(" ");
HashMap<String,Integer> m=new HashMap<>();
for(String s1:str)
{
if(m.get(s1)!=null)
{
m.put(s1,m.get(s1)+1);
}
else
{
m.put(s1,1);
}
}
Iterator<String> itr=m.keySet().iterator();
while(itr.hasNext())
{
String temp=itr.next();
if(m.get(temp)>1)
{
System.out.println(temp+"::"+m.get(temp));
}
}
}
}

Output:
java::4
oh::2
my::2

3.Write a program to reverse a given string in java in java .


import java.util.Scanner;
class Test
{
static String revStr="";
static String origStr;
public static void main(String[] args)
{

reverseString();



}

public static void reverseString()
{
System.out.println("Enter a string for reverse");
Scanner sc=new Scanner(System.in);
origStr=sc.nextLine();
int len=origStr.length();
for(int i=len-1;i>=0;i--)
{
revStr=revStr+origStr.charAt(i);
}

System.out.println("orignal string is: "+ origStr);
System.out.println("reverseString  string is: "+ revStr);

}

}

OUTPUT:
java
orignal string is: java
reverseString  string is: avaj

4. Write a program to check whether string is palindrome or not in java .

import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
String revStr="";
String origStr;
System.out.println("Enter a string for reverse");
Scanner sc=new Scanner(System.in);
origStr=sc.nextLine();
int len=origStr.length();
for(int i=len-1;i>=0;i--)
{
revStr=revStr+origStr.charAt(i);
}
if(origStr.equals(revStr))
{
System.out.println("String is palindrome!!");
}
else
{
System.out.println("String is not a palindrome!!");
}
}
}

OUTPUT:
Enter a string for reverse
java
String is not a palindrome!!


5.Write a program to count total number of characters in the given String  in java By input to user.

import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
String str;
System.out.println("Enter a string:");
Scanner sc=new Scanner(System.in);
str=sc.nextLine();

int len=str.length();
int count=0;
for(int i=0;i<len;i++)
{
count++;

}

System.out.println("No of characters in string are:"+count);
}
}

OUTPUT:
Enter a string:
viren
No of characters in string are:5

No comments:

Post a Comment

Adbox