C Program to Find the First Capital Letter in a String using Recursion

«
»

This is a C Program to find the first capital letter in a string using recursion.

Problem Description

The following C program, using recursion, finds the first capital letter that exists in a string.

Problem Solution

We have included ctype.h in order to make use of “int isupper(char);” function that’s defined inside the ctype.h headerfile. The isupper finction returns 1 if the passed character is an uppercase and returns 0 is the passed character is a lowercase.

advertisement
Program/Source Code

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 Linux system. The program output is also shown below.

/*
 * 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;
    }
Program Explanation

In this C program, library function defined in < ctype.h > header file is used to compute mathematical functions. We are reading a string using string[] array variable.

advertisement
advertisement

The caps_check() function is used to find the first capital letter in a string using Recursion. Nested if else condition statement is used to check the value of ‘i’ variable is less than the length of the string. If the condition is true then execute the statement.

Another if condition statement is used to check the character is an uppercase using isupper() function. If the condition is true then return the value 1. Otherwise, if the condition is false, then execute else statement and return the value 0.

If else condition statement is used to check the value of the ‘length’ variable is equal to 0. If the condition is true then execute the statement and print no capital letter is present in the string. Otherwise, if the condition is false, then execute the else condition statement and print the statement as the first capital letter in the given string.

advertisement
Runtime Test Cases
 
$ cc pgm32.c
$ a.out
Enter a string to find it's first capital letter: iloveC
The first capital letter in iloveC is C.

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms

advertisement
If you wish to look at other example programs on Strings, go to C Programming Examples on Strings. If you wish to look at programming examples on all topics, go to C Programming Examples.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!
advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter