There are many ways defined to reverse a string in the C++ code including stack, In-place, and iteration. In this sample, a simple string will be reversed iteratively with the following algorithm;AlgorithmSTART Step-1: Input the string Step-2: Get the length of the string using length() method Step-3: Swap the last character to first using for loop Step-4: Print ENDIncompatibility of the above calculation, the accompanying code in c++ language tried as following;Example Live Demo#include using namespace std; void strReverse(string& str){ int n = str.length(); // Swap character starting from two cout
Problem statementThere are many points in two-dimensional space which need to be visited in a specific sequence.Path from one point to other is always chosen as shortest path and path segments are always aligned with grid lines.We are given the path which is chosen for visiting the points. We need to tell the minimum number of points that must be needed to generate given paths.Algorithm1. We can solve this problem by observing the pattern of movement when visiting the stop 2. If we want to take the shortest path from one point to another point, then we will move in ... Read More
The article showcase an array to be reversed in descending order using the C++ coding wherein the highest index is swapped to lowest index consequently by traversing the array in the loop.Example Live Demo#include #include using namespace std; void reverseArray(int arr[], int n){ for (int low = 0, high = n - 1; low < high; low++, high--){ swap(arr[low], arr[high]); } for (int i = 0; i < n; i++){ cout
Problem statementGiven an array of N distinct elements, find the minimum number of swaps required to sort the arrayExampleIf array is {4, 2, 1, 3} then 2 swaps are requiredSwap arr[0] with arr[2]Swap arr[2] with arr[3}Algorithm1. Create a vector of pair in C++ with first element as array alues and second element as array indices. 2. Sort the vector of pair according to the first element of the pairs. 3. Traverse the vector and check if the index mapped with the value is correct or not, if not then keep swapping until the element is placed correctly and keep counting ... Read More
This article reverse a string using the pointer in C++ coding, First, it calculates the length of a pointer to string then run a for loop in decrement order to display the reverse string as follows;Example Live Demo#include #include using namespace std; int main(){ char *str="ajaykumar"; cout
Problem statementGiven an unsigned number, find the minimum number that could be formed by using the bits of the given unsigned number.ExampleIf input = 10 then answer would be 3Binary representation of 10 is 1010 and minimum number with 2 set bits is 0011 i.e. 3Algorithm1. Count the number of set bits. 2. (Number of set bits) ^ 2 – 1 represents the minimized number)Example Live Demo#include using namespace std; int getSetBits(int n) { int cnt = 0; while (n) { ++cnt; n = n & (n - 1); } return cnt; } int getMinNumber(int n){ int bits = getSetBits(n); return pow(2, bits) - 1; } int main() { int n = 10; cout
The Selection Sort algorithm sorts an exhibit by more than once finding the base component from the unsorted part and putting it toward the start. In each emphasis of determination sort, the base component from the unsorted subarray is picked and moved to the arranged subarray.Example Live Demo#include #include using namespace std; #define MAX_LEN 50 void selectionSort(char arr[][50], int n){ int i, j, mIndex; // Move boundary of unsorted subarray one by one char minStr[50]; for (i = 0; i < n-1; i++){ // Determine minimum element in unsorted array ... Read More
Problem statementLucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. The task is to find minimum lucky number has the sum of digits equal to n.ExampleIf sum = 22 then lucky number is 4477 as 4 + 4 + 7 + 7 = 22Algorithm1. If sum is multiple of 4, then result has all 4s. 2. If sum is multiple of 7, then result has all 7s. 3. If sum is not multiple of 4 or 7, then we can subtract one of them till sum becomes multiple of other.Example Live ... Read More
The sorting in ascending or descending order, however, can duly be performed by using string sort method and other means too in the C++ programming. But here, the string compare (first words with the second) and copy (copy the first word in a temp variable) method involved in the inner and outer traversing loop to put the words in descending order as following.Example Live Demo#include using namespace std; int main(){ char str[3][20]={"Ajay","Ramesh","Mahesh"}; char t[20]; int i, j; for(i=1; i
The stable_sort method of STL first sorts the components with name as the key in ascending order and afterward the components are arranged with their segment as the key. Moreover, The stable_sort() calculation is viewed as steady in light of the fact that the overall request of comparable components is kept up. Here is the source code of the C++ program which exhibits the stable_sort() calculation demonstrated as follows;Example Live Demo#include using namespace std; int main(){ int arr[] = { 11, 15, 18, 19, 16, 17, 13, 20, 14, 12, 10 }; int n = sizeof(arr) / sizeof(arr[0]); stable_sort(arr, arr + n); cout
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP