Java Program to find Max occurred character in a string


  • How to print duplicate characters from string in java?
  • Java program to count number of repeated characters in a string.
  • Java program for printing a repetitive letters in a string
  • count occurrences of each character in string java


  1. package com.javatutorial;
  2.  
  3. public class MaxOccuranceOfChar{
  4.    
  5. public static String MaxOccuredChar(String str) {
  6.  
  7.         char[] array = str.toCharArray();
  8.         int maxCount = 1;
  9.         char maxChar = array[0];
  10.  
  11.   for(int i = 0, j = 0; i < str.length() - 1; i = j){
  12.        int count = 1;
  13.    while (++j < str.length() && array[i] == array[j]) {
  14.           count++;
  15.         }
  16.  
  17.   if (count > maxCount) {
  18.      maxCount = count;
  19.      maxChar = array[i];
  20.  }
  21.  
  22.   }
  23.  
  24.         return (maxChar + " = " + maxCount);
  25.  }

  26.  public static void main(String[] args) {
  27.   
  28.   String str1=MaxOccuredChar("instanceofjava");
  29.   System.out.println(str1);
  30.  
  31.   String str2=MaxOccuredChar("aaaabbbccc");
  32.   System.out.println(str2);
  33.  
  34.   String str3=MaxOccuredChar("ssssiiinnn");
  35.   System.out.println(str3);
  36.  
  37.   String str4=MaxOccuredChar("jaaaavvvvvvvvaaaaaaaaaa");
  38.   System.out.println(str4);
  39.  
  40. }

  41. }


Output:

  1. i = 1
  2. a = 4
  3. s = 4
  4. a = 10
     

Java program to check two strings are anagrams



  1. package com.javatutorial;
  2.  import java.util.Arrays;

  3. public class CheckAnagramStrings {
  4.   
  5. private static boolean isAnagram(String str1, String str2) {
  6.  
  7.         if (str1.length() != str2.length()) {
  8.             return false;
  9.         }
  10.         str1 = sortCharacters(str1);
  11.         str2 = sortCharacters(str2);
  12.         return str1.equals(str2);
  13.  }
  14.  
  15. private static String sortCharacters(String str) {
  16.         char[] charArray = str.toLowerCase().toCharArray();
  17.         Arrays.sort(charArray);
  18.         return String.valueOf(charArray);
  19.  }

  20.  public static void main(String[] args) {
  21.    
  22.  String str1 = "bemru";
  23.  String str2 = "mureb";
  24.  
  25.  if (isAnagram(str1, str2)) {
  26.           System.out.println(str2 + " is anagram of " + str1);
  27.   } else {
  28.           System.out.println("Strings are not anagrams.");
  29.   }
  30.  
  31. }

  32. }


Output:

  1. mureb is anagram of bemru
     

Java Program to count number of vowels in a string


Solution #1:

  1. package com.javatutorial;
  2.  
  3. public class CountNumberofVowels {
  4.  
  5.  public static void main(String[] args) {
  6.   
  7. System.out.println("Please enter a String");
  8. Scanner in = new Scanner(System.in);                  
  9.  String input = in.nextLine();
  10.  
  11.      char[] Chararray = input.toCharArray();
  12.  
  13.  int count = 0;
  14.  
  15.  for (char ch : Chararray) {
  16.       switch (ch) {
  17.              case 'a':
  18.              case 'e':
  19.              case 'i':
  20.              case 'o':
  21.              case 'u':
  22.              case 'A':
  23.              case 'E':
  24.              case 'I':
  25.              case 'O':
  26.              case 'U':
  27.                  count++;
  28.      break;
  29.      default:
  30.             
  31.      }
  32.  }
  33.        System.out.println("Number of vowels in String "+count);
  34.  
  35. }

  36. }


Output:

  1. Please enter a String
  2. Instance Of Java
  3. Number of vowels in String 6
     
