finding the number of occurrences for a specific char using recursion

count characters in string recursion in c
count characters in string recursion python
how to count the number of occurrences of a character in a string in java
count occurrences of a substring recursively
count characters in string recursion java
recursive function to do substring search java
count occurrences of word in string java
c program to count the number of occurrences of a character in a string

This code below is part of a program, that will find the number of occurrences of the input character in a text file

public static void main(String[] args){
    [...]
    java.io.File file1=new java.io.File(dirPath1);
    FileInputStream fis = new FileInputStream(file1);
    System.out.println("  return "+rec(sc.next().charAt(0),fis));
}



public static int rec (char ch, FileInputStream fis)throws IOException{
    char current=0;
    if(fis.available()==0){
        return 0;
    }
    if(fis.read()!=-1){
        current = (char) fis.read();
    }

    if(current==ch) {     
        return 1+rec(ch,fis);
    }else
        return rec(ch,fis);
    }
} 

The problem is:

If the file has one character, and ch=that one character. it returns 0, when I traced the code I found that it doesn't enter if(current==ch). Although, they are the same char.

if there is more than on character,strings some of them-the matches chars- will enter if block and others won't. How can I fix this ?

Is there another way to find the number of occurrences recursively ?

another question: should I use try and catch in rec method to catch IOException ?

Thanks in advance

P.S. this program is from assignment,I have to use recursion and compare it with iteration.

you call fis.read() twice first one read first character and second one read nothing

this is your answer

public static int rec(char ch, FileInputStream fis) throws IOException {

    char current = 0;
    if (fis.available() == 0) {
        return 0;
    }
    int read = fis.read();
    if (read != -1) {
        current = (char) read;
    }

    if (current == ch) {

        return 1 + rec(ch, fis);
    }

    else
        return rec(ch, fis);

}

Count occurrences of a substring recursively, Given two strings str1 and str2, the task is to count the number of times “str2” occurs in “str1” using recursion. Examples: Input : str1 = "geeksforgeeks", str2 = "​geek"  Re: Using recursion to find the number of occurences of a character in a string Then check to see if the first character of the string is the passed in character. If so, return one plus the results of the freq() with the first character removed from the string.

My suggestion would be as follows:

  1. Read the whole text file into a java.lang.String
  2. Then use the library Apache Commons Lang and use this method for counting the occurrences: http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#countMatches-java.lang.CharSequence-java.lang.CharSequence-

How To Count Occurrences Of Each Character In String In Java, How do you count occurrences of a character in a string? Just note that this solution is technically correct, but sub-optimal, as it's overkill to use the very powerful regular expressions to solve such a simple problem as finding the number of occurrences of a character in a string. 2.5. Using Java 8 Features

You should use FileReader to read chars from text file.

Reader reader = new FileReader("MyFile.txt");

I think using while ((i=reader.read()) != -1) is a better approach instead of three if and an else.

So you can achieve this with fewer lines of code:

public static int rec (char ch, Reader reader)throws IOException{
    char current=0;
    int i; 
    while ((i=reader.read()) != -1) {
        current = (char) i;
        if(current==ch) {     
            return 1+rec(ch,reader);
        }else
            return rec(ch,reader);
    }
    return 0;  
} 

I think there is no need to use try and catch in rec method to catch IOException. I have used it here:

    try {
        Reader reader = new FileReader("MyFile.txt");
        System.out.println("  return " + rec('a', reader));
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

C++ Program to Find the Frequency of Characters in a String, How do you find the number of occurrences of a char in a string C++? Within the body of the loop, each character of the string is checked against ch for equality. In case of a match, the variable count is incremented. When this loop exits, the variable count will have the number of occurrences of the given character in the given string. C Program To Find The Number Of Occurrences Of a Character In a String:

Example: number of occurrences of a character in a string, Recursive characterization of the operation of counting the occurrences of the character c in the string s: if s is the empty string "", then return 0; otherwise, if the first character of s is equal to c, then return 1 plus the number of occurrences of c in the string equal to s without the first character; Find if string is K-Palindrome or not using all characters exactly once Recursive function to do substring search Given a text txt[] and a pattern pat[], write a recursive function “contains(char pat[], char txt[])” that returns true if pat[] is present in txt[], otherwise false.

Count Occurrences of a Char in a String, Learn how to count characters with the core Java library and with libraries and Use the Guava CharMatcher to work with Strings - remove special chars, A less obvious, but still interesting solution is to use recursion: ? Inside the Python For Loop, we used the If statement to check whether any character in a string is equal to the given character or not. If true, then count = count + 1. If true, then count = count + 1.

C Program To Count Occurrences Of A Character In String, C program to count the occurrences of a character in a given string – In this article, we will discuss Using Standard Method; Using Function; Using Recursion. C program to count the occurrences of a character in a given string – In this article, we will discuss the various means to count the occurrences of a character in a given string in C programming. Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly.

Comments
  • Instead of reading byte by byte, you could try using BufferedReader betterFileReader = new BufferedReader(new FileReader(PATH)); . This way you can directly read chars.
  • Thanks, it helped me.