Assignment 3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Assignment # 03

Course: Programming Techniques Total Marks: 100


Section: BS Computer Science, Software Engg
Semester: 2nd
Due Date:7 -May- 24
Late Assignments will be dealt as per policy given in the course outline. Copied
assignment will be getting zero marks and further action will be taken as per
SSCASEIT university policy.

Objective: To gain a fundamental understanding of Programming.

Warning
This is an individual assignment. Any copying or collaboration of any sort would be
considered plagiarism and would lead to zero marks being awarded for the assignment
to both individuals. Disciplinary proceedings may also be initiated for extreme cases.

1. Basic String Operations:


 How do you declare and initialize a string in C++?
 Explain the difference between a character array and a string.
 What is the significance of the '\0' character in C++ strings?
2. String Functions:
 Describe the purpose of the getline() function in C++ and when it is preferred
over cin.
 Explain the usage of the append() function in C++ with an example.
 How would you convert a string containing a numeric value to an integer using
stoi()?
3. String Manipulation:
 Write a C++ code to concatenate two strings using the '+' operator.
 Demonstrate how to copy the content of one string into another using the
assignment operator.
4. String Sorting:
 Provide an example of sorting characters in a string in ascending order.
 How can you sort a string in descending order in C++?
5. Case Conversion:
 Write a C++ program to convert all characters in a string to uppercase using a
loop.
 Demonstrate how to convert a string to lowercase using the transform() function.
6. Max Frequency Character:
 Write a program in C++ to find the character with the maximum frequency in a
given string.
 How would you modify the program to handle both uppercase and lowercase
characters?
7. In-Built String Functions:
 Explain the use of s.clear() and s.empty() functions in C++ strings.
 How does the s.substr(3, 4) function work, and what does it return?
8. Advanced String Operations:
 Provide an example of using the s.erase() function to delete a substring from a
string.
 Explain the purpose of the s.find() function and demonstrate its use.
9. String Comparison:
 Compare two strings using the s1.compare(s2) function and explain the output.
 How can you compare two strings without considering the case?
10. String Resizing:
 Explain the difference between s.length() and s.size() in C++ strings.
 How do you use the s.resize() function to change the length of a string?

Recursion:

1. Problem Statement:

Write a C++ program to implement a recursive function isPowerOfFour(int n) that takes an


integer n as input and returns a boolean value indicating whether n is a power of four. A number
is considered a power of four if it can be made by multiplying 4 by itself some number of times
(e.g., 4, 16, 64, ...). The function should return true if n is a power of four, and false otherwise.
The function should handle negative inputs by returning false, and should return false for zero as
well.

Instructions:

The function isPowerOfFour(int n) should be implemented to determine whether the given


integer n is a power of four using recursion.

i. Your program should include the necessary header files and should compile without errors.

ii. The output should be a boolean value indicating whether n is a power of four.

iii. Ensure proper indentation and comments for clarity and readability of the code.

Example:

For the input 16, the output should be true because 16 is a power of four (4^2 = 16).

For the input 6, the output should be false because 6 is not a power of four.

For the input -64 or 0, the output should be false.

Evaluation Criteria:

iv. Correctness of the program in determining whether the given integer is a power of four.

v. Proper implementation of recursion with a base case.

vi. Handling of negative inputs and zero.

vii. Code readability, including proper variable naming and comments.

viii. Adherence to the given instructions and problem statement.

2. Problem Statement:
Write a C++ program to reverse a string using recursion. The program should define a function
reverse(string s) which takes a string s as input and prints its reverse.

Instructions:
The function reverse(string s) should be implemented to reverse the given string s using
recursion.
i. You are not allowed to use any built-in or library function to directly reverse the string.
ii. Your program should include the necessary header files and should compile without errors.
iii. The output should only consist of the reversed string without any additional characters or
spaces.
iv. Ensure proper indentation and comments for clarity and readability of the code.
Example:
For the input string "binod", the output should be "donib".
Evaluation Criteria:
1. Correctness of the program in reversing the given string.
2. Proper implementation of recursion with a base case.
3. Code readability, including proper variable naming and comments.
4. Adherence to the given instructions and problem statement.

3. Problem Statement:
Write a C++ program to replace all occurrences of "pi" with "3.14" in a given string using
recursion. The program should define a function replacePi(string s) which takes a string s as
input and prints the modified string with "pi" replaced by "3.14".

Instructions:
The function replacePi(string s) should be implemented to replace all occurrences of "pi" with
"3.14" using recursion.
v. You are not allowed to use any built-in or library function to directly replace substrings.
vi. Your program should include the necessary header files and should compile without errors.
vii. The output should only consist of the modified string with "pi" replaced by "3.14", without
any additional characters or spaces.
viii. Ensure proper indentation and comments for clarity and readability of the code.
Example:
For the input string "pippppiiiipi", the output should be "3.14ppp3.14iiii3.14".
Evaluation Criteria:
1. Correctness of the program in replacing "pi" with "3.14" in the given string.
2. Proper implementation of recursion with a base case.
3. Code readability, including proper variable naming and comments.
4. Adherence to the given instructions and problem statement.
4. Problem Statement:
Write a C++ program to move all occurrences of the character 'x' to the end of a given string
using recursion. The program should define a function moveX(string s) which takes a string s as
input and returns the modified string with all 'x' characters moved to the end while maintaining
the order of other characters.

Instructions:
1. The function moveX(string s) should be implemented to move all occurrences of 'x' to
the end of the string using recursion.
2. Your program should include the necessary header files and should compile without
errors.
3. The output should only consist of the modified string with all 'x' characters moved to the
end, without any additional characters or spaces.
4. Ensure proper indentation and comments for clarity and readability of the code.
Example: For the input string "axxbdxcefxhix", the output should be "abdcfehiixx".
valuation Criteria:
1. Correctness of the program in moving all 'x' characters to the end of the given string
while maintaining the order of other characters.
2. Proper implementation of recursion with a base case.
3. Code readability, including proper variable naming and comments.
4. Adherence to the given instructions and problem statement.

Best of Luck

You might also like