Recursively counting character occurrences in a string

Noob

Im making a program to count the number of times a character is found in a string. This is what my method looks like:

public static int count (String line, char c)
{
    int charOccurences = 0; //= 0;

    for (int x = 0 ; x < line.length () ; x++)
    {
        if (line.charAt (x) == c)
        {
            charOccurences++;
            line = line.substring (0, x) + line.substring (x + 1);
            return count (line, c);
        }
        else
            return charOccurences;
    }
    return charOccurences;
}

It always returns 0, due to the fact that once the method calls itself it sets charOccurences back to 0. But i need to declare that variable for the method to work. I cant figure any way around this. Any help would be appreciated.

Rainbolt

You ignored charOccurences right after you incremented it.

charOccurences++;
line = line.substring (0, x) + line.substring (x + 1);
return charOccurences + count (line, c); // Fixed for you.

Others have mentioned that you don't need a for loop at all. If you wanted to do this purely recursively, you would simply lose the loop, and follow these steps:

  • base case:
    • first character doesn't exist (length is zero)
      • return 0;
  • recursion case:
    • The first character does exist
      • if it matches, increment occurrences
      • else do nothing
      • return (occurrences) + (result of recursing with substring);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Counting number of occurrences recursively

From Dev

Counting the Occurrences of a User Specified Character in a String

From Dev

Counting occurrences of a character in a string within a cell

From Dev

Counting the character recursively

From Dev

Counting occurrences of a string in a string

From Dev

counting occurrences of a string

From Dev

Recursively counting occurrences in a nested list of numbers

From Java

Counting occurrences of word in a string in Pandas

From Dev

Counting the number of digit occurrences in a string

From Dev

Find a character recursively in a String

From Dev

Counting number of character occurrences per line

From Dev

Counting All Character Occurrences in a Text File in C

From Dev

Counting the number of occurrences of a substring within a string in PostgreSQL

From Dev

Counting occurrences of a string in a column of a csv file

From Dev

Counting the number of specific occurrences in a java String

From Dev

issue counting number of letter occurrences in string

From Java

Count the number occurrences of a character in a string

From Dev

Find a number of occurrences of character in string

From Dev

Average of occurrences of a character in a string array

From Dev

Find a number of occurrences of character in string

From Dev

Counting repeated characters around nth character of string

From Dev

Manually counting the number of character in a string in C

From Dev

Find the largest ASCII character in a string recursively - java

From Dev

Find the amount of occurances of a character in a string recursively (java)

From Java

Count the number of occurrences of a character in a string in Javascript

From Dev

replace all occurrences of a character in a string in java?

From Java

How to replace all occurrences of a character in string?

From Dev

Regex to limit the number of occurrences of a particular character in a string

From Dev

Using strchr() to count occurrences of a character in a string

Related Related

  1. 1

    Counting number of occurrences recursively

  2. 2

    Counting the Occurrences of a User Specified Character in a String

  3. 3

    Counting occurrences of a character in a string within a cell

  4. 4

    Counting the character recursively

  5. 5

    Counting occurrences of a string in a string

  6. 6

    counting occurrences of a string

  7. 7

    Recursively counting occurrences in a nested list of numbers

  8. 8

    Counting occurrences of word in a string in Pandas

  9. 9

    Counting the number of digit occurrences in a string

  10. 10

    Find a character recursively in a String

  11. 11

    Counting number of character occurrences per line

  12. 12

    Counting All Character Occurrences in a Text File in C

  13. 13

    Counting the number of occurrences of a substring within a string in PostgreSQL

  14. 14

    Counting occurrences of a string in a column of a csv file

  15. 15

    Counting the number of specific occurrences in a java String

  16. 16

    issue counting number of letter occurrences in string

  17. 17

    Count the number occurrences of a character in a string

  18. 18

    Find a number of occurrences of character in string

  19. 19

    Average of occurrences of a character in a string array

  20. 20

    Find a number of occurrences of character in string

  21. 21

    Counting repeated characters around nth character of string

  22. 22

    Manually counting the number of character in a string in C

  23. 23

    Find the largest ASCII character in a string recursively - java

  24. 24

    Find the amount of occurances of a character in a string recursively (java)

  25. 25

    Count the number of occurrences of a character in a string in Javascript

  26. 26

    replace all occurrences of a character in a string in java?

  27. 27

    How to replace all occurrences of a character in string?

  28. 28

    Regex to limit the number of occurrences of a particular character in a string

  29. 29

    Using strchr() to count occurrences of a character in a string

HotTag

Archive