!!! must be done using #include<cstring>, using char[] Write a program that 1. r
ID: 3622217 • Letter: #
Question
!!! must be done using #include<cstring>, using char[]
Write a program that
1. reads in a single string entered from the keyboard in the form last_name,first_name with no embedded blanks. Note that a comma separates the two pieces. Have it store this input as a single string (NOTE THAT YOU DON’T HAVE TO READ THE STRING IN 1 CHAR AT A TIME,, BUT SHOULD READ IT IN ALL AT ONCE!)
2. NOW, break the string up and store the first_name and last_name separately as 2 separate strings.called FIRST and LAST (Have it capitalize the first letter of each if they are not already capitalized)
3. Have it print out each together with the number of characters in each.
4. Now have your program create a new string called THE_NAME that looks like first_name last_name (Note the blank between the first and last names) Have your program print out THE_NAME
Sample run (This should be printed twice):
Enter your name in the form last_name,first_name
doe,John
Your first name is John and it contains 4 characters
Your last name is Doe and it contains 3 characters
Your full name is John Doe
Explanation / Answer
i guess your last question wasnt exactly clear on what u needed to happen haha, anyway i revamped your old code again (luckily i had it saved since last time) and here it is again using cstrings, hope this helps
#include
#include //needed to captalize characters
#include //for a list of function & defintions for cstring go to: http://www.cplusplus.com/reference/clibrary/cstring/
using namespace std;
int main()
{
const int size = 30;//in C++ whenever a cstring/character array is declared you must give it a size.
//we gave it a size of 30 so that it could handle just about whatever we throw at it.
char input[size], FIRST[size], LAST[size], THE_NAME[size];
int comma,len,counter;
cout<<"Enter last name, first name with no blanks (comma separates the two names): ";
cin>>input;
len=strlen(input);
//now that we know how long the string is we need to set the null operator for input and THE_NAME
input[len]='';
THE_NAME[len]='';
for(int i = 0; i
if(input[i]==',')comma = i;
counter = comma;//this is used to copy the values after comma in the next for loop
for(int i = 0;i<(len-comma); i++)//using a forloop we can copy all the characters after the comma to FIRST
{
if(i==0) FIRST[i] = toupper(FIRST[i]);//uppercase this incase it isnt already
counter++;
FIRST[i]=input[counter];
}
FIRST[(strlen(FIRST)+1)] = '';//set null operator for FIRST right after the last character entered
for(int i = 0;i<=comma;i++)//using a forloop we can copy all the characters before the comma to LAST
{
LAST[i]=input[i];
if(i==0)LAST[i] = toupper(LAST[i]);//uppercase this incase it isnt already
if(i==comma)LAST[i]= '';//set null operator for LAST
}
cout<<"Your first name is "<<<" and it contains "<< strlen(FIRST)<<" Characters"<
cout<<"Your last name is "<<<" and it contains "<< strlen(LAST)<<" Characters"<
counter = 0;//reset counter
for(int i = 0; i
{
if(i
THE_NAME[i]=FIRST[i];
else if(i==comma-1)
THE_NAME[i] =' ';
else
{
THE_NAME[i]=LAST[counter];
counter++;
}
}
cout<<"Your full name is: "<<
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.