count the repeated character in one string

to find the how character occurrence in one String (for example the string is -java means there is 2 'a 'was repeated )

View Answers

December 22, 2010 at 11:17 AM

Hi Friend,

Try the following code:

import java.util.*;

class  CountCharacters {
  public static void main(String[] args) throws Exception{
  Scanner input=new Scanner(System.in);
  System.out.print("Please enter string ");
  System.out.println();
  String str=input.nextLine();
  String st=str.replaceAll(" ", "");

  char[]third =st.toCharArray();
  for(int counter =0;counter<third.length;counter++){
  char ch= third[counter];
  int count=0;
  for ( int i=0; i<third.length; i++){
  if (ch==third[i])
  count++;
}
boolean flag=false;
for(int j=counter-1;j>=0;j--){
if(ch==third[j])
flag=true;
}
if(!flag){
System.out.println("Character :"+ch+" occurs "+count+" times ");
}
}
}
}

Thanks


March 24, 2013 at 11:30 PM

//program to find number of repeating characters in a string

//Developed by Subash

import java.util.Scanner;

public class NoOfRepeatedChar

{

public static void main(String []args)

{

//input through key board

Scanner sc = new Scanner(System.in);

System.out.println("Enter a string :");

String s1= sc.nextLine();

        //formatting String to char array

        String s2=s1.replace(" ","");
        char [] ch=s2.toCharArray();

        int counter=0;

        //for-loop tocompare first character with the whole character array

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

            for(int j=0;j<ch.length;j++)
            {
                 if(ch[i]==ch[j])
                    count++; //if character is matching with others
            }
            if(count>1)
            {
                boolean flag=false;

                //for-loop to check whether the character is already refferenced or not 
                for (int k=i-1;k>=0 ;k-- )
                {
                    if(ch[i] == ch[k] ) //if the character is already refferenced
                        flag=true;
                }
                if( !flag ) //if(flag==false) 
                    counter=counter+1;
            }
        }
    if(counter > 0) //if there is/are any repeating characters
System.out.println("Number of repeating charcters in the given string is/are " +counter);

else

System.out.println("Sorry there is/are no repeating charcters in the given string");

}

}









