Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a complete C++ program that asks the user for a color, and then, using the

ID: 3546787 • Letter: W

Question

Write a complete C++ program that asks the user for a color, and then, using the functions described below, (which you are to implement) displays a message saying "Nice colour" if the colour is red or "Not red" if the entered colour is not red, displays the number of letters in the colour entered by the user and displays the last letter of the colour entered by the user.


Make sure that the output generated by your program is clear for the user by using descriptive messages; in other words don't just display a number or a letter, but include a descriptive message with the number or letter.


Functions definitions (you are not allowed to change the function definitions):

A non-void function called isRed(), which returns the boolean true if the passed string is the string red and false otherwise.

A non-void function called numberOfLetters(), which returns the number of letters in the passed colour.

A void function called lastLetter(), which returns in a second parameter the last letter of the first parameter.

Explanation / Answer

#include<iostream>
#include <algorithm>
#include <string>
using namespace std;

bool isRed(string color) //which returns the boolean true if the passed string is the string red and false otherwise.
{
    string str = color;
transform(str.begin(), str.end(),str.begin(), ::toupper);
return str.compare("RED")==0;
}
int numberOfLetters(string color) // which returns the number of letters in the passed colour.
{
return color.length();
}
void lastLetter(string color,char& last_letter) // which returns in a second parameter the last letter of the first parameter
{
last_letter = color[color.length()-1];
}
int main()
{
string color;
cout <<"Enter name of color :";
cin >> color;
cout <<endl;
char last_letter;
if(isRed(color))
cout <<"Nice colour" << endl;
else
cout <<"Not Red" << endl;
cout << "Number of letters in colour "<< color << " is " << numberOfLetters(color) << endl;
lastLetter(color,last_letter);
cout << "Last character in colour" << color << " is " << last_letter << endl;
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote