Q:

C++ program to find and print first uppercase character of a string

belongs to collection: C++ programs on various topics

0

Given a string and we have to print its first uppercase character (if exists) using C++ program.

Example:

    Input string: "hello world, how are you?"
    Output:
    No uppercase character found in string.

    Input string: "Hello World"
    Output:
    First uppercase character is: "H"

    Input string: "hello worlD"
    Output:
    First uppercase character is: "D"

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to print first uppercase character of a string in C++

#include <bits/stdc++.h>
using namespace std;

//function to return first uppercase
//character from given string
char getFirstUppercaseCharacter(string str)
{
	//loop that will check uppercase character
	//from index 0 to str.length()
	for(int i=0; i<str.length(); i++)
	{
		//'isupper() function returns true
		//if given character is in uppercase
		if(isupper(str[i]))
			return str[i];
	}
	
	return 0;	
}

//Main function
int main()
{
	//defining two strings
	string str1 = "hello world, how are you?";
	string str2 = "Hello World";
	
	//first string check 
	cout<<"Given string: "<<str1<<endl;
	char chr = getFirstUppercaseCharacter(str1);
	if(chr)
		cout<<"First uppercase character is: "<<chr<<endl;
	else
		cout<<"No uppercase character found in string"<<endl;
	
	cout<<endl;

	//second string check 
	cout<<"Given string: "<<str1<<endl;
	chr = getFirstUppercaseCharacter(str2);
	if(chr)
		cout<<"First uppercase character is: "<<chr<<endl;
	else
		cout<<"No uppercase character found in string"<<endl;	
	
	return 0;
}

Output

 
Given string: hello world, how are you?
No uppercase character found in string

Given string: hello world, how are you?
First uppercase character is: H

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ programs on various topics

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ program to declare, read and print dynamic int... >>
<< Print character through ASCII value using cout in ...