Problem Statement:
You are given a string, you need to find the first upper case letter using recursion.
Example:
Input: proDeveloperTutorial
Output: D
Solution Explanation:
We recursively traverse the string, and if any uppercase is found then return the character.
Time Complexity: O(N)
Space Complexity: O(N) for stack call
Code Solution
#include <iostream>
#include <stack>
using namespace std;
char firstUpperChar(string str, int i)
{
if (str[i] == '\0')
return 0;
if (isupper(str[i]))
return str[i];
return firstUpperChar(str, i+1);
}
int main()
{
string str = "proDeveloperTutorial";
char result = firstUpperChar(str, 0);
if (result == 0)
cout << "No uppercase letter";
else
cout << result << "\n";
return 0;
}
Output
D