Reverse string, reverse string in C Language

Source: Internet
Author: User

Reverse string, reverse string in C Language

Through this article, you will learn how to simply optimize algorithms and programming the basic method-apply, at the same time, you will better understand the string.

Start with an integer array.

 

Suppose there is (int []) {, 5}, and you want to change it to (int []) {5, 4, 3, 2, 1}. What should you do? This is the array inversion question we are going to explore today. Although this seems to be of little use, it is helpful for improving programming capabilities. We first use a graph to analyze how to reverse an integer array.

You can change the array on the left to the array on the right by changing the elements pointed to by the end of the line segment according to the line segment instructions. Every element in an integer array is normal (it does not end with '\ 0' as a string ). So it is very easy to reverse the integer array. We are now designing algorithms.

Directly, we need two variables to represent the two ends of a line segment. For arrays with an odd number of elements, we write the class C Pseudo Code as follows:

For (I = 0, j = SIZE-1; I! = J; I ++, j --)

Temp = array [I];

Array [I] = array [j];

Array [j] = temp;

For an array with an even number of elements, we write the class C Pseudo Code as follows:

For (I = 0, j = SIZE-1; I <j; I ++, j --)

Temp = array [I];

Array [I] = array [j];

Array [j] = temp;

After optimization, we find that j = SIZE-I-1, I <SIZE/2, and when the number of elements is an even number, I will not be equal to j. The code is obtained after synthesis:

#include <stdio.h>#include <stdlib.h>#define SIZE 10int main(int argc, char * argv[]){    int i, temp, array[SIZE] = {1,2,3,4,5,6,7,8,9,10};    for(i = 0; i < SIZE / 2; i++){        temp = array[i];        array[i] = array[SIZE - i - 1];        array[SIZE - i - 1] = temp;    }    for(i = 0; i < SIZE; i++)        printf("%d ",array[i]);    getch();    return 0;}

Running result:

Reverse string !!!

 

We have already learned more about strings (my previous article is more detailed ). Because the string is composed of characters plus '\ 0', it is equivalent to a character array, so it can also be reversed. If you use the above Code to reverse the integer array directly on the string, an error will certainly occur. All I want to say is that every element of an integer array is normal and has no special meaning, but a character string has a special element, that is, the element whose content is '\ 0, it indicates the end of the string, and also distinguishes the character array from the string. Therefore, if we use the previous ideas to reverse the query, the first element of the string is '\ 0'. Isn't the subsequent element meaningless? On the contrary, the program may be unstable, and subsequent code may read the error buffer. Let's take a look at the figure, which will be of great help to the subsequent algorithm design.

 

Have you fully realized it? That is to say, string inversion is actually a subarray consisting of all elements whose content is before \ 0 in the character array. We can simply design pseudo code.

Calculate the size of the sub-array

Invert sub-array

We can use the strlen () function to calculate the length of this string. In fact, we can calculate the number of elements in the sub-array, and then apply the inverted method of the integer array to reverse the sub-array. I plan to design a process to test the number of sub-array elements. The complete code is as follows:

 

# Include <stdio. h> # include <stdlib. h> # define SIZE 10int main (int argc, char * argv []) {char temp, st [SIZE] = "China"; int I, size; for (size = 0; st [size]! = '\ 0'; size ++) continue; // If the element content is not 0, it increments by 1 to get the number of sub-array elements for (I = 0; I <size/2; I ++) {temp = st [I]; st [I] = st [size-I-1]; st [size-I-1] = temp;} puts (st); getch (); return 0 ;}

 

Running result:

 

Try to sort strings by simple sorting method. Tip: use an array composed of char pointers to point to the string and sort the array.

 

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.