Question

Create a program in C++ that has only an integer type input called in. This program...

Create a program in C++ that has only an integer type input called in. This program interchanges two characters’ positions based on user’s input value. The specifications are:

If user enters 0, first and second characters are swapped.

If user enters 1, first and last characters are swapped.

If users enter 2, second and last characters are swapped.

For example:

The original text is “ABC”.

First letter is A, second letter is B and last letter is C.

If 0 is entered, out = BAC.            // first and second characters are swapped

if 1 is entered, out = CBA;             // first and last characters are swapped

if 2 is entered, out = ACB.            // second and last characters are swapped

Write a program that accomplishes above software specifications.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>

using namespace std;

int main()

{

    string str;

   

    while(1)

    {

        cout<<"Enter the string : ";

        cin>>str;

       

        // if length of strings is more than 2

        if(str.length() >= 3)

            break;

          

        cout<<"Error! Length of string should be more than 2\n";

    }

   

    cout<<"Press\n0, first and second characters are swapped.\n1, first and last characters are swapped.\n2, second and last characters are swapped.\n";

   

    int option;

    cin>>option;

   

    if(option == 0)

    {

        // swap elements

        char temp = str[0];

        str[0] = str[1];

        str[1] = temp;

    }

    else if(option == 1)

    {

        // swap elements

        char temp = str[0];

        str[0] = str[str.length() - 1];

        str[str.length() - 1] = temp;

    }

    else if(option == 2)

    {

        // swap elements

        char temp = str[1];

        str[1] = str[str.length() - 1];

        str[str.length() - 1] = temp;

    }

   

    cout<<"String : "<<str;

   

    return 0;

}

Sample Output:

nter the string ABC Press first and second characters are swapped 1. first and last characters are swapped 2. second and last

Add a comment
Know the answer?
Add Answer to:
Create a program in C++ that has only an integer type input called in. This program...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is Python The program should accept input from the user as either 7-digit phone number...

    This is Python The program should accept input from the user as either 7-digit phone number or 10-digit. If the user enters 7 characters, the program should automatically add "512" to the beginning of the phone number as the default area code. Dash (hyphen) characters do not count in input character count, but must not be random. If the user enters dashes the total character count must not exceed 12. The program should not crash if the user enters invalid...

  • In the Source Folder (src) of this project create a new C source file called "gcd.c"....

    In the Source Folder (src) of this project create a new C source file called "gcd.c". Once again, copy the contents of the "main.c" file above into the "gcd.c" source file. Modify this program to NOT ask the user to input the second Positive Integer, if the user has entered a program terminating value for the "first" input Postitive Integer. #include <stdlib.h> #include <stdio.h> int main(int argc, char *argv[]) { //============================== setbuf(stdout, NULL); // turns standard output buffering off int...

  • HI CAN YOU PLEASE PROGRAM THIS ASSIGNMENT USING C++ Ask the user to input a string. Then sort the string in reverse lex...

    HI CAN YOU PLEASE PROGRAM THIS ASSIGNMENT USING C++ Ask the user to input a string. Then sort the string in reverse lexicographic order and print it out. Reverse lexicographic means reverse alphabetic, e.g. zyxwvuts...edcba. This would be done using the integer values of the characters (ASCIl table). Use any one of the sorting methods we saw in class. For example: if the user enters "zeta" sorting it in reverse lexicographic order would give "ztea. if the user enters "ZETA"...

  • Part I Create a Java program that inputs a grade from the user. The grade input...

    Part I Create a Java program that inputs a grade from the user. The grade input from the user will be an integer. Once the input is stored, use an if-else-if block of code to determine the letter grade of the inputted integer grade. Do not use a bunch of if statements by themselves to solve this problem. You will print an error message for inputs greater than 100 and for inputs less than 0. Both errors must be handled...

  • Wrote a program called ExceptionalDivide that asks the user for 2 integer values and displays the...

    Wrote a program called ExceptionalDivide that asks the user for 2 integer values and displays the quotient of the first value divided by the second value. Make sure the your program reads in the two input values as ints. Your program must catch either of two exceptions that might arise and crash your program java.util.InputMismatchException //wrong data type inputted java.lang.ArithmeticException //divide by zero error Once you catch these exceptions, your program must print out a message explaining what happened and...

  • In this program, you will be using C++ programming constructs, such as overloaded functions. Write a...

    In this program, you will be using C++ programming constructs, such as overloaded functions. Write a program that requests 3 integers from the user and displays the largest one entered. Your program will then request 3 characters from the user and display the largest character entered. If the user enters a non-alphabetic character, your program will display an error message. You must fill in the body of main() to prompt the user for input, and call the overloaded function showBiggest()...

  • the coding language is just the basic c language and here is my first attempt at...

    the coding language is just the basic c language and here is my first attempt at this problem my problem is that if the user inputs a number that is equal to a number that has been entered previously the code will never end until the user enters a number that is bigger than any other number previously entered any help will be helpful Write a program to read-in a sequence of integers from the keyboard using scanf(). Your program...

  • In Language = C Concepts for this are (malloc/realloc, structs, and stdin/fgets/getc/scanf) Description Your program will...

    In Language = C Concepts for this are (malloc/realloc, structs, and stdin/fgets/getc/scanf) Description Your program will ask the user to type in the following information: First Name: Last Name: ID#: Email: The program will terminate when a single . is entered as a First Name. This information will be stored in the following structure type: struct student { int recordCount; char *firstName; char *lastName; char *id; char *email; } recordCount is a number assigned at the beginning of the program...

  • This program will require you to create a basic parser for an input string that is...

    This program will require you to create a basic parser for an input string that is somewhat like Python. Python has an older parser module and a newer ast module to manage pure Python syntax, but they won't be useful here. The program will read a data string, identify the control characters, and print out the overall structure of the string. Here's an example Input String: [{1}] Output: Number inside a dictionary inside a list Here are some basic rules...

  • Java programming only Create a Java program that inputs a grade from the user. The grade input from the user will be an...

    Java programming only Create a Java program that inputs a grade from the user. The grade input from the user will be an integer. Once the input is stored, use an if-else-if block of code to determine the letter grade of the inputted integer grade. Do not use a bunch of if statements by themselves to solve this problem. You will print an error message for inputs greater than 100 and for inputs less than 0. Both errors must be...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT