!!!Need this to be done twice in one program, once using the #include<string> an
ID: 3622266 • Letter: #
Question
!!!Need this to be done twice in one program, once using the #include<string> and once using #include<cstring>
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
#include
#include
#include
#include
using namespace std;
int main()
{
//string library
string name,firstname,lastname,fullname;
int pos;
cout<<"string lib ";
cout<<"Enter your name (LastName,FirstName): ";
getline(cin,name);
pos=name.find(',');
lastname=name.substr(0,pos);
lastname[0]=toupper(lastname[0]);
cout<<"You lastname is "<
firstname=name.substr(pos+1);
while(firstname[0]==' ')firstname=firstname.erase(0,1); //remove space character
firstname[0]=toupper(firstname[0]);
cout<<"You firstname is "<
fullname = firstname + ' '+ lastname;
cout<<"Your fullname is "<
//cstring library
printf("cstring lib ");
char str[255], firstN[255], lastN[255], fullN[255];
char *p2;
int i,n;
printf("Enter your name (LastName,FirstName): ");
fflush(stdin);
gets(str);
p2=strchr(str,',');
strncpy(lastN,str,p2-str);
lastN[p2-str]='';
lastN[0]=toupper(lastN[0]);
printf("You lastname is %s and it contains %d characters ",lastN,strlen(lastN));
n = p2-str+1;
while(str[n]==' ') n++;
for(i=n;i firstN[i-n]=str[i];
firstN[i-n]='';
firstN[0]=toupper(firstN[0]);
printf("You firstname is %s and it contains %d characters ",firstN,strlen(firstN));
strcat(fullN,firstN);
strcat(fullN," ");
strcat(fullN,lastN);
printf("You fullname is %s ",fullN);
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.