#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    1
    Rep Power
    0

    Recursive function help


    Hi there guys,

    As part of my course work, I'm required to write a recursive function with the following signature:

    int cpyUpperCase(const char A[], char B[])

    The intention is that the recursive function would go through all elements in A and check if it's upper case and if so.. put that element in B.

    The actual function should return the number of upper case letters found.

    Please help.

    So far, I managed to write a non-recursive version of the function, not sure how to convert to recursion.

    Code:
    int cpyUpperCase(const char A[], char B[])
    {
    	int counter = 0;
    	int upperCaseCounter = 0;
    	int sizeOfA= strlen(A);
    	for(counter=0; counter<sizeOfA; counter++)
    	{
    		if(isupper(A[counter]))
    		{
    			upperCaseCounter++;
    			B = (char*) realloc (B, upperCaseCounter * sizeof(char));
    			B[upperCaseCounter-1]= A[counter];			
    		}
    	}
    
    	return upperCaseCounter;
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    187
    Rep Power
    82
    One possible solution would be to use a static int variable in your recursive function cpyUpperCase to be used as an index in your B char array which stores only the upper case characters found in your A character array.

    Upon entering your recursive array, you would first iterate thru your A input array. You would check the first character in the A array to determine if it was upper case or not. If upper case, you would add it to your B array and increment your static int variable to point to the next position in your B array. Next you would call cpyUpperCase function using the A array ( the input A array would be incremented by one position to allow processing on each consecutive character of the A array) and the B array from within cpyUpperCase. That is, you are recursively calling cpyUpperCase from within cpyUpperCase to process each letter in the A array.

    You would continue with this procedure until you encounter the null character in your A array.

    There are a couple of other solutions but IMHO, they're just too convoluted to be easily implemented.

    Comments on this post

    • jakotheshadows agrees

IMN logo majestic logo threadwatch logo seochat tools logo