Answer to Question #178758 in C++ for Luna

Question #178758

Write a program that reads a person’s name in the following format: first name, then middle name or initial, and then last name. The program then outputs the name in the following format:

lastName, firstName middleInitial.


For example, the input

Mary Average User

Should produce the output:

User, Mary A.

The input

Mary A. User

Should also produce the output:

User, Mary A.


Your program should work the same and place a period after the middle initial even if the input did not contain a period. Your program should allow for users who give no middle name or middle initial. In that case, the output, of course, contains no middle or initial. For example, the input

May User

should produce the output

User, Mary

(Hint: You may want to use three string variables rather than one large string variable for the input. You may find it easier to not use getline.)



1
Expert's answer
2021-04-07T00:19:07-0400
#include<iostream>
#include<conio.h>
using namespace std;
class String
{
public:
    char a[30],b[30];
    int compare(char a[], char b[])
    {
        int i=0,j=0;
        while(a[i]!='\0')
        {
            i=i+1;
        }
        while(b[j]!='\0')
        {
            j=j+1;
        }
        if(i==j)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
};
int main()
{
    int k;
    String a1,a2;
    cout<<"Enter first string ";
    cin>>a1.a;
    cout<<"\nEnter second string ";
    cin>>a2.b;
    k=a1.compare(a1.a,a2.b);
    if(k==1)
    {
        cout<<"\nYes, the strings are of the same length";
    }
    else{
        cout<<"\nNo, the strings are not of the same length";
    }
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS