Home > Back-end >  C reverse output string
C reverse output string

Time:12-05

#include
#include

using namespace std;

Void reverStr (const string & amp; STR) {


Size_t len=STR. The size ();

For (size_t I=len - 1; I & gt;=0; - I) {
Cout & lt; }

Cout & lt; }

Int main () {
String STR.

Getline (cin, STR);

ReverStr (STR);

system("pause");
return 0;
}

/* can actually reverse the output, but the output, after cout CodePudding user response:

Because I cycle variables are of type size_t, this number is not a negative, that is to say, when I=0, execution - I, I=2... 32-1=4294967295; Reuse STR. At (I), of course, the subscript crossing the line

CodePudding user response:

I - I, a more classic algorithm to judge & gt; 0, such as:
 
String aa="123456789"; Int a=aa. The size ();
While (a) cout & lt;

This algorithm is not crossing the line,

CodePudding user response:

Size_t is unsigned types, so when I=0, - I was not negative, but a big number, to define the I as an int

CodePudding user response:

Modify, according to the example of you a little bit:
  • Related