Write a program in C++.Prompt the user for his/her first name. Output the user\'
ID: 3834734 • Letter: W
Question
Write a program in C++.Prompt the user for his/her first name. Output the user's nickname. Use this algorithm to generate a nickname:
If the first letter in the user's name is odd according to the ASCII chart, output "Big" followed by the first three letters of the name.
If the first letter in the user's name is even according to the ASCII chart, output "Lil" followed by the first four letters of the name.
Required output is below:
Test Case 2
Test Case 3
Test Case 4
Test Case 5
Test Case 6
Test Case 7
Test Case 8
Test Case 9
Test Case 10
Test Case 11
Standard InputAndrew
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s, name;
int asciiVal;
int flag=0;
cout<<"enter your first name: "<<endl; // enter your first name
cin>>name; // input first name
asciiVal=(int)name[0]; //getting 1 char ascii value of first name
// cout << asciiVal << endl;
// checking whether the string inputted from user contains alphbets or not
for(int i=0;i<name.size();i++){
if( (name[i]>='a' && name[i]<='z') && (name[i]>='A' && name[i]<='Z')){ //checking the ascii value of each char
flag=0; // if string contains only alphbets then flag as 0
break;
}else{
flag=1; // if string does not contains only alphbets then flag as 1
break;
}
}
if(flag==0){ // if flag value is 0 then i contains only alphabets
if(asciiVal%2==0){ //checking ascii value of first char is even or odd
s = "Lil "+ name.substr(0,4); // if even the print Lil and first 4 char from string
}else {
s= "Big "+ name.substr(0, 3); // if odd the print Big and first 3 char from string
}
cout<<"Your nick name is: "<<endl<< s; //printing the string
}else{
cout<<"Only letters are allowed."<<endl; // if it doesnot contains char then print only letters are allowed
}
}
here is the whole program
i have included comments on each and every line of code, hope u got your answer.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.