The C language uses pointers and arrays to implement reverse string and reverse string order.

Source: Internet
Author: User

The C language uses pointers and arrays to implement reverse string and reverse string order.

1. The Array Implementation (without using string functions) program is as follows:

1 void ReverseByArray (char * s)
 2 {
 3 int len = 0;
 4 while (s [len]! = '\ 0')
 5 len ++;
 6 int t;
 7 for (int i = 0; i <len / 2; i ++)
 8     {
 9 t = s [i];
10 s [i] = s [len-i-1];
11 s [len-i-1] = t;
12}
13}
For operations related to arrays, you must use the string length len (because the length is relatively simple, you don't need a string function)

On line 3-5, first check the '\ 0' character to determine the length of the string and record it with the len variable

I tried the following code. The measured string length is 1 larger than the original

int len = 0;
while (s [len ++]! = '\ 0')
; // Empty statement
Don't use the above method

Then the specific implementation of the reverse order,

There are several common methods, many online are malloc out len + 1 size space, and then put it in reverse order

But I think that since it is an array implementation, I use a temporary variable t to directly exchange the method before and after the exchange (the loop is just the loop len / 2)

Another problem that is prone to occur is when switching s [len-i-1], why do we need to decrease by 1?

 

2. Use the pointer method to reverse the string

 1 void ReverseByPointer (char * s)
 2 {
 3 char buf [100];
 4 char * b = buf;
 5 char * p = s;
 6 while (* p! = '\ 0')
 7 * (b ++) = * (p ++);
 8 while (* s! = '\ 0')
 9 * (s ++) = * (-b);
10}
Use pointers to highlight the characteristics of pointers, unlike arrays

At first glance, the program seems particularly simple.

But this program uses a buffer,

If this buffer size is not enough ↓ as follows


When the buffer is enough, the program will run normally.

The two newly created pointer variables b and p are not superfluous.

buf is the name of the array. Although the value is the first address of the array, this first address cannot be added or subtracted, so a new b pointer is created to point to buf.

s is of type char *, it can be added or subtracted, but how to add it to the end (after finding \ 0)?

Then I chose to define a p pointer variable and assign it to the pointer s, that is, s and p are the same

And p moves back to find '\ 0', its task is completed, there is no need to return to the starting position

Finally, fill the content of buf-(inverted) and fill in the string pointed to by s.

Finally, there is the problem of \ 0. Because the string is only in reverse order, the length does not change, so \ 0 is still the original s of \ 0, and you do n’t need to add \ 0

 

3. Complete program and operation result

 1 #include <stdio.h>
 2 
 3 void ReverseByArray (char * s)
 4 {
 5 int len = 0;
 6 while (s [len]! = '\ 0')
 7 len ++;
 8 int t;
 9 for (int i = 0; i <len / 2; i ++)
10 {
11 t = s [i];
12 s [i] = s [len-i-1];
13 s [len-i-1] = t;
14}
15}
16
17 void ReverseByPointer (char * s)
18 {
19 char buf [100];
20 char * b = buf;
21 char * p = s;
22 while (* p! = '\ 0')
23 * (b ++) = * (p ++);
24 while (* s! = '\ 0')
25 * (s ++) = * (-b);
26}
27
28 int main ()
29 {
30 char s [20] = "I Love you 233";
31 printf ("Source string:% s \ n", s);
32
33 ReverseByArray (s);
34 printf ("Reverse order of array mode:% s \ n", s);
35
36 ReverseByPointer (s);
37 printf ("Reverse order of pointer mode:% s \ n", s);
38}
39
40 / * Running result in VC compiler
41 Source string: I Love you 233
42 Reverse order in array mode: 332 uoy evoL I
43 Reverse pointer mode: I Love you 233
44 Press any key to continue ...
45 * /
 Original Original (Blog Park)

Related Article

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.