Posts

Showing posts with the label Java Programs

Java program to find palindromic substring in a string.

public class PalidrumSubString {  public static void main(String args[])  {   String s = "my name rar kumar poop";   String p[] = s.split(" ");   String s2 = "";   for (int j = 0; j < p.length; j++)   {    for (int i = p[j].length() - 1; i >= 0; i--)    {     char z = p[j].charAt(i);     s2 = s2 + z;    }    if (p[j].equals(s2))    {     System.out.println(s2);    }    else    {     s2 = "";    }   }  } }

Java program to count the occurrence of any character in given String.

for  ( int  i = 0; i < len; i++) {    x = Character.toLowerCase(s.charAt(i));     if  (x == a)     c = c + 1;   }   System.out.println( "Frequency of the letter   " + a +  " in the sentence "  + s +  " is "  + c);

Reverse a String

public static  String reverseString(String s)  {    int  l = s.length();   String backward = "";    for  ( int  i = l - 1; i >= 0; i--)   {    backward = backward + s.charAt(i);   }    return  backward;  }

remove spaces from sentance

//1. Using replaceAll() Method strWithoutSpace = str.replaceAll(" ", ""); System.out.println(strWithoutSpace); //2. Without Using replaceAll() Method char[] strArray = str.toCharArray(); StringBuffer sb = new StringBuffer(); for (i = 0; i < strArray.length; i++) { if( (strArray[i] != ' ') && (strArray[i] != '\t') ) { sb.append(strArray[i]); } }

Count a Word

public static int countWords(String str) { int count = 1; for(int i=0; i<=str.length()-1; i++) { if(str.charAt(i) == ' ' && str.charAt(i+1)!=' ') { count++; } } return count; }

Delete Words from Sentence

strOrig = strOrig.replaceAll(word, "");

Armstrong

while(nu != 0) { rem = nu%10; num = num + rem*rem*rem; nu = nu/10; } 153 = 1 pow3 + 5 pow 3 +m pow3 int  num = Integer.parseInt(args[0]);    int  n = num;                                    //use to check at last time    int  check = 0, remainder;    while  (num > 0) {    remainder = num % 10;    check = check + (int) Math.pow(remainder, 3);    num = num / 10;   }    if  (check == n)    System.out.println(n +  " is an Armstrong Number" );    else    System.out.println(n +  " is not a Armstrong Number" );  }

palindrome

while(num != 0) { rem = num%10; rev = rev*10 + rem; num = num/10; }

factorial

for(i=num; i>0; i--) { fact = fact*i; }

Alphabet Check

if((ch>='a' && ch<='z') || (ch>='A' && ch<='Z')) { System.out.print(ch + " is an Alphabet"); } else { System.out.print(ch + " is not an Alphabet"); }

java program to remove vowels

