Prompt
Answer
C/C++ Function Tutorial: String Reversal
In this tutorial, we will write a C++ function to reverse a given string. The program will highlight important concepts in C/C++ such as function definition, input validation, and basic string manipulation.
Function Overview
The function reverseString
will take a string as an input parameter and return its reversed version. We will ensure the function adheres to best practices including:
- Proper documentation
- Input validation
- Inline comments for clarity
Necessary Imports
For this task, we primarily use the <string>
library to handle strings in C++.
Code Implementation
Here’s the complete implementation of the reverseString
function.
#include
#include
#include
/**
* @brief Reverses the input string.
*
* This function takes a string as input, validates it,
* and returns the reversed string.
*
* @param input The string to be reversed.
* @return std::string The reversed string.
* @throws std::invalid_argument if the input string is empty.
*/
std::string reverseString(const std::string& input) {
// Check for input validity
if (input.empty()) {
throw std::invalid_argument("Input string cannot be empty.");
}
std::string reversed; // Variable to hold the reversed string
// Loop through the string backwards and build the reversed version
for (int i = input.length() - 1; i >= 0; --i) {
reversed += input[i]; // Append each character to the reversed string
}
return reversed; // Return the reversed string
}
int main() {
try {
// Test the reverseString function with a sample input
std::string original = "Enterprise DNA";
std::string reversed = reverseString(original);
std::cout << "Original String: " << original << std::endl;
std::cout << "Reversed String: " << reversed << std::endl;
} catch (const std::invalid_argument& e) {
// Handle the empty string exception
std::cerr << "Error: " << e.what() << std::endl;
}
return 0; // Indicate successful execution
}
Key Sections Explained
Imports
#include <iostream>
: Required for standard input/output stream.#include <string>
: Provides string manipulation capabilities.#include <stdexcept>
: Used for throwing exceptions.
Function Documentation
- The function
reverseString
is documented using a docstring format that clearly states its purpose, parameters, return type, and exceptions.
Input Validation
- A check is performed to ensure that the input string is not empty. If it is, an exception is thrown.
String Reversal Logic
- A
for
loop iterates through the input string from the last character to the first, constructing the reversed string.
Main Function
- The main function tests
reverseString
and handles any exceptions that might arise from invalid input.
Code Usage Example
To use the reverseString
function, you can simply call it from your main program, providing it with a string:
std::string myString = "Hello World!";
std::string reversed = reverseString(myString);
std::cout << "Reversed String: " << reversed << std::endl; // Outputs: !dlroW olleH
Conclusion
In this tutorial, we created a simple yet effective C++ function to reverse a string. This demonstrates fundamental programming concepts, including:
- Function creation and documentation
- Input validation
- String manipulation
For further learning, consider exploring courses on the Enterprise DNA Platform for more advanced C/C++ programming concepts and techniques.
Description
This tutorial guides you through creating a C++ function to reverse a string. It covers function definition, input validation, string manipulation, and best coding practices with a complete implementation example.