Write a C program to enter a first name, middle name and last name in separate v
ID: 3761282 • Letter: W
Question
Write a C program to enter a first name, middle name and last name in separate variables. After entering each name at the keyboard, call a function named convert that will capitalize the first letter of the name and set the rest of the name to lowercase (for example, jOhN becomes John). Combine the properly capitalized names into one string and output the string (make sure you have a space between each part of the name). Count the number of characters in the total name (including spaces between names) and output that total also. This code is C++ and is an introduction to programming.Explanation / Answer
#include <iostream>
#include <string.h>
#include <sstream>
using namespace std;
int main(){
string first,last,middle;
cout << "Enter first name : ";
cin >> first;
cout << "Enter last name : ";
cin >> last;
cout << "Enter middle name : ";
cin >> middle;
int i = 0;
int temp;
while(first[i])
{
temp = first[i];
if(i==0)
{
if(temp>=97&&temp<=122)
temp = temp-32;
first[i] = temp;
}
else
{
if(temp>=65&&temp<=90)
temp = temp+32;
first[i] = temp;
}
i++;
}
i = 0;
while(middle[i])
{
temp = middle[i];
if(i==0)
{
if(temp>=97&&temp<=122)
temp = temp-32;
middle[i] = temp;
}
else
{
if(temp>=65&&temp<=90)
temp = temp+32;
middle[i] = temp;
}
i++;
}
i = 0;
while(last[i])
{
temp = last[i];
if(i==0)
{
if(temp>=97&&temp<=122)
temp = temp-32;
last[i] = temp;
}
else
{
if(temp>=65&&temp<=90)
temp = temp+32;
last[i] = temp;
}
i++;
}
stringstream ss;
ss << first<<" "<<middle<<" "<<last;
string fullName = ss.str();
cout << fullName << endl;
cout << "length of string : " << fullName.length() << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.