[Question Solution] What? Can you flip the string with this pose?

This article introduces 4 ways to reverse strings:

  • The first two are to achieve the purpose of flipping by modifying the operation string;
  • The latter two are achieved by modifying the output mode to achieve the purpose of flipping.

Question requirements: the
user enters a string of characters, and the program returns a string in reverse order.

Example:

用户输入:asdfghjkl
程序输出:lkjhgfdsa

Method 1: Via XOR^=

#include <stdio.h>
#include <string.h>

void overturn_string(char str[]);

int main(void){
    
    
    char str[40];   //创建一个空字符串数组

    gets(str);  //获取字符串

    overturn_string(str);   //调用函数
}

void overturn_string(char str[]){
    
    
    //初始化各变量
    int length = 0;
    length = strlen(str);   //获取字符串的长度
    int i = 0;

    for(i = 0; i < length / 2; i++){
    
    
        //length / 2可以提高交换效率
        //采用异或^= 来处理字符串
        str[i] ^= str[length - i - 1];
        str[length - i - 1] ^= str[i];
        str[i] ^= str[length - i - 1];
    }
    //输出字符串
    puts(str);
}

Why can ^= reverse the string?
First obtain the decimal number of the ASCII code table of the character, and then convert it to binary. Through XOR to process to get the flipped binary, and then perform a step in reverse to convert back to characters.

Method 2: Through the pointer

#include <stdio.h>
#include <string.h>

int inverse(char *str);

int main(void){
    
    
    char str[40];   //创建一个空的字符串

    gets(str);      //获取用户输入

    inverse(str);   //调用函数
}

int inverse(char *str){
    
    
    int length = strlen(str);   //获取字符串长度
    //初始化变量,创建两个字符串指针
    char *p1;
    char *p2;

    p1 = str;   //指向字符串第一个字符的地址
    p2 = str + (length - 1);    //指向字符串最后一个字符的地址

    while(p1 < p2){
    
     //如果p1 和p2 的地址重合或p2 的地址小于p1 的地址时就会退出循环
        char c = *p1;   //创建一个char 类型的变量来存储*p1 的值
        *p1 = *p2;      //*p1 获取*p2 的值
        *p2 = c;        //*p2 获取临时存储在变量c 中的*p1 的值
        ++p1;           //将p1 指向的地址向后移动一位
        --p2;           //将p2 指向的地址向前移动一位
    }

    puts(str);  //输出交换后的字符串
}

Insert picture description here


If you find it helpful, please give me a thumbs up! !


Method 3: Through recursion

#include <stdio.h>
#include <string.h>

void overturn_string(char *str);

int main(void){
    
    
    char str[40];   //创建一个空字符串数组

    gets(str);  //获取字符串

    overturn_string(str);   //调用函数
}

void overturn_string(char *str){
    
    
    if(*str != NULL || *str != '\0'){
    
       //递归的结束条件
        overturn_string(str + 1);       //参数指向的地址往下移动一位
    }

    --str;  //因为递归*str 指向的是空字符,所以要将指向的地址前移一位

    printf("%c", *str);     //递归的函数开始从尾到头输出
}

Method 4: Pass the length of the string

#include <stdio.h>
#include <string.h>

int main(void){
    
    
    char str[40];   //创建一个空字符串数组

    gets(str);  //获取字符串

    int length = strlen(str);   //求出字符串的长度

    //直接获取字符串的长度后,从尾开始往头输出
    for(int i = length - 1; i >= 0; i--){
    
    
        printf("%c", str[i]);
    }

    return 0;
}

The article is over here, I look forward to seeing you next time~
end~


Guess you like

Origin blog.csdn.net/weixin_43776724/article/details/106453103