First uppercase letter in a string (Iterative and Recursive)
Last Updated :
09 Dec, 2022
Given a string find its first uppercase letter
Examples :
Input : geeksforgeeKs
Output : K
Input : geekS
Output : S
Method 1: linear search
Using linear search, find the first character which is capital
C++
// C++ program to find the first
// uppercase letter using linear search
#include <bits/stdc++.h>
using namespace std;
// Function to find string which has
// first character of each word.
char first(string str)
{
for (int i = 0; i < str.length(); i++)
if (isupper(str[i]))
return str[i];
return 0;
}
// Driver code
int main()
{
string str = "geeksforGeeKS";
char res = first(str);
if (res == 0)
cout << "No uppercase letter";
else
cout << res << "\n";
return 0;
}
Java
// Java program to find the first
// uppercase letter using linear search
import java.io.*;
import java.util.*;
class GFG {
// Function to find string which has
// first character of each word.
static char first(String str)
{
for (int i = 0; i < str.length(); i++)
if (Character.isUpperCase(str.charAt(i)))
return str.charAt(i);
return 0;
}
// Driver program
public static void main(String args[])
{
String str = "geeksforGeeKS";
char res = first(str);
if (res == 0)
System.out.println("No uppercase letter");
else
System.out.println(res);
}
}
// This code is contributed
// by Nikita Tiwari.
Python3
# Python3 program to find the first
# uppercase letter using linear search
# Function to find string which has
# first character of each word.
def first(word) :
for i in range(0, len(word)) :
if (word[i].isupper()) :
return word[i]
return "No uppercase letter :("
# Driver code
word = "geeksforgeeS"
print(first(word))
# This code is contributed by Anshul Suresh
C#
// C# program to find the first uppercase
// letter using linear search
using System;
class GFG {
// Function to find string which has
// first character of each word.
static char first(string str)
{
for (int i = 0; i < str.Length; i++)
if (char.IsUpper(str[i]) )
return str[i];
return '0';
}
// Driver function
public static void Main()
{
string str = "geeksforGeeKS";
char res = first(str);
if (res == '0')
Console.WriteLine("No uppercase"
+ " letter");
else
Console.WriteLine(res);
}
}
// This code is contributed by Sam007
PHP
<?php
// PHP program to find the first
// uppercase letter using linear search
// Function to find string which has
// first character of each word.
function first($str)
{
for ($i = 0; $i < strlen($str); $i++)
if (ctype_upper($str[$i]))
{
return $str[$i];
}
return 0;
}
// Driver code
$str = "geeksforGeeKS";
$res = first($str);
if (ord($res) ==ord(0) )
echo "No uppercase letter";
else
echo $res . "\n";
// This code is contributed by Sam007
?>
JavaScript
<script>
// JavaScript program to find the first
// uppercase letter using linear search
// Function to find string which has
// first character of each word.
function first(str) {
for (var i = 0; i < str.length; i++)
if (str[i] === str[i].toUpperCase()) return str[i];
return 0;
}
// Driver code
var str = "geeksforGeeKS";
var res = first(str);
if (res == 0) document.write("No uppercase letter");
else {
document.write(res);
document.write("<br>");
}
// This code is contributed by rdtank.
</script>
Output:
G
Time Complexity : O(N)
Auxiliary Space: O(1)
Method 2 (Using recursion)
Recursively traverse the string and if any uppercase is found return that character
C++
// C++ program to find the
// first uppercase letter.
#include <bits/stdc++.h>
using namespace std;
// Function to find string which has
// first character of each word.
char first(string str, int i=0)
{
if (str[i] == '\0')
return 0;
if (isupper(str[i]))
return str[i];
return first(str, i+1);
}
// Driver code
int main()
{
string str = "geeksforGeeKS";
char res = first(str);
if (res == 0)
cout << "No uppercase letter";
else
cout << res << "\n";
return 0;
}
Java
// Java program to find the
// first uppercase letter.
import java.io.*;
class GFG {
// Function to find string which has
// first character of each word.
static char first(String str, int i)
{
if(str.charAt(i)=='\0'){
return 0;
}
if(Character.isUpperCase(str.charAt(i))) {
return str.charAt(i);
}
try {
return first(str, i + 1);
}
catch(Exception e){
System.out.println("Exception occurs");
}
return 0;
}
// Driver code
public static void main(String args[])
{
String str = "geeksforGeeKS";
char res = first(str,0);
if (res == 0)
System.out.println("No uppercase letter");
else
System.out.println (res );
}
}
// This code is contributed
// by Shravan Sutar(suthar826)
Python 3
def firstUpperCase(word,index):
""""Recursively traverse from start of string to end,
If character is upper return it.
"""
if index==len(word):
#if no upperCase letter found in string return "No UpperCase letter found."
return "No UpperCase letter found."
if word[index].isupper():
return word[n]
return firstUpperCase(word,index+1)
word="geeksforGeekS"
print(firstUpperCase(word,0))
C#
// C# program to find the
// first uppercase letter.
using System;
class GFG
{
// Function to find string
// which has first character
// of each word.
static char first(string str, int i)
{
if (str[i] == '\0')
return '0';
if (char.IsUpper(str[i]))
return (str[i]);
return first(str, i + 1);
}
// Driver code
static public void Main ()
{
string str = "geeksforGeeKS";
char res = first(str, 0);
if (res == 0)
Console.WriteLine("No uppercase letter");
else
Console.WriteLine(res );
}
}
// This code is contributed by Anuj_67.
PHP
<?php
//PHP program to find the
// first uppercase letter.
// Function to find string
// which has first character
// of each word.
function first($str, $i = 0)
{
if ($str[$i] == '\0')
return 0;
if (ctype_upper($str[$i]))
return $str[$i];
return first($str, $i+1);
}
// Driver code
$str = "geeksforGeeKS";
$res = first($str);
if (ord($res) ==ord(0))
echo "No uppercase letter";
else
echo $res , "\n";
// This code is contributed
// by m_kit
?>
JavaScript
<script>
// Javascript program to find the
// first uppercase letter.
// Function to find string which has
// first character of each word.
function first(str,i=0)
{
if (i == str.length)
return 0;
if (str[i] == str[i].toUpperCase())
return str[i];
return first(str, i+1);
}
// Driver code
var str = "geeksforGeeKS";
var res = first(str);
if (res == 0)
document.write( "No uppercase letter");
else
document.write( res + "<br>");
</script>
Output :
G
Time Complexity : O(N)
Auxiliary Space: O(N) for call stack
Similar Reads
Count consonants in a string (Iterative and recursive methods) Given a string, count total number of consonants in it. A consonant is an English alphabet character that is not vowel (a, e, i, o and u). Examples of constants are b, c, d, f, and g. Examples : Input : abc de Output : 3 There are three consonants b, c and d. Input : geeksforgeeks portal Output : 12
7 min read
Minimum length substring with each letter occurring both in uppercase and lowercase Find the minimum length substring in the given string str such that, in the substring, each alphabet appears at least once in lower case and at least once in upper case. Examples: Input: S = âAaBbCcâOutput: AaExplanation: Possible substrings that has each alphabet in lowercase and uppercase are:AaBb
14 min read
Check string for all uppercase-lowercase or first uppercase and rest lowercase characters Given a string S, the task is to check if all the characters in S are in lowercase, or all the characters are in uppercase or only the first character is in uppercase, and the remaining characters are in lowercase. Print "YES" if any of the condition gets satisfied else print "NO". Examples: Input:
6 min read
Remove Repeating chars and Reverse String until no Repetitions Given a string S which consists of only lowercase English alphabets, the task is to remove the first repeating character, reverse it, and repeat until there are no repeating characters. Return the final string. Examples: Input: S = "abab"Output: baExplanation: In 1st operation: The first non repeati
10 min read
Check if uppercase characters (Capital letters) in a string are used correctly or not | Set 2 Given a string S consisting of uppercase and lowercase letters, the task is to check if uppercase characters are used correctly in the given string or not. Correct usage of uppercase characters are as follows: All characters in the string are in uppercase. For example, âGEEKSâ.None of the characters
5 min read