RECENTLY MOST VIEWS
MOST POPULAR
-
Note: A number will be called DISARIUM if sum of its digits powered with their respective position is equal to the original number. For e...
-
The International Mobile Station Equipment Identity(IMEI) is a number, usually unique, to identify mobile phones, as well as some satellit...
-
Harshad Number : In recreational mathematics, a Harshad number (or Niven number), is an integer (in base 10) that is divisible by the sum ...
-
package com.altafjava.sln.test; public class SecondLargestNumber { public static void main(String[] args) { int[] arr={5,20,8,1...
-
package com.fdcis.test; /** * How to find duplicate characters in a string in java? * */ public class Test { public static v...
Blog Archive
-
▼
2018
(38)
-
▼
August
(19)
- How to convert string into int primitive data type?
- Write a JAVA program for the given string and take...
- How to find largest number less than a given numbe...
- How to count occurrences of each character of a st...
- How to count occurrences of each character of a st...
- How to find second largest number in an integer ar...
- How to find sum of all digits of a number in java?
- How to find duplicate elements in an array using H...
- How to find duplicate elements in an array?
- Java Program to check two strings are Anagram or not
- How do you check the equality of two inner arrays ...
- How do you check the equality of two arrays in java?
- How to count duplicate characters in a string in j...
- How to find duplicate characters in a string in java?
- How do you remove all white spaces from a string i...
- How do you remove all white spaces from a string i...
- How to print Number Pyramid Pattern in java?
- How to create a pyramid of numbers in java?
- How to reverse a string in java?
-
►
March
(19)
- Write a Java Program to take an String array like ...
- Write a Java program to take an string array and f...
- Write a JAVA program for the given string and take...
- Write a program in java to find the longest palind...
- Write a program in java to print total palindrome ...
- Write a program in java to check whether an string...
- Write a program in java to check whether an string...
- Write a java program to print this numeric pattern...
- Write a java program to print this numeric pattern...
- Java Program to view System Properties
- How to make our classes as immutable class by usin...
- Write a java program in java to reverse each word ...
- WAP in java to reverse each word of string like ...
- Write a Java Program to reverse an String without ...
- Write a program in java to find the 1st non repeat...
- Write a Java progarm to input this and print this ...
- WAP to print char occurrence in given string I/P...
- Write a Java Program to print char occurrence in g...
- WAP in Java to count occurrence of each character ...
-
▼
August
(19)
About
Hello ALTAF
Friday, August 24, 2018
Thursday, August 23, 2018
Write a JAVA program for the given string and take another string and check whether the taken string is substring of given string or not.
/* Write a JAVA program for the given string and take another string and check whether the taken string is substring of given string or not.
Given String : google
Input String : gle
Output : gle is the substring of google
*/
Given String : google
Input String : gle
Output : gle is the substring of google
*/
import java.util.Scanner;
class CheckSubstring
{
public static void main(String[] args)
{
String s="google";
Scanner sc=new Scanner(System.in);
System.out.println("Enter an String : ");
String a=sc.next();
boolean flag=false;int len=0;
for(int i=1;i<=s.length()-a.length();i++)
{
for(int j=0;j<a.length();j++)
{
if(a.charAt(j)!=s.charAt(i+j) )
{
len=0;
break;
}
else
len++;
}
if(len==a.length())
{
flag=true;
break;
}
}
if(flag==true)
System.out.println(a+" is the substring of "+s);
else
System.out.println(a+" is not substring of "+s);
}
}
How to find largest number less than a given number and without a given digit?
For example, If given number 165 and given digit is 6 then you should find the largest number less than 165 but it should not contain 4 in it. In this case answer is 159.
package com.altafjava.test;
public class FindLargestNo {
public static void main(String[] args) {
int givenNum=165;
int givenDigit=6;
for(int i=givenNum;i>=Integer.MIN_VALUE;i--){
if((i+"").contains(givenDigit+"")==false){
System.out.println(i);
break;
}
}
}
}
Note :- double quotes(" ") is used to convert primitive data type into String
package com.altafjava.test;
public class FindLargestNo {
public static void main(String[] args) {
int givenNum=165;
int givenDigit=6;
for(int i=givenNum;i>=Integer.MIN_VALUE;i--){
if((i+"").contains(givenDigit+"")==false){
System.out.println(i);
break;
}
}
}
}
Note :- double quotes(" ") is used to convert primitive data type into String
How to count occurrences of each character of a string without using Regular Expression in java?
package com.altafjava.coos.test;
public class CountOccurrences2 {
public static void main(String[] args) {
String s="annotation";
System.out.println("Original String = "+s);
while(s.length()!=0){
int count=0;
for(int i=0;i<s.length();i++){
if(s.charAt(0)==s.charAt(i))
count++;
}
System.out.println(s.charAt(0)+" = "+count);
s=s.replaceAll(s.charAt(0)+"", "");
}
}
}
public class CountOccurrences2 {
public static void main(String[] args) {
String s="annotation";
System.out.println("Original String = "+s);
while(s.length()!=0){
int count=0;
for(int i=0;i<s.length();i++){
if(s.charAt(0)==s.charAt(i))
count++;
}
System.out.println(s.charAt(0)+" = "+count);
s=s.replaceAll(s.charAt(0)+"", "");
}
}
}
How to count occurrences of each character of a string in java?
package com.altafjava.coocis.test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CountCountOccurrences {
public static void main(String[] args) {
String s="annotation";
System.out.println("Original String = "+s);
while(s.length()!=0){
int count=0;
Pattern p=Pattern.compile(s.charAt(0)+"");
Matcher matcher=p.matcher(s);
while(matcher.find()){
count++;
}
System.out.println(s.charAt(0)+" = "+count);
s=s.replaceAll(s.charAt(0)+"","");
}
}
}
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CountCountOccurrences {
public static void main(String[] args) {
String s="annotation";
System.out.println("Original String = "+s);
while(s.length()!=0){
int count=0;
Pattern p=Pattern.compile(s.charAt(0)+"");
Matcher matcher=p.matcher(s);
while(matcher.find()){
count++;
}
System.out.println(s.charAt(0)+" = "+count);
s=s.replaceAll(s.charAt(0)+"","");
}
}
}
Wednesday, August 22, 2018
How to find second largest number in an integer array?
package com.altafjava.sln.test;
public class SecondLargestNumber {
public static void main(String[] args) {
int[] arr={5,20,8,1,9,2,14,34,11};
int largest=Integer.MIN_VALUE;
int secondLargest=Integer.MIN_VALUE;
for(int i=0;i<arr.length;i++){
if(arr[i]>largest){
secondLargest=largest;
largest=arr[i];
}
}
System.out.println("Original Array = 5,20,8,1,9,2,14,34,11");
System.out.println("Largest No = "+largest);
System.out.println("Second Largest No = "+secondLargest);
}
}
How to find sum of all digits of a number in java?
package com.altafjava.sad.test;
public class SumAllDigit {
public static void main(String[] args) {
int num=9526;
System.out.println("Original number = "+num);
int sum=0;
while(num!=0){
int remainder=num%10;
sum=sum+remainder;
num=num/10;
}
System.out.println("Sum = "+sum);
}
}
How to find sum of all digits of a number in java? |
Tuesday, August 21, 2018
How to find duplicate elements in an array using HashSet?
package com.altafjava.fdeia.test;
import java.util.HashSet;
public class Test2 {
public static void main(String[] args) {
int[] arr={1,6,5,1,2,2,9,6};
System.out.println("Input Array = 1,6,5,1,2,2,9,6");
System.out.print("The repeating elements are : ");
HashSet<Integer> hashSet=new HashSet<>();
for (int i = 0; i < arr.length; i++)
{
if(hashSet.add(arr[i])==false)
System.out.print(arr[i]+" ");
}
}
}
As we know HashSet does not allow duplicate elements. We can get benifit from it. If we add element into HashSet and if it is returning false. It that means it element is already added HashSet and this is duplicate element.
How to find duplicate elements in an array using HashSet? |
How to find duplicate elements in an array?
package com.altafjava.fdeia.test;
public class Test {
public static void main(String[] args) {
int[] arr={1,6,5,1,2,2,9,6};
System.out.println("Input Array = 1,6,5,1,2,2,9,6");
System.out.print("The repeating elements are : ");
for (int i = 0; i < arr.length; i++)
{
for(int j=i+1;j<arr.length;j++){
if(arr[i]==arr[j]){
System.out.print(arr[i]+" ");
break;
}
}
}
}
}
Note:-
We are using Brute Force Mechanism. This will take lot of time. Hence we can take the help of HashSet but in interview they check your logical skill.
We are using Brute Force Mechanism. This will take lot of time. Hence we can take the help of HashSet but in interview they check your logical skill.
How to find duplicate elements in an array? |
Monday, August 20, 2018
Java Program to check two strings are Anagram or not
An anagram of a string is another string that contains same characters, only the order of characters can be different. For example, “LISTEN” and “SILENT” are anagram of each other.
package com.caots.test;
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
//String s1="LISTEN", s2="SILENT";
String s3="TRIANGLE", s4="INTEGRAL";
boolean flag=checkAnagram(s3, s4);
if(flag==true)
System.out.println(s3+" & "+s4+" are Anagram strings");
else
System.out.println("No Anagram strings");
}
public static boolean checkAnagram(String s1,String s2){
char[] crr1=s1.toCharArray();
char[] crr2=s2.toCharArray();
Arrays.sort(crr1);
Arrays.sort(crr2);
if(Arrays.equals(crr1, crr2))
return true;
return false;
}
}
Java Program to check two strings are Anagram or not |
How do you check the equality of two inner arrays in java?
package com.ceo2a.test;
import java.util.Arrays;
public class Test2 {
public static void main(String[] args) {
int[] arr1={5,6,9};
int[] arr2={5,6,9};
Object[] objArr1={arr1};
Object[] objArr2={arr2};
System.out.println("objArr1 == objArr2 ? "+Arrays.equals(objArr1, objArr2));
System.out.println("objArr1 == objArr2 ? "+Arrays.deepEquals(objArr1, objArr2));
}
}
How do you check the equality of two inner arrays in java? |
How do you check the equality of two arrays in java?
package com.ceo2a.test;
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
int[] arr1={5,6,9};
int[] arr2={6,5,9};
int[] arr3={5,6,9};
System.out.println("arr1 == arr2 ? "+Arrays.equals(arr1, arr2));
System.out.println("arr1 == arr3 ? "+Arrays.equals(arr1, arr3));
}
}
How do you check the equality of two arrays in java? |
Sunday, August 19, 2018
How to count duplicate characters in a string in java?
package com.cdcis.test;
/**
* How to count duplicate characters in a string in java?
*
*/
public class Test {
public static void main(String[] args) {
String s="altafjava.blogspot.com";
System.out.println("Original String = "+s);
int count=0;
for(int i=0;i<s.length();i++){
char c=s.charAt(i);
for(int j=1;j<s.length();j++){
if(s.charAt(i)==s.charAt(j)){
count++;
}
}
System.out.println(s.charAt(i)+" = "+count);
count=0;
s=s.replaceAll(Character.toString(s.charAt(i)),"");
}
}
}
How to count duplicate characters in a string in java? |
How to find duplicate characters in a string in java?
package com.fdcis.test;
/**
* How to find duplicate characters in a string in java?
*
*/
public class Test {
public static void main(String[] args) {
String s="altafjava.blogspot.com";
System.out.println("Original String = "+s);
System.out.print("Duplicate characters = ");
while(s.length()!=0){
char c=s.charAt(0);
if(s.indexOf(c,1)!=-1){
System.out.print(s.charAt(0)+" ");
}
if(s.charAt(0)=='.')
s=s.replaceAll("\\.","");
else
s=s.replaceAll(s.charAt(0)+"","");
}
}
}
How to find duplicate characters in a string in java? |
How do you remove all white spaces from a string in java?
package com.rwsfs.test;
public class Test2 {
public static void main(String[] args) {
String s="Good Morning Mumbai";
StringBuilder sb=new StringBuilder();
for(int i=0;i<s.length();i++){
if(s.charAt(i)!=' '){
sb.append(s.charAt(i));
}
}
System.out.println("Original String = "+s);
System.out.println("\nNew String = "+sb);
}
}
How do you remove all white spaces from a string in java? |
How do you remove all white spaces from a string in java?
package com.rwsfs.test;
public class Test {
public static void main(String[] args) {
String s="My name is khan and i am not a terrorist";
String newString=s.replaceAll("\\s","");
System.out.println("Original String = "+s);
System.out.println("New String = "+newString);
}
}
How do you remove all white spaces from a string in java? |
Thursday, August 16, 2018
How to create a pyramid of numbers in java?
package com.npp.test;
public class Test {
public static void main(String[] args) {
int n=10;
for(int i=1;i<=n;i++)
for(int j=1;j<=n+1;j++)
System.out.print(i>9?j<=n-i+1?" ":i+" ":j<=n-i+1?" ":i+" ");
}
System.out.println();
}
Instead of writing ternery operator we can write this also
if(i>9)
System.out.print(j<=n-i+1?" ":i+" ");
else
System.out.print(j<=n-i+1?" ":i+" ");
How to reverse a string in java?
package com.rs.test;
public class Test {
public static void main(String[] args) {
String s="altafjava";
StringBuilder rs=new StringBuilder();
for(int i=s.length()-1;i>=0;i--){
rs.append(s.charAt(i));
}
System.out.println("Original String = "+s);
System.out.println("Reversed String = "+rs);
}
}
How to reverse a string in java? |
Friday, March 23, 2018
Write a Java Program to take an String array like this String[] s={"Welcome","to","2018","hi","17"} and filter only numbers.
Write a Java Program to take an String array like this String[] s={"Welcome","to","2018","hi","17"} and filter only numbers.
class FilterNumber
{
public static void main(String[] args)
{
String[] s={"Welcome","to","2018","hi","17"};
for(String n:s)
{
try
{
System.out.println(Integer.parseInt(n));
}
catch (NumberFormatException e)
{}
}
}
}
Write a Java Program to take an String array like this String[] s={"Welcome","to","2018","hi","17"} and filter only numbers. [All Java Interview programs] |
Write a Java program to take an string array and find the duplicate and sort in ascending order.
Write a Java program to take an string array and find the duplicate and sort in ascending order.
import java.util.*;
class RemoveDuplicateStringArray
{
public static void main(String[] args)
{
String[] s={"z","a","c","a","d","c","b"};
TreeSet<String> ts=new TreeSet<>(Arrays.asList(s));
System.out.println(ts);
}
}
Write a Java program to take an string array and find the duplicate and sort in ascending order. [All Java Interview Programs] |
Write a JAVA program for the given string and take another string and check whether the taken string is substring of given string or not.
Write a JAVA program for the given string and take another string and check whether the taken string is substring of given string or not.
Given String : google
Input String : gle
Output : gle is the substring of google
import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
String s="google";
Scanner sc=new Scanner(System.in);
System.out.println("Enter an String : ");
String a=sc.next();
boolean flag=false;int len=0;
for(int i=1;i<=s.length()-a.length();i++)
{
for(int j=0;j<a.length();j++)
{
if(a.charAt(j)!=s.charAt(i+j) )
{
len=0;
break;
}
else
len++;
}
if(len==a.length())
{
flag=true;
break;
}
}
if(flag==true)
System.out.println(a+" is the substring of "+s);
else
System.out.println(a+" is not substring of "+s);
}
}
Write a JAVA program for the given string and take another string and check whether the taken string is substring of given string or not. [All Java Interview Programs] |
Tuesday, March 20, 2018
Write a program in java to find the longest palindrome from the given String. If the length is same then print in descending order.
Write a program in java to find the longest palindrome from the given String.
If the length is same then print in descending order.
Input : madamLeveLrotornitin
Total Palindromes = madam ada LeveL eve rotor oto nitin iti
Longest Palindrome : rotor
class Palindrome4
{
public static void main(String[] args) {
// String s="madamLeveLnitin";
// String s="madamnitin";
String s="madamLeveLrotornitin";
String s2="";String s3="";
System.out.print("Total Palindromes = ");
for(int i=0;i<s.length();i++)
{
for(int j=i+2;j<=s.length();j++)
{
s2=s.substring(i,j);
StringBuffer sb=new StringBuffer(s2).reverse();
if(s2.equals(sb.toString()))
{
System.out.print(s2+" ");
if(s2.length()>s3.length())
s3=s2;
else if(s2.length()==s3.length())
{
if(s3.compareTo(s2)<0)
s3=s2;
}
}
}
}
System.out.println("\n\nLongest Palindrome = "+s3);
}
}
Write a program in java to find the longest palindrome from the given String. [All Java Interview Programs] |
Write a program in java to print total palindrome strings presented in given string. Input : abcacbbbca Output : [bb, acbbbca, bbb, cac, cbbbc, bcacb]
Write a program in java to print total palindrome strings presented in given string.
Input : abcacbbbca
Output : [bb, acbbbca, bbb, cac, cbbbc, bcacb]
import java.util.*;
class Palindrome3
{
public static void main(String[] args)
{
String s="abcacbbbca";
String s2="";
HashSet<String> hs=new HashSet<>();
for(int i=0;i<s.length();i++)
{
for(int j=i+2;j<=s.length();j++)
{
s2=s.substring(i,j);
StringBuffer sb=new StringBuffer(s2).reverse();
if(s2.equals(sb.toString()))
hs.add(s2);
}
}
System.out.print("Total Palindromes = "+hs);
}
}
Write a program in java to print total palindrome strings presented in given string. [All Java Interview Programs] |
Write a program in java to check whether an string is palindrome or not without using reverse method
Write a program in java to check whether an string is palindrome or not
without using reverse method.
public class Palindrome2
{
public static void main(String[] args)
{
String s="malayalam";
String s2="";
for(int i=s.length()-1;i>=0;i--)
s2=s2+s.charAt(i);
if(s.equals(s2))
System.out.println(s+" is palindrome");
else
System.out.println(s+" is not palindrome");
}
}
Write a program in java to check whether an string is palindrome or not without using reverse method [All Java Interview Programs] |
Write a program in java to check whether an string is palindrome or not
/*
Write a program in java to check whether an string is palindrome or not
*/
public class Palindrome
{
public static void main(String[] args)
{
String s="madam";
String s2=new StringBuffer(s).reverse().toString();
if(s.equals(s2))
System.out.println(s+" is palindrome");
else
System.out.println(s+" is not palindrome");
}
}
Write a program in java to check whether an string is palindrome or not
*/
public class Palindrome
{
public static void main(String[] args)
{
String s="madam";
String s2=new StringBuffer(s).reverse().toString();
if(s.equals(s2))
System.out.println(s+" is palindrome");
else
System.out.println(s+" is not palindrome");
}
}
Write a program in java to check whether an string is palindrome or not [All Java Interview Programs] |
Sunday, March 18, 2018
Write a java program to print this numeric pattern 1 2 3 4 8 7 6 5 9 10 11 12 16 15 14 13
Write a java program to print this numeric pattern
1 2 3 4
8 7 6 5
9 10 11 12
16 15 14 13
class NumericPattern2
{
public static void main(String[] alt){
int n=4;
for(int i=0;i<n;i++){
for(int j=1;j<=n;j++){
if(i%2==0)
System.out.print(i*n+j+" ");
else
System.out.print((i+1)*n-j+1+" ");
}
System.out.println();
}
}
Write a java program to print this numeric pattern 1 2 3 4 8 7 6 5 9 10 11 12 16 15 14 13
Write a java program to print this numeric pattern
1 2 3 4
8 7 6 5
9 10 11 12
16 15 14 13
public class NumericPattern
{
public static void main(String[] args)throws Exception {
int c=0;
for(int i=1;i<=4;i++)
{
if(i%2==1)
{
for(int j=1;j<=4;j++)
System.out.print(" "+ ++c);
c=c+4;
}
else
{
int t=c;
for(int k=1;k<=4;k++)
{
System.out.print(" "+t--);
}
}
System.out.println();
}
}
}
Write a java program to print this numeric pattern. [All Java Interview Programs] |
Java Program to view System Properties
// Java Program to view System Properties
class SystemProperties
{
public static void main(String[] alt)
{
System.out.println("\nOS Version = "+System.getProperty("os.version"));
System.out.println("\nOS Name = "+System.getProperty("os.name"));
System.out.println("\nOS Architecture = "+System.getProperty("os.arch"));
System.out.println("\nJava Compiler = "+System.getProperty("java.compiler"));
System.out.println("\nExtension Directory = "+System.getProperty("java.ext.dirs"));
System.out.println("\nPath Environment Variable = "+System.getProperty("java.library.path"));
System.out.println("\nFile Separator = "+System.getProperty("file.separator"));
System.out.println("\nPath Separator = "+System.getProperty("path.separator"));
System.out.println("\nCurrent working directory = "+System.getProperty("user.dir"));
System.out.println("\nCurrent working directory = "+System.getProperty("user.dir"));
System.out.println("\nAccount Name = "+System.getProperty("user.name"));
System.out.println("\nJVM implementation version = "+System.getProperty("java.vm.version"));
System.out.println("\nJVM implementation Name = "+System.getProperty("java.vm.name"));
System.out.println("\nJava installation directory = "+System.getProperty("java.home"));
System.out.println("\nJVM version = "+System.getProperty("java.runtime.version"));
System.out.println("\nJVM version = "+System.getProperty("deployment.javaws.jre.0.osname"));
System.out.println("\nJVM version = "+System.getProperty("deployment.javaws.jre.0.path"));
}
}
class SystemProperties
{
public static void main(String[] alt)
{
System.out.println("\nOS Version = "+System.getProperty("os.version"));
System.out.println("\nOS Name = "+System.getProperty("os.name"));
System.out.println("\nOS Architecture = "+System.getProperty("os.arch"));
System.out.println("\nJava Compiler = "+System.getProperty("java.compiler"));
System.out.println("\nExtension Directory = "+System.getProperty("java.ext.dirs"));
System.out.println("\nPath Environment Variable = "+System.getProperty("java.library.path"));
System.out.println("\nFile Separator = "+System.getProperty("file.separator"));
System.out.println("\nPath Separator = "+System.getProperty("path.separator"));
System.out.println("\nCurrent working directory = "+System.getProperty("user.dir"));
System.out.println("\nCurrent working directory = "+System.getProperty("user.dir"));
System.out.println("\nAccount Name = "+System.getProperty("user.name"));
System.out.println("\nJVM implementation version = "+System.getProperty("java.vm.version"));
System.out.println("\nJVM implementation Name = "+System.getProperty("java.vm.name"));
System.out.println("\nJava installation directory = "+System.getProperty("java.home"));
System.out.println("\nJVM version = "+System.getProperty("java.runtime.version"));
System.out.println("\nJVM version = "+System.getProperty("deployment.javaws.jre.0.osname"));
System.out.println("\nJVM version = "+System.getProperty("deployment.javaws.jre.0.path"));
}
}
How to make our classes as immutable class by using Java programming?
/*
How to make our classes as immutable clas by using Java programming?
*/
final class Employee
{
final int id;
final String name;
public Employee(int id,String name)
{
this.id=id;
this.name=name;
}
public int getId()
{
return id;
}
public String getName()
{
return name;
}
}
class ImmutableClass
{
public static void main(String[] alt)
{
Employee e1=new Employee(222,"Samar");
System.out.println(e1.getId()+" = "+e1.getName());
Employee e2=new Employee(333,"Athar");
System.out.println(e2.getId()+" = "+e2.getName());
}
}
How to make our classes as immutable clas by using Java programming?
*/
final class Employee
{
final int id;
final String name;
public Employee(int id,String name)
{
this.id=id;
this.name=name;
}
public int getId()
{
return id;
}
public String getName()
{
return name;
}
}
class ImmutableClass
{
public static void main(String[] alt)
{
Employee e1=new Employee(222,"Samar");
System.out.println(e1.getId()+" = "+e1.getName());
Employee e2=new Employee(333,"Athar");
System.out.println(e2.getId()+" = "+e2.getName());
}
}
Write a java program in java to reverse each word of string like this input:-"Altaf Java Blog" output:-"Blog Java Altaf"
/* Write a java program in java to reverse each word of string like this
input:-"Altaf Java Blog"
output:-"Blog Java Altaf"
*/
class ReverseString3
{
public static void main(String[] alt)
{
String s="Altaf Java Blog";
String[] s2=s.split(" ");
String s3=new String();
for(int i=s2.length-1;i>=0;i--)
{
s3=s3.concat(s2[i]+" ");
}
System.out.println(s3);
}
}
input:-"Altaf Java Blog"
output:-"Blog Java Altaf"
*/
class ReverseString3
{
public static void main(String[] alt)
{
String s="Altaf Java Blog";
String[] s2=s.split(" ");
String s3=new String();
for(int i=s2.length-1;i>=0;i--)
{
s3=s3.concat(s2[i]+" ");
}
System.out.println(s3);
}
}
WAP in java to reverse each word of string like input : "Sriman java group" output:- "namirS avaJ puorG"
/* WAP in java to reverse each word of string like
input : "Sriman java group"
output:- "namirS avaJ puorG"
Note: Here instead of String we are using StringBuffer because we cannot modify the string object beacause of String
immutability. If we use String concat() method then every time new object will be created which is memory causes memory
wastage.
*/
class ReverseString2
{
public static void main(String[] alt)
{
String s="Sriman Java Group";
String[] s2=s.split(" ");
StringBuffer sb=new StringBuffer("");
for(String a:s2)
{
for(int i=a.length()-1;i>=0;i--)
sb.append(a.charAt(i));
System.out.print(sb+" ");
sb=new StringBuffer("");
}
}
}
input : "Sriman java group"
output:- "namirS avaJ puorG"
Note: Here instead of String we are using StringBuffer because we cannot modify the string object beacause of String
immutability. If we use String concat() method then every time new object will be created which is memory causes memory
wastage.
*/
class ReverseString2
{
public static void main(String[] alt)
{
String s="Sriman Java Group";
String[] s2=s.split(" ");
StringBuffer sb=new StringBuffer("");
for(String a:s2)
{
for(int i=a.length()-1;i>=0;i--)
sb.append(a.charAt(i));
System.out.print(sb+" ");
sb=new StringBuffer("");
}
}
}
Write a Java Program to reverse an String without reverse() method
// Write a Java Program to reverse a string without reverse() method
import java.util.*;
class ReverseString
{
public static void main(String[] args)
{
String s="Vishnu";
StringBuffer sb=new StringBuffer("");
for(int i=s.length()-1;i>=0;i--)
sb.append(s.charAt(i));
System.out.println(sb);
}
}
Write a Java Program to reverse an String without reverse() method |
Write a program in java to find the 1st non repeated character from the string.
/*
WAP to find the 1st non repeated character from the string.
In this string ("altaf alam") 1st Non repeat character is 't'
*/
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class NonRepeatChar {
public static void main(String[] args) {
String s="altaf alam";
for(int i=0;i<s.length();i++)
{
Pattern p=Pattern.compile(String.valueOf(s.charAt(i)));
Matcher m=p.matcher(s);
m.find();
if(!(m.find()))
{
System.out.println("First Non repeatable Character = "+s.charAt(i));
break;
}
}
}
}
Write a Java progarm to input this and print this Input =a2c3z5 Output = aaccczzzzz
/*
Write a Java progarm to input this and print this
Input =a2c3z5
Output = aaccczzzzz
*/
import java.util.*;
class Test
{
public static void main(String[] args)
{
String s="a2c3z5";
String[] s2=s.split("(?<=\\G..)");
for(String s3:s2)
{
char c=s3.charAt(0);
int n=s3.charAt(1)-48;
for(int i=0;i<n;i++)
{
System.out.print(c);
}
}
}
}
Write a Java progarm to input this and print this
Input =a2c3z5
Output = aaccczzzzz
*/
import java.util.*;
class Test
{
public static void main(String[] args)
{
String s="a2c3z5";
String[] s2=s.split("(?<=\\G..)");
for(String s3:s2)
{
char c=s3.charAt(0);
int n=s3.charAt(1)-48;
for(int i=0;i<n;i++)
{
System.out.print(c);
}
}
}
}
WAP to print char occurrence in given string I/P : s=anand s2=prabhakar O/P : 3a 0n a n 0d
/*
WAP to print char occurrence in given string
I/P : s=anand
s2=prabhakar
O/P : 3a 0n a n 0d
*/
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
String s="anand";
String s2="prabhakar";
String s3="";
for(int i=0;i<s.length();i++)
{
String ch=String.valueOf(s.charAt(i));
Pattern p=Pattern.compile(ch);
Matcher m=p.matcher(s2);
int count=0;;
while(m.find())
{
count++;
}
if(s3.contains(ch))
System.out.print(ch+" ");
else
System.out.print(count+ch+" ");
s3=s3+ch;
}
}
}
WAP to print char occurrence in given string
I/P : s=anand
s2=prabhakar
O/P : 3a 0n a n 0d
*/
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
String s="anand";
String s2="prabhakar";
String s3="";
for(int i=0;i<s.length();i++)
{
String ch=String.valueOf(s.charAt(i));
Pattern p=Pattern.compile(ch);
Matcher m=p.matcher(s2);
int count=0;;
while(m.find())
{
count++;
}
if(s3.contains(ch))
System.out.print(ch+" ");
else
System.out.print(count+ch+" ");
s3=s3+ch;
}
}
}
Write a Java Program to print char occurrence in given string I/P : aabbccadc O/P : 3a 2b 3c 1d
/*
Write a Java Program to print char occurrence in given string
I/P : aabbccadc
O/P : 3a 2b 3c 1d
*/
import java.util.regex.*;
public class Test
{
public static void main(String[] args)
{
String s="aabbccadc";
String s4="";
for(int i=0;i<s.length();i++)
{
String ch=String.valueOf(s.charAt(i));
if(!(s4.contains(ch)))
{
Pattern p=Pattern.compile(ch);
Matcher m=p.matcher(s);
int count=0;String s3=null;
while(m.find())
{
count++;
s3=m.group();
s4=s4.concat(s3);
}
System.out.print(count+s3+" ");
}
}
}
}
Write a Java Program to print char occurrence in given string
I/P : aabbccadc
O/P : 3a 2b 3c 1d
*/
import java.util.regex.*;
public class Test
{
public static void main(String[] args)
{
String s="aabbccadc";
String s4="";
for(int i=0;i<s.length();i++)
{
String ch=String.valueOf(s.charAt(i));
if(!(s4.contains(ch)))
{
Pattern p=Pattern.compile(ch);
Matcher m=p.matcher(s);
int count=0;String s3=null;
while(m.find())
{
count++;
s3=m.group();
s4=s4.concat(s3);
}
System.out.print(count+s3+" ");
}
}
}
}
Wednesday, March 14, 2018
WAP in Java to count occurrence of each character of given string.
/*
Count occurrence of each character of given string
Input:-"Sriman java group"
Output:- S-1, r-2, i-1, m-1, a-3, n-1, J-1, v-1, G-1, o-1, u-1, p-1
*/
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CharOccurance
{
public static void main(String[] args)
{
String s = "Sriman Java Group";
String s2=null;
String s3=new String();
Pattern p=null; Matcher m=null;
for(int i=0;i<s.length();i++)
{
String ch=String.valueOf(s.charAt(i));
int count=0;
if(!(s3.contains(ch)) && !(ch.equals(" ")))
{
p=Pattern.compile(ch);
m=p.matcher(s);
while(m.find())
{
count++;
s2=m.group();
s3=s3.concat(s2);
}
System.out.println(s2+" - "+count);
}
}
}
}
Count occurrence of each character of given string
Input:-"Sriman java group"
Output:- S-1, r-2, i-1, m-1, a-3, n-1, J-1, v-1, G-1, o-1, u-1, p-1
*/
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CharOccurance
{
public static void main(String[] args)
{
String s = "Sriman Java Group";
String s2=null;
String s3=new String();
Pattern p=null; Matcher m=null;
for(int i=0;i<s.length();i++)
{
String ch=String.valueOf(s.charAt(i));
int count=0;
if(!(s3.contains(ch)) && !(ch.equals(" ")))
{
p=Pattern.compile(ch);
m=p.matcher(s);
while(m.find())
{
count++;
s2=m.group();
s3=s3.concat(s2);
}
System.out.println(s2+" - "+count);
}
}
}
}
Subscribe to:
Posts (Atom)
Program List
- armstrong
- char-occurrances
- check-anagram
- check-duck-no
- check-duplicate-no
- check-endianess-of-os
- check-equality-of-two-arrays
- check-fibonacci-series
- check-harshad-no
- check-kaprekar
- check-keith-no
- check-niven-no
- check-palindrome
- check-prime
- check-prime-no
- check-substring
- check-unique-no
- check-valid-imei
- convert-array-to-list
- convert-string-to-int
- count-duplicate-char-in-string
- count-occurrences-char-of-string
- create-immutable-class
- disarium
- filter-number
- find-1st-non-repeat-char
- find-common-no-in-array
- find-duplicate-char-in-string
- find-duplicate-no-in-array
- find-largest-no-less-than-given-no
- find-palindrome
- find-second-largest-number
- image-store-in-database
- jdbc-program
- number-pyramid-pattern
- numeric-pattern
- remove-duplicate-in-string-array
- remove-white-space-of-string
- reverse-list
- reverse-string
- search-element-in-array
- search-list
- sorting-array
- sorting-list
- sorting-user-defined-object
- string-reverse
- sum-of-all-digits
- swap-2-no
- view-system-properties