import java.util.Scanner; public class JavaProgram { public static void main(String args[]) { String strOrig, strNew; Scanner scan = new Scanner(System.in); System.out.print("Enter a String : "); strOrig = scan.nextLine(); System.out.print("Removing Vowels from The String [" +strOrig+ "]\n"); strNew = strOrig.replaceAll("[aeiouAEIOU]", ""); System.out.print("All Vowels Removed Successfully..!!\nNow the String is :\n"); System.out.print(strNew); } } import java.util.Scanner; public class JavaProgram { public static void main(String args[]) { String str, r; Scanner scan = new Scanner(System.in); System.out.print("Enter a String : "); str = scan.nextLine(); System.out.print("Removing Vowels from String [" +str+ "]\n"); r = removeVowels(str); ...

String Utility - Replace , Replace All,Replace First

replace(char oldChar, char newChar) replace(CharSequence target, CharSequence replacement) replaceAll(String regex, String replacement) replaceFirst(String regex, String replacement) String myString = "__x___x___x_x____xx_" ; char oldChar = 'x' ; char newChar = 'o' ; String newString = myString . replace ( oldChar , newChar ); // __o___o___o_o____oo_ String myString = "one fish, two fish, three fish" ; String target = "fish" ; String replacement = "sheep" ; String newString = myString . replace ( target , replacement ); // one sheep, two sheep, three sheep String str = new String ( "==qwerty==" ). replaceAll ( "^==" , "?" ); \\str : "?qwerty==" String str = new String ( "==qwerty==" ). replaceAll ( "==$" , "?" ); \\str : "==qwerty?" String str = new String ( "===qwerty==" ). replaceAll ( "(=)+" , ...

Java program to find the frequency of one string in another string.

import  java.util.Scanner; public class   WordFrequencyCounter  {      public static void   main (String args[]) {         Scanner scan =  new  Scanner(System.in);         System.out.println( "enter main string" );         String s = scan.nextLine();         System.out.println( "enter string to be searched" );         String f = scan.nextLine();         s = s + " ";          int  l = s.length();          char  a;          int  c = 0;         String s1 = "";          for  (int i = 0; i < l; i++) {             a = s.charAt(i);              if  (a != ' ') {                 s1 = s...

Java program to print fibonacci series.

A fibonacci series is one in which the next term is the sum of previous two terms Example. 0 1 1 2 3 5 8…… import  java.util.Scanner; public class   Fibonacci  {   public static void   main (String args[]) {   func(10);  }  static void  func(int n) {   int a = 0, b = 1, c = 0, i;   System.out.print(a + " , " + b);    for  (i = 3; i <= n; i++) {    c = a + b;    System.out.print(", " + c);    a = b;    b = c;   }  } }

Java program to print Tribonacci Series.

A tribonacci series is one in which the sum next term is the sum of previous three terms Example 0 1 2 3 6 11 20…………. import  java.util.Scanner; public class   Tribonacci   {   public static void   main (String args[]) {   func(10);  }   static void  func(int n) {    int  a = 0, b = 1, c = 2, d = 0, i;   System.out.print(a + " , " + b + " , " + c);    for  (i = 4; i <= n; i++) {    d = a + b + c;    System.out.print(", " + d);    a = b;    b = c;    c = d;   }  } } Output: 0 , 1 , 2, 3, 6, 11, 20, 37, 68, 125 BUILD SUCCESSFUL (total time: 1 second)

Java program to reverse a number.

package  demo; import  java.util.Scanner; public class   ReverseNumber {   public static void   main (String[] args)   {   Scanner sc = new Scanner(System.in);    int  num, r, reverse = 0;   System.out.print( "Enter any number: " );   num = sc.nextInt();    while  (num != 0)    {    r = num % 10;    reverse = reverse * 10 + r;    num = num / 10;   }   System.out.println( "Reversed of number: "  + reverse);  } } Output : Enter any number: 123789 Reversed of number: 987321 BUILD SUCCESSFUL (total time: 4 seconds)

Java program to sum digits of a number.

package  demo; import  java.util.Scanner; public class   SumOfDigits {   public static void  main(String[] args)   {   Scanner sc = new Scanner(System.in);    int  num, sum = 0, r;   System.out.println( "Enter a number: " );   num = sc.nextInt();    while  (num != 0) {    r = num % 10;    num = num / 10;    sum = sum + r;   }   System.out.print( "Sum of digits of number:  "  + sum);  } } Output: Enter a number: 32165 Sum of digits of number:  17 BUILD SUCCESSFUL (total time: 8 seconds)

Java program to swap two array.

package  demo; import  java.util.Scanner; public class   SwappingArray {   public static void   main (String[] args)   {    int [] a =  new int [10], b =  new int [10], c =  new int [10];   Scanner sc = new Scanner(System.in);   System.out.println( "Enter First array: " );    for  (int i = 0; i < 10; i++) {    a[i] = sc.nextInt();   }   System.out.println( "Enter Second array: " );    for  (int i = 0; i < 10; i++) {    b[i] = sc.nextInt();   }   System.out.println( "Arrays before swapping" );   System.out.println( "First array: " );    for  (int i = 0; i < 10; i++) {    System.out.print(a[i]);   }   System.out.println(" Second array:  ");    for  (int i = 0; i < 10; i++) {    System.out.print(b[i]);   }    for  (int i = 0; i < 10; i++) {   ...