Search
 
SCRIPT & CODE EXAMPLE
 

CPP

String reversal

/*
	C++ program for String reversal by lolman_ks.
    This logic can be used to build this program in other languages also.
*/
/*
	Logic: Place one counter at the start and one at the end.
    Keep swapping the characters while the 2nd counter > 1st Counter.
*/

#include <iostream>
using namespace std;

string returnReversedString(string text){
    int counter_1 = 0; //Place one counter at the start.
  	int counter_2 = text.length() - 1; //And the other at the end.

  	//Run a loop till the 1st counter is less than the 2nd.
    while(counter_1 < counter_2){
        char save = (char)(text[counter_1]); //Save the value of text[counter_1].

        text[counter_1] = text[counter_2];
        text[counter_2] = save;
      	//The above two lines swap the characters.

        ++counter_1;
        --counter_2;
    }
	return text;
}

//Implementations of this function.

int main(){
	cout << returnReversedString("hello") << endl; //Returns "olleh".
  	return 0;
}

/*
	I hope if you find my answers are useful. Please promote them if they are.
    #lolman_ks.
*/
Comment

PREVIOUS NEXT
Code Example
Cpp :: round double to n decimal places c++ 
Cpp :: how to run a msi file raspbrain 
Cpp :: how to split a string into words c++ 
Cpp :: how to find length of character array in c++ 
Cpp :: adding elements to a vector c++ 
Cpp :: delete map elements while iterating cpp 
Cpp :: c++ hours minutes seconds 
Cpp :: c++ declare variable 
Cpp :: how to write something in power of a number in c++ 
Cpp :: c++ struct with default values 
Cpp :: length of string c++ 
Cpp :: for in c++ 
Cpp :: how to get size of char array in c++ 
Cpp :: ue4 c++ enumaeration 
Cpp :: c++ function 
Cpp :: all data types in c++ 
Cpp :: c++ cout colored output xcode 
Cpp :: c++ 
Cpp :: arduino funktion 
Cpp :: c++ isalphanum 
Cpp :: read and write file in c++ 
Cpp :: stl sort in c++ 
Cpp :: string to uint64_t c++ 
Cpp :: print a string with printf in c++ 
Cpp :: find prime number c++ 
Cpp :: remove specific element from vector c++ 
Cpp :: cpp absolute value 
Cpp :: getline(cin string) not working 
Cpp :: how to empty string c++ 
Cpp :: Search Insert Position leetcode solution in cpp 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =