This is a C Program to find the first capital letter in a string without using recursion.
The following C program, using iteratiom, finds the first capital letter that exists in a string.
We have included ctype.h in order to make use of “int isupper(char);” function that’s defined inside the ctype.h header file. The isupper finction returns 1 if the passed character is an uppercase and returns 0 is the passed character is 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 Linux system. The program output is also shown below.
/* * C Program to find the First Capital Letter in a String without * 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) { int i = 0; while (string[i] != '\0') { if (isupper(string[i])) { return string[i]; } i++; } return 0; }
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.
The caps_check() function is used to find the first capital letter in a string. While loop is used to check till the end of the character is present in a string is the first capital letter in that string.
If condition statement is used to check the character present in a string is an uppercase using isupper(). If the condition is true then it will return 1. Otherwise, if the condition is false then execute else statement and return the value as 0.
If else condition statement check the value of ‘length’ variable is equal to 0. If the condition is true print the output as no capital letter is present in the string. Otherwise, if the condition is false then execute the else condition statement and print the output as the first capital letter in the given string.
$ cc pgm35.c $ a.out Enter a string to find it's first capital letter: prOgraMmInG The first capital letter in prOgraMmInG is O.
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Buy C Books
- Buy Computer Science Books
- Apply for C Internship
- Practice BCA MCQs
- Watch Advanced C Programming Videos