Question

In: Computer Science

Using string functions and all other programming features discussed in the class, write a program that...

Using string functions and all other programming features discussed in the class, write a program that will prompt a user to input their name (first and last). Ex: Please enter your first and last name: John Doe Then, output the string. Next, prompt the user to input their nickname. Ex: Enter your nickname: Rowdy Then modify the name string to consist of the person’s last name comma, nickname (backwards, in all caps, enclosed in double quotes) and first name. Then output the modified string. Ex: Doe, “YDWOR” John Bonus (+10) Output first and last name alternating every two characters: John Doe Ex: JoDohne Tim Wheaterton Ex: TiWhmeaterton NOTE: This program should loop, prompting the user to decide whether or not he or she wishes to enter another name. Ex: Do you wish to enter another name(y/n)?

comment program must have getline and concatenation also in c++

Solutions

Expert Solution

Code:

name.cpp:

#include<iostream>
#include<string>       //to use getline()
#include<algorithm>       //to use reverse()
using namespace std;

int main()
{
   string c="y";       //initialize choice to continue to yes
  
   do
   {
       string name;
       cout<<"Please enter your first and last name: ";
       getline(cin,name);   //take input with space
      
       string first="",last="";   //initialize first and last name to null strings
       int flg=0;                    //flag to indicate end of first name
      
       for(int i=0;i<name.length();i++)   //seperation of first and last name
       {
           if(name[i]==' ')   //when space detected change flag
           {
               flg=1;
               continue;
           }
          
           if(flg==0)       //when flag==0 (no space detected yet) copy characters to first name
           {
               first=first+name[i];
           }
           else           ////when flag==1 (space detected) copy characters to last name
           {
               last=last+name[i];
           }
       }
      
       string nname; //nickname
       cout<<"Enter your nickname: ";
       getline(cin,nname);   //take nickname
      
       reverse(nname.begin(), nname.end());   //reverse nickname
      
       for(int i=0;i<nname.length();i++)   //change case of nickname to UPPER
       {
           if (nname[i]>='a' && nname[i]<='z')
               nname[i] = nname[i] - 32;
       }
      
       name=last+", \""+nname+"\" "+first;       //modify string name as required
       cout<<"Modified String : "<<name<<endl;
      
       string alter="";   //to store alternating characters
       int f=first.length(),i=0;   //initialize counter for first anme and its end
       int l=last.length(),j=0;   //initialize counter for last name and its end
      
       while(i<f||j<l)       //as first name can be small than last name or vice versa hence used || (OR)
       {
           if(i<f)       //to make sure don't copy NULL character '\0'
           {
               alter=alter+first[i];
               i++;
               if(i<f)   //to make sure don't copy NULL character '\0'
               {
                   alter=alter+first[i];
                   i++;
               }
           }
          
           if(j<l)       //to make sure don't copy NULL character '\0'
           {
               alter=alter+last[j];
               j++;
               if(j<l)       //to make sure don't copy NULL character '\0'
               {
                   alter=alter+last[j];
                   j++;
               }
           }
       }
      
       cout<<"Alternating every two character string: "<<alter<<endl;   //Output alternating string
      
       cout<<"Do you wish to enter another name(y/n)? ";   //Ask if you want to continue
       cin>>c;
       cin.ignore();   //used to ignore enter key hitted for next getline()
      
   }while(c=="y");
  
   return 0;
}

Screenshot:

Output:


Related Solutions

