To reverse an array in C++, you can use the following method:
- Declare an array of the desired type and size.
- Calculate the length of the array using the sizeof() operator.
- Declare two variables, start and end, to keep track of the start and end indices of the array.
- Use a while loop where start is less than end: Swap the elements at start and end indices of the array. Increment start and decrement end.
- The array will be reversed after completing the loop.
Here's an example implementation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#include <iostream> using namespace std; void reverseArray(int arr[], int length) { int start = 0; int end = length - 1; while (start < end) { // Swap elements at start and end indices int temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; // Increment start and decrement end start++; end--; } } int main() { int arr[] = {1, 2, 3, 4, 5}; int length = sizeof(arr) / sizeof(arr[0]); cout << "Original Array: "; for (int i = 0; i < length; i++) { cout << arr[i] << " "; } reverseArray(arr, length); cout << "\nReversed Array: "; for (int i = 0; i < length; i++) { cout << arr[i] << " "; } return 0; } |
In this example, the reverseArray()
function takes an array and its length as parameters. It uses a while loop to swap the elements from the start and end indices until they meet in the middle. The main()
function initializes an array, calculates its length, and then calls reverseArray()
to reverse the array. Finally, it prints the original and reversed arrays.
How to reverse words in a sentence using an array in C++?
To reverse words in a sentence using an array in C++, you can follow the steps below:
- Include the necessary header files:
1 2 3 |
#include <iostream> #include <string> using namespace std; |
- Define a function to reverse the words in a sentence:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
void reverseWords(string &sentence) { int len = sentence.length(); // Reverse the entire sentence using two pointers approach int start = 0, end = len - 1; while (start < end) { swap(sentence[start], sentence[end]); start++; end--; } // Reverse individual words in the sentence using two pointers approach int wordStart = 0; for (int i = 0; i < len; i++) { if (sentence[i] == ' ') { int wordEnd = i - 1; while (wordStart < wordEnd) { swap(sentence[wordStart], sentence[wordEnd]); wordStart++; wordEnd--; } wordStart = i + 1; } } // Reverse the last word in the sentence (if any) int wordEnd = len - 1; while (wordStart < wordEnd) { swap(sentence[wordStart], sentence[wordEnd]); wordStart++; wordEnd--; } } |
- Create a main function and utilize the reverseWords function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
int main() { string sentence; // Read the sentence from the user cout << "Enter a sentence: "; getline(cin, sentence); // Reverse the words in the sentence reverseWords(sentence); // Display the reversed sentence cout << "Reversed sentence: " << sentence << endl; return 0; } |
- Compile and run the program.
Now, you can enter a sentence and the program will reverse its words. For example:
1 2 |
Enter a sentence: Hello world! Reversed sentence: world! Hello |
How to reverse an array by swapping elements in C++?
To reverse an array by swapping elements in C++, you can use the following approach:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#include <iostream> void reverseArray(int arr[], int start, int end) { while (start < end) { // Swapping elements int temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; start++; end--; } } int main() { int size; std::cout << "Enter the size of the array: "; std::cin >> size; int arr[size]; std::cout << "Enter the elements of the array: "; for (int i = 0; i < size; i++) { std::cin >> arr[i]; } std::cout << "Original array: "; for (int i = 0; i < size; i++) { std::cout << arr[i] << " "; } reverseArray(arr, 0, size - 1); std::cout << "\nReversed array: "; for (int i = 0; i < size; i++) { std::cout << arr[i] << " "; } return 0; } |
This program asks the user to enter the size and elements of an array. It then calls the reverseArray
function with the array, starting index, and ending index as arguments. Inside the reverseArray
function, a while
loop is used to swap the elements at the start and end indices. The loop continues until the start index becomes greater than or equal to the end index, ensuring that all elements are swapped. Finally, the reversed array is printed in the main
function.
How to reverse an array of strings in C++?
To reverse an array of strings in C++, you can follow these steps:
- Include the necessary header file for input and output () and for string manipulation ().
- Create a function named reverseArray that takes an array of strings as a parameter.
- Inside the function, declare two integer variables named start and end. Initialize start with 0 and end with the length of the array minus 1.
- Use a loop to swap the strings at the start and end indices while incrementing start and decrementing end. Continue the loop until start becomes greater than or equal to end.
- After the loop, the array will be reversed.
Here is the complete example code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <iostream> #include <string> void reverseArray(std::string arr[], int length) { int start = 0; int end = length - 1; while (start < end) { std::swap(arr[start], arr[end]); start++; end--; } } int main() { std::string arr[] = { "Hello", "World", "This", "Is", "Reversed" }; int length = sizeof(arr) / sizeof(arr[0]); reverseArray(arr, length); for (int i = 0; i < length; i++) { std::cout << arr[i] << " "; } return 0; } |
This code will reverse the strings in the array "arr" and output the reversed array: "Reversed Is This World Hello".