Write a C Program to find First Capital Letter in String using Recursion

C Program to find First Capital Letter in String


Write a C Program to find First Capital Letter in String using Recursion. Here’s simple Program to find First Capital Letter in String using Recursion in C Programming Language.


Recursion : :


  • Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.
  • The C programming language supports recursion, i.e., a function to call itself. But while using recursion, programmers need to be careful to define an exit condition from the function, otherwise it will go into an infinite loop.
  • Recursive functions are very useful to solve many mathematical problems, such as calculating the factorial of a number, generating Fibonacci series, etc.

Problem : :


The following C program, using recursion, finds the first capital letter that exists in a string. We have included ctype.h  in order to make use of “isupper(char);” function that’s defined inside the ctype.h headerfile.

The isupper() function returns 1 if the passed character is an uppercase and returns 0 is the passed character is a lowercase.


Here is the source code of the C program to find the first capital letter in a string using recursion. The C program is successfully compiled and run on a Windows system. The program output is also shown below.


SOURCE CODE : :


/* C program to find the first capital letter in a string using recursion */


#include <stdio.h>
#include <string.h>
#include <ctype.h>
 
char caps_check(char *);
 
int main()
{
    char string[20], letter;
 
    printf("Enter a string to find it's first capital letter: ");
    scanf("%s", string);
    letter = caps_check(string);
    if (letter == 0)
    {
        printf("No capital letter is present in %s.\n", string);
    }
    else
    {
        printf("The first capital letter in %s is %c.\n", string, letter);    }
        return 0;
    }
    char caps_check(char *string)
    {
        static int i = 0;
        if (i < strlen(string))
        {
            if (isupper(string[i]))
            {
                return string[i];
            }
            else
            {
                i = i + 1;
                return caps_check(string);
            }
        }
        else return 0;
    }

Output :


************OUTPUT*********************

Enter a string to find it's first capital letter: codezClub

The first capital letter in codezClub is C.

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach upto you in the short interval.


Thanks for reading the post….

Leave a Reply