tags:

views:

809

answers:

6

When I compile this program I only get the first capital letter but not the rest.

Input:

ABldjfdslkjfCK

I only get 'A' that is it?

#include <stdio.h>
#include <string.h>
FILE *fp;

int main(void)
{   
    int size; 
    char input[100]; // array size of 100 

    if (fp = fopen("message.txt","r")) // file exists
    {
        fgets(input,100,fp);// scans the sentence. 
    }
    else 
    {
    printf("file not found");// if there is no such a file. 
    }  

    size=strlen(input);  
    recursive(size,input); 

    return 0;
}

int recursive(int size, char array[])
{
    static int index = 0; // static so when the function is called, the  value is kept

    if (index < size) // start from index 0 until the size-1
    {
        if (array[index] >= 'A' && array[index] <= 'Z') // check for A to Z  (CAPITALIZE CHARACTERS only)
        {
            printf("%c\n", array[index]); // print it
        }
        else 
        {
            return recursive(size,array); // calls the function (recursion)
        }
    }
    return 0;
}
+4  A: 

your recursive function calls to itself only if it finds a non capital character. when it finds the first capital character it prints it and quits

Raz
+1  A: 

The first error I see is that you never increment index.

jeffamaphone
+12  A: 

You never increment the value of index. Furthermore, you do not call the function recursive if the current character is a capital letter, so the function just returns.

Rather than using a static variable for index, it would be better to pass it in as an argument to recursive; otherwise, the function is non-reentrant.

James McNellis
Reentrant - http://en.wikipedia.org/wiki/Reentrant_%28subroutine%29 (for anyone who doesn't remember what it means, like me :)
Edan Maor
+2  A: 

Two problems:

  1. You are terminating your recursion incorrectly. You need to keep recursing until you reach the end of the array.
  2. You have a second problem where you are not incrementing index so each call will always look at the first character.
R Samuel Klatchko
+3  A: 

Your current function prints only A because at soon as it finds an uppercase letter (A in your case) it returns a 0.

There are other issues too, so I would rewrite the function like this:

void recursive(const char* s)
{
 if (s[0] == '\0') // stop condition: empty string
  return;
 if (isupper(s[0])) // main body
  printf("%c\n", s[0]);
 recursive(s+1); // recursion: advance to the next character
}

Use it like this: recursive(input) and include <ctype.h> (for isupper).

Cristian Ciupitu
Of course, this is an example of tail recursion, which is trivially removed by using a while loop instead - but you've captured the intent rather clearly (and the basic rule of successful recursion: know when to stop and when to continue).
Jonathan Leffler
+3  A: 

Among the other issues that your recursive function has is that the index variable is static. That's not a problem in the current versions, since you don't actually use it except in a trivial way. But once you try to fix the other problems (which may result in you using index in s more complicated way), having it static will pose a couple problems:

  • there's no good way to initialize it properly after the first use. This is often fixed by having a non-recursive 'wrapper' function that initializes the state variable and passes it to a private recursive function that does the actual work (and takes the value as a parameter instead of using a static instance). The wrapper just kicks off the function that does the real work.
  • the recursive calls to the function will modify the static variable, which will modify the state of the 'saved' instance of the in progress recursive calls. This might not be a problem if the recursion is a type known as 'tail recursion' where the caller that performs the recursive call won't perform any additional work after the recursive call returns, but not all recursive functions are tail recursion. Though your example could easily be (I'd still try to avoid the static variable, though).
Michael Burr