Question:
How to reverse a string in C++ using loops?
Puns
2012-01-05 06:17:27 UTC
i need to write a code to enter the random number eg:16345 and get a the output as 16345. i have to use only C++. please do not put comments in the code. i get all confused. when my sir showed it he used 5 variables (thats all i remember. lol)
1 was for the string (i.e. 16345 in this case)
1 was for number of characters in string (5 in this case)
i dun remember the others.
PLEASE help..............
Six answers:
dewcoons
2012-01-05 06:31:53 UTC
I will outline the steps for you, but you need to write the code if you are going to lean anything...



1) Read in the number and save it as a "string" (in your example, 16345)

2) Determine the length of the string and save that as an integer variable (in your example, 5)

3) Create another string to hold the reversed information

4) Set up a "For" loop that cycles the number of times as the string is long. Have it start with the length of the original string (in your example it would start with 5) and have it count down to 1

5) In the "for" loop, take the last character of the original string and assign it to the reversed string

- assign original string (character 5) to reversed string

- subtract one from the count (making it 4)

- assign original string (character 4) to reversed string (+ existing reverse string)

- subtract one from the count (making it 3)

- assign original string (character 3) to reversed string (+ existing reverse string)...

and continue until you have copied the entire string

6) Output the new reversed string (54361)
Tdz
2012-01-05 06:23:31 UTC
Since "i(you) need to write a code", I will only point you towards success!!



Do a loop starting from 0, and ending with the string's length (16345 is 5 character long for example)

now in a buffer variable that been predefined as empty (your variable:StringType = ""), push it (or "append") the last character from the string minus the loop index!

Pseudo code:

myString = 16345

myBuffer = ""

for i = 0, i should be smaller than the length of myString, i gains one every turn



myBuffer += myString's last character - i

end for

///

The first time myBuffer will be 5, then 54, then 543, 5436 and 54361



Thats it really look at your reference documents for the functions names!



Good luck!
?
2016-10-03 04:08:50 UTC
feels like an task. There are greater direct information on a thank you to try this, like s = "ABCDEF" r = "".connect(reversed(s)) print r The reversed() geared up-in function returns an iterator, no longer a chain, so which you would be able to no longer use it quickly. yet you should use it in a for loop: r = "" for ch in reversed(s) : r = r + ch print r it is gruesome for great string because of the fact, if there are N characters interior the string s, the reversed replica r would be assigned N+a million cases. Strings are immutable, in an attempt to append a character to a string ability coming up an completely new string that has the previous version's characters, plus the appended character(s). once you're making use of this strategies-set, you do no longer could desire to apply reversed(), the two. in simple terms rewrite the string task interior the loop: r = "" for ch in s : r = ch + r print r the 1st version, making use of connect, is probable the suited because of the fact it creates only one new string. If this could properly be a classification exercising and the instructions say to apply a for loop, oh properly. Use something like that final loop. it is the least complicated.
Chris C
2012-01-05 09:18:28 UTC
1 variable to hold the string: string myString = "16345";

1 variable to hold the string length: int len = myString.length();

1 variable for the loop: int n;

1 loop: for()



And personally, I'd use the loop and simply swap the values in place, using std::swap(), an example can be found here: http://www.cplusplus.com/reference/string/swap/



When I say "I'd swap the values in place", I mean I'd simply switch the first and last character, then the second and second to the last, etc. E.G. std::swap(myString[0], myString[len - 1]); and std::swap(myString[1], myString[len - 2]);, etc.
Osama
2015-02-07 15:09:02 UTC
1- the first step you enter your string.

2-make variable to store the length of a string.

3-make a comparison between the length of string if it is larger or equal than zero.

4-print the string.

5-reduce the length by one every step in loop.

#include

#include

using namespace std;





void Reverse(string name);



int main()

{

string name;

cout<<"enter your string"<
cin>>name;

Reverse(name);





system("pause");

return 0;

}



void Reverse(string name)

{

int temp=name.length()-1; //variable to store the length of string

while(temp >= 0)

{

cout<
--temp;





}

}
roger
2012-01-06 10:51:53 UTC
i think you mena how to reverse the digits in a number

here ya go:





#include

int main(void){

int i,r;

r=0;

cout << "enter a number: ";

cin >> i;

while(i){

r=r*10+ i%10;

i/=10;

}

cout << r;

return 0;

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...