
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
First uppercase letter in a string (Iterative and Recursive) in C++
In this tutorial, we are going to learn how find the first uppercase letter in the given string. Let's see an example.
Input −Tutorialspoint
Output −T
Let's see the steps to solve the problem using iterative method.
Initialize the string.
Iterate over the string.
Check whether the current character is uppercase or not using isupper method.
If the character is uppercase than return the current character.
Example
Let's see the code.
#include <bits/stdc++.h> using namespace std; char firstUpperCaseChar(string str) { for (int i = 0; i < str.length(); i++) if (isupper(str[i])) { return str[i]; } return 0; } int main() { string str = "Tutorialspoint"; char result = firstUpperCaseChar(str); if (result == 0) { cout << "No uppercase letter" << endl; } else { cout << result << endl; } return 0; }
Output
If you run the above code, then you will get the following result.
T
Let's see the steps to solve the problem using recursive method.
Initialize the string.
Write a recursive function which accepts two parameter string and index.
If the current character is end of the string then return.
If the current character is uppercase then return the current character.
Example
Let's see the code.
#include <bits/stdc++.h> using namespace std; char firstUpperCaseChar(string str, int i = 0) { if (str[i] == '\0') { return 0; } if (isupper(str[i])) { return str[i]; } return firstUpperCaseChar(str, i + 1); } int main() { string str = "Tutorialspoint"; char result = firstUpperCaseChar(str); if (result == 0) { cout << "No uppercase letter"; } else { cout << result << endl; } return 0; }
Output
If you run the above code, then you will get the following result.
T
Conclusion
If you have any queries in the tutorial, mention them in the comment section.
- Related Articles
- Make first letter of a string uppercase in JavaScript?
- Count consonants in a string (Iterative and recursive methods) in C++
- Binary Search (Recursive and Iterative) in C Program
- How to lowercase the entire string keeping the first letter in uppercase with MySQL?\n\n\n
- Find Length of a Linked List (Iterative and Recursive) in C++
- Count half nodes in a Binary tree (Iterative and Recursive) in C++
- Count full nodes in a Binary tree (Iterative and Recursive) in C++
- C Program for Binary Search (Recursive and Iterative)?
- Program for average of an array(Iterative and Recursive) in C++
- Check if linked list is sorted (Iterative and Recursive) in Python
- How to test if a letter in a string is uppercase or lowercase using javascript?
- Array filtering using first string letter in JavaScript
- Print first letter of each word in a string in C#
- Program to make vowels in string uppercase and change letters to next letter in alphabet (i.e. z->a) in JavaScript
- Capitalize last letter and Lowercase first letter of a word in Java
