recursive function

This is a discussion on recursive function within the C Programming forums, part of the General Programming Boards category; I am trying to follow the logic of the following program: Is the mystery function in effect counting the # ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    23

    recursive function

    I am trying to follow the logic of the following program:

    Is the mystery function in effect counting the # of instances a character variable shows up in a string? Please explain the code in the mystery function. I am not getting what is going on when the function calls itself.

    here's the whole program. There are three separate character variables being used to call the mystery function.

    Code:
    #include <stdio.h>
    
    int mystery(char, char * );
    
    main()
    
    {
    
       char ch='i';
       char chr = 't';
       char charac = 'u';
       char str[20]="utilities";
    
       mystery(ch,str);
       printf("returned : %d\n",mystery(ch,str));
       mystery(chr,str);
       printf("returned : %d\n",mystery(chr,str));
       mystery(charac,str);
       printf("returned : %d\n",mystery(charac,str));
    
    
    }
    
    int mystery (char ch, char *str)
    
    {
    
      int ans;
       if (str[0]=='\0')
         ans = 0;
       else
          if (ch==str[0])
            ans=1+mystery(ch, &str[1]);  /*  what is this doing exactly */
          else
            ans=mystery(ch,&str[1]);  /* what is this doing exactly */
       return ans;
    }

  2. #2
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284

    Re: recursive function

    mystery counts the number of times the character ch occurs in the string str

    the logic is
    if length of the string is 0 return 0 as there cannot be any occurence of ch
    if the first character of the string is ch , no of times the character ch occurs in the string is 1+(no of times the character ch occurs in str[1], .str[2],........)
    else the number of occurences of ch is just the number of times the character ch occurs in str[1],str[2],................


    Originally posted by Peachy
    I am trying to follow the logic of the following program:

    Is the mystery function in effect counting the # of instances a character variable shows up in a string? Please explain the code in the mystery function. I am not getting what is going on when the function calls itself.

    here's the whole program. There are three separate character variables being used to call the mystery function.

    Code:
    #include <stdio.h>
    
    int mystery(char, char * );
    
    main()
    
    {
    
       char ch='i';
       char chr = 't';
       char charac = 'u';
       char str[20]="utilities";
    
       mystery(ch,str);
       printf("returned : %d\n",mystery(ch,str));
       mystery(chr,str);
       printf("returned : %d\n",mystery(chr,str));
       mystery(charac,str);
       printf("returned : %d\n",mystery(charac,str));
    
    
    }
    
    int mystery (char ch, char *str)
    
    {
    
      int ans;
       if (str[0]=='\0')
         ans = 0;
       else
          if (ch==str[0])
            ans=1+mystery(ch, &str[1]);  /*  what is this doing exactly */
          else
            ans=mystery(ch,&str[1]);  /* what is this doing exactly */
       return ans;
    }

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    23
    PINKO:

    how is the string being access if there is no incrementation of
    str ?? how is str[2] , str[3] being checked against ch??? I don't see where there is incrementation to next element of string each time mystery function is called. Maybe there is a better way to ask this, but I am confused how each character of the string is being accessed here.

    Code:
    
       else
          if (ch==str[0])
            ans=1+mystery(ch, &str[1]);

    And in the else (if ch is not = to str[0])
    this part of code is not adding 1 to the 'count' of ans variable, right???

    Code:
    else
            ans=mystery(ch,&str[1]);

  4. #4
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    you can think of &str[1] as a pointer to the string starting at str+1
    the code could have been equivalently written as
    else
    if (ch==str[0])
    ans=1+mystery(ch,str+1);

    so the function mystery is being applied str+1
    from where if necessary it may be applied to (str+1)+1 etc....

    to see that &str[1] and str+1 correspond to the same address run this code


    #include <stdio.h>

    int main(void)
    {
    char str[100],*p,*q;
    p=str+1;
    q=&str[1];
    printf("p:%p q:%p",p,q);
    return 0;
    }

    Originally posted by Peachy
    PINKO:

    how is the string being access if there is no incrementation of
    str ?? how is str[2] , str[3] being checked against ch??? I don't see where there is incrementation to next element of string each time mystery function is called. Maybe there is a better way to ask this, but I am confused how each character of the string is being accessed here.

    Code:
    
       else
          if (ch==str[0])
            ans=1+mystery(ch, &str[1]);

    And in the else (if ch is not = to str[0])
    this part of code is not adding 1 to the 'count' of ans variable, right???

    Code:
    else
            ans=mystery(ch,&str[1]);

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    23
    ok so, say str[10] = "balance";

    &str[1] will look at this as " alance "
    not as " a " (the first 'a' in the string) ??

    and str+1 will do the same as &str[1] ???

  6. #6
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Originally posted by Peachy
    ok so, say str[10] = "balance";

    &str[1] will look at this as " alance "
    not as " a " (the first 'a' in the string) ??

    and str+1 will do the same as &str[1] ???
    Yes , in this case .

    To test this simply run this program

    #include <stdio.h>

    int main(void)
    {
    char str[]="balance";
    printf("%s\n%s",str+1,&str[1]);
    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive function
    By Fork in forum C Programming
    Replies: 3
    Last Post: 10-26-2006, 11:27 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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