Multiple methods to implement the string/unsigned number reversely output_stack_recursion _ header and tail pointer

Source: Internet
Author: User

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;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.