Question

i need a litle bit help. i need to write this method recursively: public static int howManyChar (String s, char c) and without using the substring method and loops..

any ideas? i am stuck at this point.

public static int howManyChar (String s, char c)
{
    int count = 0;
    int lastChar = s.length()-1;
    if (s.length()== 0)
        return 0;
    if (s.charAt(lastChar) == c)
    {
       count++;
    }

i have succeeded using the substring method:

    int count =0;
    if (s.charAt(0)==c)
        count++;
    if (s.length()>1)
    {
        count+=howManyChar(s.substring(1), c);
    }
    return count;

but i want to write the code without using substring.

Was it helpful?

Solution

try this

public static int howManyChar (String s, char c) { if (s.length()==0) return 0; return s.charAt(0) == c ? 1 : 0 + howManyChar(s.substring(1), c); }

OR if you cannot use the substring method

public static int howManyChar (String s, char c, int pos) { if (pos >= s.length()) return 0; return s.charAt(pos) == c ? 1 : 0 + howManyChar(s, c, ++pos); }

OTHER TIPS

I have looked at some of the answers here, they are either too complicated for your question, use substring, or do not use basic recursion

your solution should be something like this.

function countChar(String a, char b, int i)
if(i==a.length)
return 0
else if(b ==a.charAt(i))
return 1 + function countChar(a, b, i+1)
else
return function countChar(a,b,i+1)

This part of the code is the loop.

function countChar(String a, char b, int i)
{
   if(i==a.length) 
   {
       return 0
   }
   else 
   {
      //put here a print statement to see what happens.
     // print charAt(i) or i itself
      return function countChar(a, b, i+1) //<-- moving to the next index
   }
}

Learn that loop. You should use charAt(i). Substring is not neccesssary..

In order to fulfil the requirement, you can wrap your method in another recursive one.

function howManyChar(String a, Char b)
{
   countChar(a, b, 0);
}
public static int howManyChar (String s, char c)
{
    index++;
        if (s.length() == index)
            return 0;
        else if(s.charAt(index) == c)
           return 1 + howManyChar(s, c);
        else
            return 0 +  howManyChar(s, c);

  }

Now this isn't the best soultion, I have a class level variable (index) holding the place in the string. But without using substring I can't think of a better way to do it.

I have a good solution without subscript...

public class test{

   int counter=0;int last;
    public static void main(String[] args){
        test t1=new test(); //object of test
        String line="This is a working code"; //line in which char to be counted
        char c='q'; // char to be counted
        t1.last=line.length()-1;  //setting last
        System.out.println("In sentence: "+line);
        if(t1.count(line, c)!=-1)
        System.out.println("The character "+c+" is "+t1.counter+" times"); //printing result
        else
        System.out.println("The character "+c+" is not found");
    }
    public int count (String line, char c){
        if (last<0){   // case for empty string or nil char.
            return counter;                     // recursion ends here and return counted
        }
        else if(line.charAt(last)==c)
        {
            counter++;  //counting
        }
        last--;  //shitin last;
        count(line, c);
        return -1;  //if even a single times is not found then return -1;
      }
}

It may help you...You may take line and char which is to be searched as input from Command prompt......Thank you....

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top