# Substring Function

This is a discussion on Substring Function within the C Programming forums, part of the General Programming Boards category; question is :- int count(String s, char c) that counts how many times the character c occurs inside string s. ...

1. ## Substring Function

question is :-
int count(String s, char c) that counts how many times the character c occurs inside string s.
You can use the String methods charAt and substring to extract information from string s.

* these methods are required to be fully recursive in that they contain no loops at all;
neither for, while or do-while.
*

I guess this a java woreksheet i am doing.
any suggestions/ideas to solve this ?

I tried but i m stuck when i have to get a char out of string(array of char in C)
and to concatenate that in a new string but no such function in c and also i cant use any loops

I have attached the worksheet
recursion.pdf

2. What are you asking? How to do this in Java, or how to do this in C? How about writing it out in words first, then you should be able to do it in either one. The rest is just syntax.

Quzah.

3. Recursion is just another way of looping. It's not smallpox, so don't be afraid of it.

Maybe start with just printing out a string of chars, one at a time, using recursion. Sort of, get comfortable with how recursion works.

It's up to you to get this work started. Show that you've put work into it, and have a thorough understanding of the assignment.

4. > any suggestions/ideas to solve this ?

> int count(String s, char c) that counts how many times the character c occurs inside string s.

Consider the case of when s is a single character string.

Mostly, it is about advancing through the string 1 char at a time.
Code:
```localAnswer = something;

5. Originally Posted by quzah
What are you asking? How to do this in Java, or how to do this in C? How about writing it out in words first, then you should be able to do it in either one. The rest is just syntax.

Quzah.
IN C ..yea i have some idea how to do this but cant do in C

6. Originally Posted by tarunjain07
IN C ..yea i have some idea how to do this but cant do in C
Like I said, put it into words first. If you can't express exactly how to do the job with actual words, then you will very likely not be able to program it either.

Quzah.

7. This code is working fine..
is it fine ? some errors etc

Code:
```#include<stdio.h>
#include<string.h>
#include<stdlib.h>
static int i=-1;;
static int cont=0;
int count(char s[], char c)
{
i++;
if(strlen(s)==0)
{
return 0;
}
if(s[i]==c && i<strlen(s) )
{
cont++;
count(s,c);

}

if(s[i]!=c && i<strlen(s))
{
count(s,c);
}

return cont;
}
int main(void)
{
char a[]="Hello Tarun Jain Jain aaa Hello HelloH" ;
int b;
b=count(a,'H');
printf("%d\n",b);
return 0;

}```

8. Try calling the function again.

And no, you count should be a local variable INSIDE your recursive function.

And you should be able to eliminate ALL your strlen calls.
Think about what s[0] is when you're at the end of the string.

9. Is your program working fine?

You should know!

Does it have errors? What are those errors?

You should know, and you should post what those errors are!

For crying out loud.