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>
usingnamespacestd;
// Function to find string which has
// first character of each word.
charfirst(string str)
{
for(inti = 0; i < str.length(); i++)
if(isupper(str[i]))
returnstr[i];
return0;
}
// Driver code
intmain()
{
string str = "geeksforGeeKS";
charres = first(str);
if(res == 0)
cout << "No uppercase letter";
else
cout << res << "\n";
return0;
}
Java
// Java program to find the first
// uppercase letter using linear search
importjava.io.*;
importjava.util.*;
classGFG {
// Function to find string which has
// first character of each word.
staticcharfirst(String str)
{
for(inti = 0; i < str.length(); i++)
if(Character.isUpperCase(str.charAt(i)))
returnstr.charAt(i);
return0;
}
// Driver program
publicstaticvoidmain(String args[])
{
String str = "geeksforGeeKS";
charres = 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.
deffirst(str) :
fori inrange(0, len(str)) :
if(str[i].istitle()) :
returnstr[i]
return0
# Driver code
str="geeksforGeeKS"
res =first(str)
if(res ==0) :
print("No uppercase letter")
else:
print(res)
# This code is contributed by Nikita Tiwari
C#
// C# program to find the first uppercase
// letter using linear search
usingSystem;
classGFG {
// Function to find string which has
// first character of each word.
staticcharfirst(stringstr)
{
for(inti = 0; i < str.Length; i++)
if(char.IsUpper(str[i]) )
returnstr[i];
return'0';
}
// Driver function
publicstaticvoidMain()
{
stringstr = "geeksforGeeKS";
charres = 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.
functionfirst($str)
{
for($i= 0; $i< strlen($str); $i++)
if(ctype_upper($str[$i]))
{
return$str[$i];
}
return0;
}
// 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
?>
Output:
G
Method 2 (Using recursion) Recursively traverse the string and if any uppercase is found return that character
Python3
//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, inti=0)
{
if(str[i] =='\0')
return0;
if(isupper(str[i]))
returnstr[i];
returnfirst(str, i+1);
}
//Driver code
intmain()
{
string str="geeksforGeeKS";
char res =first(str);
if(res ==0)
cout << "No uppercase letter";
else
cout << res << "\n";
return0;
}
Java
// Java program to find the
// first uppercase letter.
importjava.io.*;
classGFG {
// Function to find string which has
// first character of each word.
staticcharfirst(String str, inti)
{
if(str.charAt(i) == '\0')
return0;
if(Character.isUpperCase(str.charAt(i)))
returnstr.charAt(i);
returnfirst(str, i + 1);
}
// Driver code
publicstaticvoidmain(String args[])
{
String str = "geeksforGeeKS";
charres = first(str,0);
if(res == 0)
System.out.println("No uppercase letter");
else
System.out.println (res );
}
}
// This code is contributed
// by Nikita Tiwari.
Python 3
defcapital(N, i, x):
ifi >=x:
return-1
elifN[i].isupper():
returni
ifi < x:
returncapital(N, i +1, x)
defmain():
N =input()
N =list(N)
x =len(N)
y =(capital(N, 0, x))
print(y)
if__name__ =='__main__':
main()
C#
// C# program to find the
// first uppercase letter.
usingSystem;
classGFG
{
// Function to find string
// which has first character
// of each word.
staticcharfirst(stringstr, inti)
{
if(str[i] == '\0')
return'0';
if(char.IsUpper(str[i]))
return(str[i]);
returnfirst(str, i + 1);
}
// Driver code
staticpublicvoidMain ()
{
stringstr = "geeksforGeeKS";
charres = 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.
functionfirst($str, $i= 0)
{
if($str[$i] == '\0')
return0;
if(ctype_upper($str[$i]))
return$str[$i];
returnfirst($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
?>
Output :
G
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy