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 '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");

}

}

Ads









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
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
How to display string or character
How to display string or character  how to print character or a word without using array concept
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
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
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
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
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
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
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
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...++; String word = str.substring(count + 1, i); if (map.containsKey(word)) { map.put
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
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
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.... to console. Example  : A Code to count the number of vowel in a string
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: This program takes a character at the console and converts it into a string format using
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
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
Java Count Vowels
Java Count Vowels       In this program you will learn how to count vowels in a String. Here you... to the String's length and will find the number of vowels one by one as shown
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
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

Ads