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++
#include <bits/stdc++.h>
using namespace std;
char first(string str)
{
for ( int i = 0; i < str.length(); i++)
if ( isupper (str[i]))
return str[i];
return 0;
}
int main()
{
string str = "geeksforGeeKS" ;
char res = first(str);
if (res == 0)
cout << "No uppercase letter" ;
else
cout << res << "\n" ;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
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 ;
}
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);
}
}
|
Python3
def first(word) :
for i in range ( 0 , len (word)) :
if (word[i].isupper()) :
return word[i]
return "No uppercase letter :("
word = "geeksforgeeS"
print (first(word))
|
C#
using System;
class GFG {
static char first( string str)
{
for ( int i = 0; i < str.Length; i++)
if ( char .IsUpper(str[i]) )
return str[i];
return '0' ;
}
public static void Main()
{
string str = "geeksforGeeKS" ;
char res = first(str);
if (res == '0' )
Console.WriteLine( "No uppercase"
+ " letter" );
else
Console.WriteLine(res);
}
}
|
PHP
<?php
function first( $str )
{
for ( $i = 0; $i < strlen ( $str ); $i ++)
if (ctype_upper( $str [ $i ]))
{
return $str [ $i ];
}
return 0;
}
$str = "geeksforGeeKS" ;
$res = first( $str );
if (ord( $res ) ==ord(0) )
echo "No uppercase letter" ;
else
echo $res . "\n" ;
?>
|
Javascript
<script>
function first(str) {
for ( var i = 0; i < str.length; i++)
if (str[i] === str[i].toUpperCase()) return str[i];
return 0;
}
var str = "geeksforGeeKS" ;
var res = first(str);
if (res == 0) document.write( "No uppercase letter" );
else {
document.write(res);
document.write( "<br>" );
}
</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++
#include <bits/stdc++.h>
using namespace std;
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);
}
int main()
{
string str = "geeksforGeeKS" ;
char res = first(str);
if (res == 0)
cout << "No uppercase letter" ;
else
cout << res << "\n" ;
return 0;
}
|
Java
import java.io.*;
class GFG {
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 ;
}
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 );
}
}
|
Python 3
def firstUpperCase(word,index):
if index = = len (word):
return "No UpperCase letter found."
if word[index].isupper():
return word[n]
return firstUpperCase(word,index + 1 )
word = "geeksforGeekS"
print (firstUpperCase(word, 0 ))
|
C#
using System;
class GFG
{
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);
}
static public void Main ()
{
string str = "geeksforGeeKS" ;
char res = first(str, 0);
if (res == 0)
Console.WriteLine( "No uppercase letter" );
else
Console.WriteLine(res );
}
}
|
PHP
<?php
function first( $str , $i = 0)
{
if ( $str [ $i ] == '\0' )
return 0;
if (ctype_upper( $str [ $i ]))
return $str [ $i ];
return first( $str , $i +1);
}
$str = "geeksforGeeKS" ;
$res = first( $str );
if (ord( $res ) ==ord(0))
echo "No uppercase letter" ;
else
echo $res , "\n" ;
?>
|
Javascript
<script>
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);
}
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
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
09 Dec, 2022
Like Article
Save Article