Like Tree2Likes
  • 2 Post By Adak

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. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    19

    Smile 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. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

    Instead of posting your assignment, I want you to post your attempt at solving your assignment, and ask very specific questions about what has you stumped, if you should be stumped on anything.

    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. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    > 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;
    return localAnswer + recursiveFunc(s+1);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    19
    Quote Originally Posted by quzah View Post
    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. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by tarunjain07 View Post
    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.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Jul 2011
    Posts
    19
    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. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.
    quzah and AndrewHunter like this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. substring in C
    By -EquinoX- in forum C Programming
    Replies: 2
    Last Post: 04-18-2008, 01:39 PM
  2. substring of a variable...
    By Mythic Fr0st in forum C++ Programming
    Replies: 2
    Last Post: 01-17-2007, 06:53 PM
  3. Getting a substring help plz
    By joshua in forum C Programming
    Replies: 3
    Last Post: 11-01-2005, 05:11 PM
  4. character substring
    By ghe1 in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2002, 10:34 AM
  5. How to substring ?
    By appsforum in forum C Programming
    Replies: 1
    Last Post: 09-10-2001, 03:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21