i can't find the answer. please help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • idle
    New Member
    • Oct 2006
    • 7

    i can't find the answer. please help

    #include<stdio. h>
    int count (char letter, const char *my_string);
    void main()
    {
    int total_l;
    char my_string[] = “walla la oh la la la la la long”;
    char letter = ‘l’;
    total_l = count(letter, my_string);
    printf(“There are %d character l in the string\n”, total_l);
    }

    *required to write a recursive function to count the number of times character ‘l’
    appears in a string “walla la oh la la la la la long”

    * prints that number on your screen. Your function should be able to;

    (a) Count character ‘l’.
    (b) Test whether the character ‘l’ is in the string.
    (c) If the string had no character at all, we would know immediately that there
    were zero occurrences of the character being counted.
    (d) If there is any, your program should track the number of occurrence.
  • ltgbau
    New Member
    • Sep 2006
    • 41

    #2
    Code:
    int count(char ch, char str[])
    {
    	int cnt=0;
    	if(strlen(str)==1)
    	{
    		if(str[0]==ch)
    			cnt++;
    	}
    	else
    	{
    		if(str[0]==ch)
    			cnt++;
    		cnt+=foo(ch, str+1);
    	}
    	return cnt;
    }

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Originally posted by ltgbau
      Code:
      int count(char ch, char str[])
      {
      	int cnt=0;
      	if(strlen(str)==1)
      	{
      		if(str[0]==ch)
      			cnt++;
      	}
      	else
      	{
      		if(str[0]==ch)
      			cnt++;
      		cnt+=foo(ch, str+1);
      	}
      	return cnt;
      }
      You've called foo instead of count.

      I think you will find this goes rather wrong for

      count( 'l', "");

      which is a specific return case mentioned.

      Comment

      • ltgbau
        New Member
        • Sep 2006
        • 41

        #4
        yes, the condition is not strict enough :D
        i don't know why he needs a recursive function to count the number of times character ???

        sorry for my mistake :)




        Code:
         [left]int count(char ch, char str[])
        
         
        {
        int cnt=0;
        [b]int len=strlen(str);[/b]
        [b]if(len<=0)[/b]
        [b]{[/b]
        [b]return 0;[/b]
        [b]}[/b]
        [b]else[/b] if(len==1)
        {
        if(str[0]==ch)
        cnt++;
        }
        else
        {
        if(str[0]==ch)
        cnt++;
        cnt+=[b]count[/b](ch, str+1);
        }
        return cnt;
        }
        [/left]


        Comment

        • idle
          New Member
          • Oct 2006
          • 7

          #5
          i've treid already.
          but why this error still there.

          error C2065: 'strlen' : undeclared identifier

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Because you haven't got

            #include <string.h>

            in your file (at the top preferably)

            Comment

            • idle
              New Member
              • Oct 2006
              • 7

              #7
              What is it means by

              " missing function header (old-style formal list?)"

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                Often a ; where there shouldn't be one

                Code:
                void fn()[b];[/b]
                {
                }
                should be

                Code:
                void fn()
                {
                }

                Comment

                Working...