Home Answers Viewqa Java-Beginners count the repeated character in one string

 
 


gokulavanan
count the repeated character in one string
2 Answer(s)      2 years and 5 months ago
Posted in : Java Beginners

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 Pages:
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
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... class, we have found that 'm' is repeated successively. Here is the code of Main
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 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
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
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 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
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
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
Java: Example - Count occurences
Java: Example - Count occurences Problem: Count the number of times one string is found in another. //-------------------------------------------- count() int count(String base, String searchFor) { int len
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
What is Character Functions
What is Character Functions  What is Character Functions   Hi, Here is the answer, Character Functions are INITCAP, UPPER, LOWER, SUBSTR... returns results based upon groups of rows rather than one result per row, use group
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
integer to string
integer to string  i have to develop a program which convert integer into character from 1 to 10,000 if we input 1 then it give output 'one' n so... a look at the following link: Java Count number to words
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
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
Exercise - Count Words
the string one character at a time. Use a boolean variable to indicate whether... between words should only count if they include at least one blank so... Java: Exercise - Count Words Problem Write a method which counts
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 
Removing character from a string in sql.
Removing character from a string in sql.  How to remove or replace character from a string
Removing character from a string in sql.
Removing character from a string in sql.  Removing special characters from a string in sql
word and character counting - Java Beginners
word and character counting  here is the java code i made but i have... javax.swing.*; import java.io.*; public class count { public static void main(String args[]) { String inFile = "c:\\LowerCase.txt"; String outFile
Exercise - Count Words
the string one character at a time. Use a boolean variable to indicate whether... between words should only count if they include at least one blank so... Java: Exercise - Count Words Problem Write a method which counts
javascript regex validate Special character
the string. Test() method takes one argument and check that to the pattern...javascript regex validate Special character   javascript regex validate Special character   <html> <head> <title>Zip
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
Count characters from text file in Java
it into character array in order to count the occurrence of each character...Count characters from text file Using Java Here we are going to count the occurrence of each character from the text file. For this, we have used
MySQL String Function
(str, count) function returns a string that consist a String str repeated... returns the ASCII code value of the leftmost character of the String str. Returns...,... [USING charset_name] ) function  return a string consisting the character
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) {  
Count number of "*"
:"); String text = bf.readLine(); int count = 0; for (int i = 0; i...Count number of "*"  I have this code to count the number of * from a string entered. but I need to find it from an text file. Any idea? import
Character from text file - Java Beginners
Character from text file  Write a program that reads characters from a text file. Your program will count how many time each character appear...: ?The contents of a string cannot be changed once the string is created.? Character
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
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
Count characters from text file in Java
is a java code that count the occurrence of each character from text file. import...Count characters from text file in Java  At the "Count chracters fro... void main(String[] args) throws Exception{ FileInputStream fstream = new
Count Vowels
Count Vowels   Read a string from the user, your program counts the number of vowels in the text and have it report a sum of each vowel found as well as the number of spaces. Make it in two classes
Java file line count
main(String[] args) throws Exception { int count = 0; File f = new File("C...()) { String line = input.nextLine(); count++; } System.out.println...Java file line count In the section, you will learn how to count the number
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
string
string  read a sentence and replacing character with immediate preeceding character ex: input:Be careful output:Ad bzqdetk
Java count characters from file
into character array in order to count the occurrence of each character... read the file and count the occurrence of each character of the content...Java count characters from file In this section, you will learn how to count
How to convert entity character to html character
How to convert entity character to html character  Please help me to "convert entity character to html character" in java. If possible please provide source code which take String as input and return as 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...++; String word = str.substring(count + 1, i); if (map.containsKey(word)) { map.put
Programming: Count Words - Dialog
, and displaying it (plus 1) at the end. Looking at every character in a string... it exceeds the last character index in the input string. Here is a rough draft... in the string. 5. If the character at that position is a blank 6
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
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 : string substr (string $var... string of variable $var, the initial index position is denoted by $initial
Programming: Count Words - Dialog
. Loop. Loop across the string one position at a time. Extra credit... Java NotesProgramming: Count Words - Dialog Description Write a program... words, and so is "test this". Only count blanks if the last
string
() ) { String token = st.nextToken(); Character ch=Character.valueOf(token.charAt...string  a java program to input a string and display the string with the first character of every word in capital   import java.util.
Hibernate Projection Count
Hibernate Projection Count       In this section, you will learn to hibernate projection count.... A unidirectional one-to-many association on a foreign key is rarely required
string
string   just i want to a program in a short form to the given string in buffered reader for example input string: Suresh Chandra Gupta output: S. C...; public class StringTest { public static void main(String [] args

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.