First uppercase letter in a string (Iterative and Recursive)
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; } |
chevron_right
filter_none
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. |
chevron_right
filter_none
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(str) : for i in range(0, len(str)) : if (str[i].istitle()) : return str[i] return 0 # Driver code str = "geeksforGeeKS"res = first(str) if (res == 0) : print("No uppercase letter") else : print(res) # This code is contributed by Nikita Tiwari |
chevron_right
filter_none
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 |
chevron_right
filter_none
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 ?> |
chevron_right
filter_none
Output:
G
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; } |
chevron_right
filter_none
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); return first(str, i + 1); } // 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 Nikita Tiwari. |
chevron_right
filter_none
Python 3
# Python 3 program to find the # first uppercase letter. #include <bits/stdc++.h> # Function to find string which has # first character of each word. def first(str, i): if (str[i] == '\0'): return 0 if (str[i].isupper()): return str[i] return first(str, i+1) # Driver code str = "geeksforGeeKS"res = first(str,0) if (res == 0): print("No uppercase letter") else: print(res) # This code is contributed # by Smitha |
chevron_right
filter_none
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. |
chevron_right
filter_none
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 ?> |
chevron_right
filter_none
Output :
G
Recommended Posts:
- Function to copy string (Iterative and Recursive)
- Program to reverse a string (Iterative and Recursive)
- Count consonants in a string (Iterative and recursive methods)
- Program to count vowels in a string (Iterative and Recursive)
- Check if linked list is sorted (Iterative and Recursive)
- String containing first letter of every word in a given string with spaces
- Count of words whose i-th letter is either (i-1)-th, i-th, or (i+1)-th letter of given word
- Get the first letter of each word in a string using regex in Java
- Check if the string contains consecutive letters and each letter occurs exactly once
- Number of positions where a letter can be inserted such that a string becomes palindrome
- Get K-th letter of the decoded string formed by repeating substrings
- String slicing in Python to check if a string can become empty by recursive deletion
- Recursive program to replace all occurrences of pi with 3.14 in a given string
- Recursive function to check if a string is palindrome
- Print all subsequences of a string | Iterative Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