Solution #2:

  1. package com.javatutorial;
  2.  
  3. public class CountNumberofVowels {
  4.  
  5.  public static void main(String[] args) {
  6.  
  7.  int vowels = 0, digits = 0, blanks = 0; char ch; 

  8. System.out.println("Please enter a String");
  9. Scanner in = new Scanner(System.in);                  
  10. String testString= in.nextLine();
  11.  
  12.  
  13. for(int i = 0; i < testString.length(); i ++)
  14. {
  15.  
  16.   ch = testString.charAt(i);
  17.  
  18.   if(ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' ||   
  19.       ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U')
  20.           vowels ++;
  21.        else if(Character.isDigit(ch))
  22.           digits ++;
  23.      else if(Character.isWhitespace(ch))
  24.            blanks ++;
  25.   }
  26.  
  27.     System.out.println("Vowels : " + vowels);
  28.     System.out.println("Digits : " + digits);
  29.     System.out.println("Blanks : " + blanks);           
  30.    
  31.  
  32. }

  33. }
Output:

  1. Please enter a String
  2. vowels and consonants
  3. Vowels : 6
  4. Digits : 0
  5. Blanks : 2

Java Program to Count the number of occurrences of a char in a String?



Solution #1:

  1. package com.javatutorial;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class CountNumberofChars {
  6.  
  7.  public static void main(String[] args) {
  8.   
  9.  String str = "";
  10.  
  11.  Scanner in= new Scanner(System.in);
  12.  System.out.println("Please enter a String");
  13.  
  14.  str=in.nextLine();
  15.  
  16.  System.out.println("Please enter a Character");
  17.  String chr=in.next();
  18.  
  19.  int charCount = str.length() - str.replaceAll("a", "").length();
  20.  
  21.  System.out.println("Number of occurances of given character:"+charCount);
  22.  
  23. }

  24. }



Output:

  1. Please enter a String
  2. Java
  3. Please enter a Character
  4. a
  5. Number of occurances of given character:2


Solution #2:


  1. package com.javatutorial;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class CountNumberofChars {
  6.   
  7. public static int countOccurrences(String find, String string)
  8. {
  9. int count = 0;
  10. int indexOf = 0;
  11.  
  12. while (indexOf > -1)
  13. {
  14.     indexOf = string.indexOf(find, indexOf + 1);
  15.     if (indexOf > -1)
  16.             count++;
  17.       }
  18. return count;
  19.  }

  20.  public static void main(String[] args) {
  21.   
  22.  int charCount=countOccurrences("a", "Instance of Java");
  23.  
  24.  System.out.println("Number of occurrences of given character:"+charCount);
  25.  
  26. }

  27. }

Output:

  1. Number of occurrences of given character: 3

Java program To Count the number of words in a String




  1. package com.javatutorial;
  2.  
  3. public class CountNumberofWords {
  4.  
  5.  public static void main(String[] args) {
  6.  
  7. String s="";
  8. int count=0;
  9.  
  10. Scanner in = new Scanner(System.in);
  11. System.out.println("Please enter a String");
  12.  s=in.nextLine();
  13.  
  14. char ch[]= new char[s.length()];    
  15.  
  16. for(int i=0;i<s.length();i++)
  17. {
  18.  
  19.     ch[i]= s.charAt(i);
  20.  
  21.     if( ((i>0)&&(ch[i]!=' ')&&(ch[i-1]==' ')) || ((ch[0]!=' ')&&(i==0)) )
  22.         count++;
  23.  
  24. }
  25. System.out.println("Number of words in given String: "+count);
  26.  
  27. }

  28. }


Output:

  1. Please enter a String
  2. Java Tutorial
  3. Number of words in given String: 2

Pattern Program in java Part-3


 Program #1:  java program to print pyramid of stars using for loop in below format


*
**
***
****
*****
******
*******
********
*********
**********




  1. package com.learnJavaOnline;
  2. public class PrintStarsFormat {
  3.  
  4. public static void main(String[] args) {
  5.   
  6. int  numberOfStars=10;
  7.  
  8. for (int row = 1; row <= 10; row++) {
  9.  
  10.     for(numberOfStars = 1; numberOfStars <= row; numberOfStars++) {
  11.       System.out.print("*");
  12.     }
  13.  
  14.     System.out.println(); 
  15.  }
  16.  
  17. }
  18.  
  19. }
Output:
  1. *
  2. **
  3. ***
  4. ****
  5. *****
  6. ******
  7. *******
  8. ********
  9. *********
  10. **********

            

Program #2: java program to print pyramid of stars using for loop in below format



**********
*********
********
*******
******
*****
****
***
**
*



  1. package com.learnJavaOnline;
  2. public class PrintStarsPyramid {
  3.  
  4. public static void main(String[] args) {
  5.   
  6. int  numberOfStars=10;
  7.  
  8. for(int i=numberOfStars; i>0 ;i--){
  9.  
  10.     for(int j=0; j < i; j++){
  11.  
  12.           System.out.print("*");
  13.     }
  14.  
  15.     System.out.println("");
  16. }
  17.  
  18. }
  19.  
  20. }
Output:
  1. **********
  2. *********
  3. ********
  4. *******
  5. ******
  6. *****
  7. ****
  8. ***
  9. **
  10. *



Program #3 java program to print pyramid of stars using for loop in below format


*
**
***
****
*****
******
*******
********
*********
**********
**********
*********
********
*******
******
*****
****
***
**
*




  1. package com.learnJavaOnline;
  2. public class PrintStarsPyramid {
  3.  
  4. public static void main(String[] args) {
  5.   
  6. int  numberOfStars=10;
  7.  
  8.  for(int i=1; i<= numberOfStars ;i++){
  9.  
  10. for(int j=0; j < i; j++){
  11.  
  12.   System.out.print("*");
  13.  
  14. }
  15.  
  16. System.out.println("");
  17.  
  18. }
  19.  
  20. for(int i=numberOfStars; i>0 ;i--){
  21.  
  22. for(int j=0; j < i; j++){
  23.   System.out.print("*");
  24. }
  25.  
  26. System.out.println("")}
  27.  
  28. }
  29.  
  30. }
Output:
  1. *
  2. **
  3. ***
  4. ****
  5. *****
  6. ******
  7. *******
  8. ********
  9. *********
  10. **********
  11. **********
  12. *********
  13. ********
  14. *******
  15. ******
  16. *****
  17. ****
  18. ***
  19. **
  20. *




Print Pascals triangle using java program


Select Menu