First uppercase letter in a string (Iterative and Recursive) - GeeksforGeeks

12 Jan, 2022
Given a string find its first uppercase letterExamples : 
Input : geeksforgeeKs
Output : K

Input  : geekS
Output : S
Method 1: linear search Using linear search, find the first character which is capital  
C++
Java
Python3
C#
PHP
Javascript
// 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 codeint main(){    string str = "geeksforGeeKS";    char res = first(str);    if (res == 0)        cout << "No uppercase letter";    else        cout << res << "\n";    return 0;}
// Java program to find the first// uppercase letter using linear searchimport 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 program to find the first# uppercase letter using linear search # Function to find string which has# first character of each word.def first(str) :         for i in range(0, len(str)) :                 if (str[i].istitle()) :            return str[i]                 return 0  # Driver codestr = "geeksforGeeKS"res = first(str) if (res == 0) :    print("No uppercase letter")     else :    print(res)          # This code is contributed by Nikita Tiwari
// C# program to find the first uppercase// letter using linear searchusing 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 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?>
<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
Method 2 (Using recursion) Recursively traverse the string and if any uppercase is found return that character 
C++
Java
Python 3
C#
PHP
Javascript
// 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 codeint main(){    string str = "geeksforGeeKS";    char res = first(str);    if (res == 0)        cout << "No uppercase letter";    else        cout << res << "\n";    return 0;}
// 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 occures");        }        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)
def capital(N, i, x):    if i >= x:        return -1    elif N[i].isupper():        return i    if i < x:        return capital(N, i + 1, x)  def main():    N = input()    N = list(N)    x = len(N)    y = (capital(N, 0, x))    print(y)  if __name__ == '__main__':    main()
// 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 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?>
<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 codevar str = "geeksforGeeKS";var res = first(str);if (res == 0)    document.write( "No uppercase letter");else    document.write( res + "<br>");  </script>
Output : 
G 
Sam007
vt_m
jit_t
Smitha Dinesh Semwal
harshitshukla2
rdtank
noob2000
suthar826
Recursion
Strings
Strings
Recursion
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Backtracking | Introduction
Print all possible combinations of r elements in a given array of size n
Recursive Practice Problems with Solutions
Write a program to reverse digits of a number
Recursive Functions
Write a program to reverse an array or string
Reverse a string in Java
Longest Common Subsequence | DP-4
C++ Data Types
Check for Balanced Brackets in an expression (well-formedness) using Stack