For these of string functions, write the code for it in C++ or Python (without using...
For these of string functions, write the code for it in C++ or Python (without using any of thatlanguage's built-in functions) You may assume there is a function to convert Small string into the language string type and a function to convert your language's string type back to Small string type. 1. int [] searchA,ll(string in...str, string sub): returns an array of positions of sub in in...str or an one element array with -1 if sub doesn't exist in in...str
Using the string functions below, write new functions to do the following, and test them in...
Using the string functions below, write new functions to do the following, and test them in your main() function: Determine whether the first or last characters in the string are any of the characters a, b, c, d, or e. Reverse a string Determine whether a string is a palindrome (spelled the same way forward or backward FUNCTIONS REFERENCE: string myString = "hello"; // say we have a string… // … we can call any of the following // string...
(Use the string class to solve the problem) Write a program (in c++) that can be...
(Use the string class to solve the problem) Write a program (in c++) that can be used to train the user to use less sexist language by suggesting alternative versions of sentences given by the user. The program will ask for a sentence, read the sentence into a string variable, and replace all occurrences of masculine pronouns with gender-neutral pronouns. For example, it will replace “he” with “she or he”, and “him” with “her or him”. Be sure to preserve...
Write a C program that calculates a student grade in the C Programming Class. Ask the...
Write a C program that calculates a student grade in the C Programming Class. Ask the user to enter the grades for each one of the assignments completed in class: Quiz #1 - 25 points Quiz #2 - 50 points Quiz #3 - 30 points Project #1 - 100 points Project #2 - 100 points Final Test - 100 points The total of the quizzes count for a 30% of the total grade, the total of the projects counts for...
Only Program in C for this. No other programming language is allowed. Using a function, we...
Only Program in C for this. No other programming language is allowed. Using a function, we will add a range of values of an array. The range is going to be determined by the user. In this example, if you put the following array down as: 1.5 -5.6 8.9 4.6 7.8 995.1 45.1 -5964.2 … and the user tells you to add from the 3rd element to the 6th element, your program is going to need to add the values:...
write C++ program using functions (separate function for each bottom) Write a program to find if...
write C++ program using functions (separate function for each bottom) Write a program to find if a number is large word for two given bottom base - bottom1 and bottom2. You can predict that a number, when converted to any given base shall not exceed 10 digits. . the program should ask from user to enter a number that it should ask to enter the base ranging from 2 to 16 after that it should check if the number is...
Using class, write a program that: Body Mass Index (BMI) calculation - write a program that...
Using class, write a program that: Body Mass Index (BMI) calculation - write a program that takes users' input (weight and Height) and calculates the BMI. If the result is less than 18.5 display "you are underweight", if the result is greater than 18.5 display "you have a normal weight", if the result is greater than 24.9 display "your weight is considered overweight", and the result is greater than 30 display "your weight is considered as obese" (BMI = 703...
I need to write a C++ program that appends "not" into the string without using the...
I need to write a C++ program that appends "not" into the string without using the append method or any standard libraries. It should return the string if there isn't an "is" in it. Examples: is is = is not is not This is me = This is not me What is yellow? = What is not yellow? The sky is pink = The sky is not pink isis = isis What happened to you? = What happened to you?
write a C++ program to CREATE A CLASS EMPLOYEE WITH YOUR CHOICE OF ATTRIBUTES AND FUNCTIONS...
write a C++ program to CREATE A CLASS EMPLOYEE WITH YOUR CHOICE OF ATTRIBUTES AND FUNCTIONS COVERING THE FOLLOWING POINTS: 1) COUNTING NUMBER OF OBJECTS CREATED ( NO OBJECT ARRAY TO BE USED) USING ROLE OF STATIC MEMBER 2) SHOWING THE VALID INVALID STATEMENTS IN CASE OF STATIC MEMBER WITH NON STATIC MEMBER FUNCTION, NON STATIC MEMBERS OF CLASS WITH STATIC MEMBER FUNCTION, BOTH STATIC. SHOW THE ERRORS WHERE STATEMENTS ARE INVALID. 3) CALL OF STATIC MEMBER FUNCTION, DECLARATION OF...
Write in C programming using if and else statements only please!!! Write a program that plays...
Write in C programming using if and else statements only please!!! Write a program that plays the following card game: The user starts out with a pot of $100. At each hand of the game, the dealer and the player are dealt a random number between 1 and 52. The player wins $20 if his/her number is greater than the dealer's number; otherwise they lose $20.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT