Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

ENSE504 Introduction to Computing

Lab 3. Characters, Strings, and If Statement


Lab 3. Characters, and Strings

Lab Objectives:


To gain experience with

•   Using characters and standard C++ strings on the PC

•   Using the Visual Studio program template for these programs.


1. Getting a Character from a String (Visual Studio)


Procedure

Use the Visual Studio program template for this program.

Specification

This program should prompt the user to enter a string. Use the getline function to get the string, so the program gets the entire line of characters typed by the user, including any space characters.

The program should then display the string length.

Then the program should prompt the user to enter the index number of the character to display.   The prompt message should state the minimum (0) and maximum (length – 1) possible character index values for the string.

After the user has entered the required index number, the program should display that character    from the string, and the ASCII code number value of the character. To do this, assign the character (char) to an int variable and display the value of the int variable.

Program Design

•   Display the program title.

•   Define any necessary variables.

•  Get the input values, perform the calculations, and display the answers (in this order).

•   Finally, display the end of program message.

Sample Program Run

User input is shown underlined, but it will not be underlined on the computer screen when you run your program.

STRING CHARACTER

Enter a string:

Example of a string!

String length = 20

Enter an index number from 0 to 19:9

Character 9 = f

ASCII Code = 102

END OF PROGRAM

Other possible results for this string are:

Character 0 = E

ASCII Code = 69

Character 7 =

ASCII Code = 32

Character 19 = !

ASCII Code = 33

Note that character 7 is a space character.

What happens if you:

•  enter a negative index value,

•  enter the length of the string as the index value,

•  enter an index value greater than the length of the string.

•  enter another string that is different from the above example, and show the screenshot of the code output in your report.


2. Swap digits


Write a C++ program to swap the first and last digits of any number.

 

Sample Output:

Input any number: 1239456

The number after swapping the first and last digits are: 6239451                                •  show the screenshot of the code output for three different numbers in your report.