S24 CMP120L LAB12 CString Pointer
S24 CMP120L LAB12 CString Pointer
Anas Al Majali
College of Engineering Lab Instructor: Sahar Mahmoud
Dept of Computer Science & Engg Office: ESB-1004B
P. O. Box 26666 Phone: 971-6-515-4944
Sharjah, UAE e-mail: skmahmoud@aus.edu
Semester: Spring 2024
Note: using ChatGPT will be considered a violation of the AUS integrity code.
Objectives:
To practice Cstrings.
To practice declaring and initializing pointers.
To practice using the & and * operators.
Using Visual Studio 2022, write the below programs, compile and provide screenshots of output.
Note: you are required to submit copy of the code + screenshots of program run for each exercise.
[3 pts] Exercise #1
Write a C++ program to help two friends compare their favorite books. The program should prompt the users
to input the titles of their favorite books using getline() method. Additionally, it should include a
compareBooks() function that utilizes the strcmp() function to compare the books entered by the users. If
the friends have the same book in common, the program should print a message indicating this. Otherwise, it
should inform them that they don't share the same favorite book and display each user's favorite book.
Sample output:
[4 pts] Exercise #2
Define a function named caesarCipher() that takes two parameters: a C-string representing the message to
be encrypted and an integer representing the shift value. Inside the function, iterate through each character
in the message: If the character is alphabetic, shift it by the specified amount in the alphabet.
In the main function: Declare a character array named message[] to store the user's input message. Prompt
the user to input a message and shift value. Call the caesarCipher function with the input message and shift
value to encrypt the message. Use isalpha() method which take a character as a parameter.
1
Sample output:
[3 pts] Exercise #3
Write a C++ program that perform the following:
a. Declare an integer pointer ptr, then use it to allocate memory for a new integer.
Print the address of the integer.
b. Assign a new value to the integer using user input via *ptr.
c. Declare an integer x and initialize it with the value pointed at by ptr.
d. Print the value of x using its own variable name.
e. Delete the integer pointed at by ptr, then point ptr to x.
f. Print the address of x using ptr.
g. Print the value of x using ptr.
Samples output