1. recursive call method to implement reverse output of the unsigned number
C language implementation (DEV c ++ 4.9.9.2 run through)
[Cpp]
# Include <stdio. h>
Void reverse_print (unsigned long num)
{
If (num = 0)
Return;
Printf ("% d", num % 10); // outputs the second bit
Reverse_print (num/10); // recursive call. The second bit is output in sequence.
}
Int main (void)
{
Unsigned long num = 12345678;
Reverse_print (num );
Printf ("\ n ");
System ("PAUSE ");
Return 0;
}
# Include <stdio. h>
Void reverse_print (unsigned long num)
{
If (num = 0)
Return;
Printf ("% d", num % 10); // outputs the second bit
Reverse_print (num/10); // recursive call. The second bit is output in sequence.
}
Int main (void)
{
Unsigned long num = 12345678;
Reverse_print (num );
Printf ("\ n ");
System ("PAUSE ");
Return 0;
}
2. Stack to implement string Inversion
C ++ implementation. To use c language implementation, You need to define your own stack (dev c ++ 4.9.2 runs through)
[Cpp]
# Include <iostream>
# Include <stack>
Using namespace std;
Int main ()
{
Stack <char> s;
Char n;
Cout <"Enter the string to be reversed:" <endl;
N = getchar ();
While (n! = '\ N ')
{
S. push (n );
N = getchar ();
}
While (! S. empty ())
{
Cout <s. top ();
S. pop ();
}
Cout <endl;
System ("pause ");
Return 0;
}
# Include <iostream>
# Include <stack>
Using namespace std;
Int main ()
{
Stack <char> s;
Char n;
Cout <"Enter the string to be reversed:" <endl;
N = getchar ();
While (n! = '\ N ')
{
S. push (n );
N = getchar ();
}
While (! S. empty ())
{
Cout <s. top ();
S. pop ();
}
Cout <endl;
System ("pause ");
Return 0;
}
3. Set a header and tail pointer to implement reverse string output.
C language implementation (DEV c ++ 4.9.9.2 run through)
[Cpp]
# Include <stdio. h>
Char * converse (char * str );
Int main (int argc, char * argv [])
{
Char str [] = "1234567890 zxcvbnma"; // string array to be converted
Char * sdest;
Printf ("before converse: str = % s \ n", str );
Sdest = converse (str );
Printf ("after converse: str = % s \ n", sdest );
System ("PAUSE ");
Return 0;
}
Char * converse (char * str)
{
Char temp;
Char * s1 = str; // s1: Header pointer
Char * s2 = str + strlen (str)-1; // s2: tail pointer
// The head pointer and the tail pointer exchange the pointed value and move it to the middle until they meet each other.
For (; s1 <s2; s1 ++, s2 --)
{
Temp = * s1;
* S1 = * s2;
* S2 = temp;
}
Return str;
}
# Include <stdio. h>
Char * converse (char * str );
Int main (int argc, char * argv [])
{
Char str [] = "1234567890 zxcvbnma"; // string array to be converted
Char * sdest;
Printf ("before converse: str = % s \ n", str );
Sdest = converse (str );
Printf ("after converse: str = % s \ n", sdest );
System ("PAUSE ");
Return 0;
}
Char * converse (char * str)
{
Char temp;
Char * s1 = str; // s1: Header pointer
Char * s2 = str + strlen (str)-1; // s2: tail pointer
// The head pointer and the tail pointer exchange the pointed value and move it to the middle until they meet each other.
For (; s1 <s2; s1 ++, s2 --)
{
Temp = * s1;
* S1 = * s2;
* S2 = temp;
}
Return str;
}