[C++] 如何反轉字串 (How to Invert A String in C++)

The Problem:Your job is, inverting an input string,

Sample Input:abcde
Output:edcba

Here is an example to solve this problem in C++:

#include <iostream>
#include <string.h>
#define MAX_SIZE 256
using namespace std;

int main(){
    char input[MAX_SIZE];

    cout << "Input a String to inverse: ";

    cin.getline(input,MAX_SIZE);
    int input_Size = strlen(input);

    cout << "Before String inverse: " << input;

    for(int i = 0;i < input_Size/2; i++){
        char temp;
        temp = *(input+(input_Size-i-1));
        *(input+(input_Size-i-1)) = *(input+i);
        *(input+i) = temp;
    }

    cout << "After String inverse: " << input;
    return 0;
}
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments