[C Language] Invert a string

invert string

topic

describe

Invert the words in a sentence without inverting the punctuation. For example, I like beijing. After passing through the function, it becomes: beijing. like I

Enter description:

Each test input contains 1 test case: I like beijing. The input case length does not exceed 100

Output description:

Output the inverted strings in sequence, separated by spaces

Example

enter:

I like beijing.

Output:

beijing. like I

parse

First, we can reverse the entire string, and then use a pointer to identify spaces. When a space is encountered, the previous characters will be reversed. When '\0' is encountered, the pointer will not be allowed to continue going backwards.

First of all, we need to load the string, so we will use gets() here first.

int main() {
    
    
    char str[100] = {
    
    0};
    gets(str);
    return 0;
}

Then we need to write a function that reverses the order of a string. It can be used to reverse the entire string at the beginning, or it can be used to reverse individual words later.

The reverse order is very simple. Set the two pointers to move from both sides to the middle and exchange them. When the left pointer runs to the right of the right pointer, the function stops.

void reverse(char* left, char* right) {
    
    
    while (left < right) {
    
    
        char tmp = *left;
        *left = *right;
        *right = tmp;
        right--;
        left++;
    }
}

Then the entire string is reversed. We use strlen() to get the length of the string, and then "-1+array name" to get the pointer to the end.

reverse(str, str + len - 1);

Then the following is our reverse order of words one by one. To reverse the order of words one by one, we must first find these words.

Then we can first create a pointer pointing to the beginning of the string, and then let it go all the way back, and perform operations when encountering two situations: 1. Encountering a space 2. Encountering ' \0 '

So this must be a loop. When the pointer points to something other than a space or ' \0 ', the pointer keeps +1

	char* cur = str;
	while ( *cur != ' ' && *cur != '\0') {
    
    
            cur++;
	}

Then when the loop stops, cur must have pointed to the space, and -1 is equivalent to getting the right pointer. So at this time, we need to reverse the previous characters. We must get the left pointer, so at the beginning, we can create a pointer to hold the starting position.

Then we can reverse a word

	char* cur = str;
	char* start = cur;
	while ( *cur != ' ' && *cur != '\0') {
    
    
		cur++;
    }
	reverse(start, cur - 1);

At this time, cur is still pointing to the space. At this time, let cur++ skip the space and point to the following letters.

But if what we find is not a space, but ' \0 ', letting it jump out at this time will cause unknown problems, so we have to add an if condition to make it only when it encounters a space. ++

	char* cur = str;
	char* start = cur;
	while ( *cur != ' ' && *cur != '\0') {
    
    
		cur++;
	}
	reverse(start, cur - 1);
	if (*cur != '\0')
    cur++;

After writing the above, I have actually only finished one word in reverse order. If I want him to continue exploring, I will definitely need to add another layer of loops. So how should I write the conditions for this loop?

This string of codes must go all the way to the last word, so what does it mean that we have found the last word?

In fact, when cur points to ' \0 ', it means that we have found the last word, then the loop can end at this time, so we can add a while loop based on this condition outside.

char* cur = str;
//我们这个初始化cur的代码只用执行一次,不要把它装到循环里了
    while (*cur) {
    
    
        char* start = cur;
        while ( *cur != ' ' && *cur != '\0') {
    
    
            cur++;
        }
        reverse(start, cur - 1);
        if (*cur != '\0')
            cur++;
    }

Finally, we only need to print the string we have processed, and this question is over.

Code overview

#include <stdio.h>
void reverse(char* left, char* right) {
    
    
    while (left < right) {
    
    
        char tmp = *left;
        *left = *right;
        *right = tmp;
        right--;
        left++;
    }
}
int main() {
    
    
    char str[100] = {
    
    0};
    gets(str);
    int len = strlen(str);
    reverse(str, str + len - 1);
    char* cur = str;
    while (*cur) {
    
    
        char* start = cur;
        while ( *cur != ' ' && *cur != '\0') {
    
    
            cur++;
        }
        reverse(start, cur - 1);
        if (*cur != '\0')
            cur++;
    }
    printf("%s",str);
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_42150700/article/details/129629052