Related Tutorials/Questions & Answers:
count the repeated character in one string
count the repeated character in one string  to find the how character occurrence in one String (for example the string is -java means there is 2...; } if(!flag){ System.out.println("Character :"+ch+" occurs "+count+" times
J2ME count character into string
J2ME count character into string  i am new in J2ME, my problem is how to count character into number, i had been research google for almost 2 days and got nothing...some one plz give me a punch :) as an example, 1. i got a text
Advertisements
Count the character in java
Count the character in java  Write a java program to count... CountCharacters { public static void count(final String str){ Runnable...); System.out.print("Please enter string "); String str=input.nextLine(); count(str
Character count by while loop
Character count by while loop  Write the program to count the number...]) flag=true; } if(!flag){ System.out.println("Character :"+ch+" occurs "+count... CountCharacters { public static void main(String[] args) throws Exception
Find Successively Repeated Character using Java
Find Successively Repeated Character using Java In this section, we are going to find the successively repeated character using Regular Expression... Repeated Character is m
Get Character from String
Get Character from String     ... of character and retrieve elements from that character array one by one... toCharArray() to convert string to character array. 3. Retrieve character from
Count Characters from the input string Java
Count Characters from the input string of Java In this section, you will learn how to count the occurrence of each character in the string. For this, we have... to count the occurrence of each character of the array.  Here is the code
How to Remove Repeated Characters from the String?
How to Remove Repeated Characters from the String?  Hi, I trying to develop an application which will remove the repeated characters from... the example of how to remove repeated characters from the string. Please visit
Remove Repeated Characters from the String
Remove Repeated Characters from the String In this we are going to remove repeated characters from the string. For this, we have allowed the user to enter the string. The class BufferedReader reads the string from the console. Then we
How to display string or character
How to display string or character  how to print character or a word without using array concept
Count words in a string method?
Count words in a string method?  How do you Count words in a string...=br.readLine(); int count=0; String arr[]=st.split... count the length of arr which return the number of words in the inputted string
Removing character from a string in sql.
Removing character from a string in sql.  How to remove or replace character from a string
Replace Character in String
Replace Character in String        This example replaces a character with a specified character in a given string. To replace a character with the given character in sting first
Removing character from a string in sql.
Removing character from a string in sql.  Removing special characters from a string in sql
Find number of words begin with the specified character
("Enter character to count:"); String s1 = input.next(); StringTokenizer stk...Count number of words begin with the specified character In this section, you... character from the string. To do this, we have allowed the user to input a sentence
Java Remove a character from string
Java Remove a character from string In this tutorial, you will learn how to remove a character from the string. There are several methods for examining... to remove a particular character from the string. In the given example, we have
Convert string into character array
Description: This tutorial demonstrate how to convert a string into array of char. The toCharArray() method converts string to a new character array...;{   public static void main(String[] args) {  
Find Character form String example
in Action Script3:- String maintain a index position for every character in the string. The first character index position is 0.  String provide charAt() method to find out the character in the string with the help of index position
find all unique character in string using java
find all unique character in string using java  example: hello how are you. output should be waryu
how to count words in string using java
how to count words in string using java  how to count words in string...: "); String st=input.nextLine(); int count=0...()){ String token=stk.nextToken(); count
Determining the Character Boundaries in a Unicode String
Determining the Character Boundaries in a Unicode String In this section,  you will learn how to determine the character boundaries in a string. By determining the character boundaries, you will be able to break the string
Java count frequency of words in the string
Java count frequency of words in the string. In this tutorial, you will learn how to count the occurrence of each word in the given string. String...))) i++; String word = str.substring(count + 1, i); if (map.containsKey(word
PHP get character from String
PHP Get Character From String To get sub string from a String we can use substr() method. Structure of this method is :ADS_TO_REPLACE_1 string substr (string $var, int $initial [, int $len]  ) In the above example we want
Count numbers of spaces and number of words from the input string
Count numbers of spaces and number of words from the input string Java In this section, you will learn how to count the number of spaces and words from...;} } System.out.println("Number of spaces :"+count); String[] words 
Java count vowels
will read the string once you enter the string. Then we have taken a count... to length of string input and inside loop checking each character...Java count vowels In this section you will learn how to count the number
how to count unique and duplicate values from one table in mysql?
how to count unique and duplicate values from one table in mysql?  I have use EMP table.I want to count unique and duplicate records from emp table and how to use in java program
how to split string after first occurrence of alphanumeric character?
how to split string after first occurrence of alphanumeric character?  String strArray="135(i),15a,14(g)(q)12,67dd(),kk,159";//splited by ',' divide string after first occurrence of alphanumeric value/character expected output
Write a method which will remove any given character from a string?
Write a method which will remove any given character from a string? In this example we will describe remove any given character from a string... argument. It checks the character you want to remove in the string in a sequence
Convert Character into a String
Convert Character into a String   ... the functionality to convert a character into a string.  Code Description:ADS... it into a string format using by the toString() method. This method converts the character
string
and a character. It then count the occurrence of that character in the string and display...string  How to count a particular character in a given sentence and also displaying that contain word? Like I want to find "a" in the string
String copyValueOf(char[] data, int offset, int count)
String copyValueOf(char[] data, int offset, int count..., int count) method of String class. We are going to use copyValueOf(char[] data, int offset, int count) method of String class in Java
how to pass a string from one class to another
how to pass a string from one class to another  hi all, Good morning. I am trying to develop a coding pass a string from one class and declare... string from one class to another. import java.util.*; class A{ static String
how to use string of one class into another Class
how to use string of one class into another Class  I created textField(JTextField t1=new JTextField();(it is in ClassA. The input of this textField i.e t1.getText() is to be used in different(another Class say in ClassB. How can
Count Palindromes from the string
Count Palindromes from the string In this section, we are going to find out... { public static void main(String[] args) { int count = 0; Scanner input = new...++; } } System.out.println("Number of Palindromes in the specified string: " + count
Java Count Vowels
Java Count Vowels       In this program you will learn how to count vowels in a String. Here you... will go up to the String's length and will find the number of vowels one by one
plz give me program to this: Given a string and a number ‘n’, find the ‘n’th distinct repeating character.
plz give me program to this: Given a string and a number ?n?, find the ?n?th distinct repeating character.    Given a string and a number ?n?, find the ?n?th distinct repeating character. For example, Input: Abracadabra, 1
2. Given a string and a number ‘n’, find the ‘n’th distinct repeating character.
2. Given a string and a number ?n?, find the ?n?th distinct repeating character.    Given a string and a number ?n?, find the ?n?th distinct repeating character. For example, Input: Abracadabra, 1 Abracadabra, 2 Abracadabra, 3
In data structure in java how to parse the given string and counts the member of character that match the given data?
In data structure in java how to parse the given string and counts the member of character that match the given data?  Design a parser that parse the given string and counts the member of character that match the given data.using
String progrm
String progrm  wap to count character of given string in java without using any method
Write a java program that encrypt and decrypt some string by adding or removing 2 on each character.
Write a java program that encrypt and decrypt some string by adding or removing 2 on each character.  Write a java program that encrypt and decrypt some string by adding or removing 2 on each character. Example 1 : Please Enter
count characters
main(String[] args) { int count=0; Scanner input=new Scanner...]=='a'){ count++; } } System.out.println("Character... CountCharacter { public static void main(String[] args) { int count=0
how to check and print the no of times an integer is repeated in an array
how to check and print the no of times an integer is repeated in an array  how to check and print the no of times an integer is repeated...(String[] args) throws Exception{ int array[]={2,4,2,5,2,4,3,4,5,2}; for(int
Split string after each character using regular expression
Split string after each character using regular expression... to split the String after each character using expression. For this we are going... SplittingFind.java are described below: String regex = ("\\w.+"):- Creating
String Determining a Character's Unicode Block
String Determining a Character's Unicode Block   ... explanation about the UnicodeBlock() method of String class. We are going to use UnicodeBlock() method of String class in Java. The description of the code is given
Count letters in a string.
Description: This tutorial demonstrate how to find number of letter exists in a string. The str.length() method find the length of string. Code...;public static void main(String[] args) {   
display repeated lines in a file
display repeated lines in a file  i want a java program that accepts file from user and displays the repeated lines in a file
Removing Line Termination Characters from a String
symbol where the new line character was present in the string or text... Removing Line Termination Characters from a String... of with program code which reads the string or text of a text file which
String
String  write down the code of remove any character from a given string without using any string function   please give me the code of remove any given character from a given string without using function
Java : Length of String
Java : Length of String In this tutorial we are going to write logic to count... Roseindia"; int count = 0; /* String method to print length of String... of String */ for (char c : str.toCharArray()) { count
What is Character Functions
What is Character Functions  What is Character Functions   Hi, Here is the answer,ADS_TO_REPLACE_1 Character Functions are INITCAP, UPPER.... Group Functions returns results based upon groups of rows rather than one result