5 words in reverse order | Huawei od computer test

Original title:[Full marks][Huawei OD machine test questions 2023 JAVA] Words in reverse order_Ruo Bodou's blog-CSDN blog

c++ code:

#include <iostream>

using namespace std;

int main()
{
    string a[100];
    int i = 0;
    while(cin >> a[i]) i ++;
    for(int j = 0; j < i; j ++ )
    {
        //取出字符串数组中的每个字符串a[i]
        string str = a[j];
        char temp = '\0';
        for(int k = str.length() - 1; k >= 0; k -- )
        {
            //反转后将字符串中的每个字符输出
            //如果是标点, 先不输出
            if(str[k] == ',' || str[k] == '.' || str[k] == '!') {
                temp = str[k];
            }
            else cout << str[k];
        }
        if(temp != '\0') cout << temp;
        cout << ' ';
        temp = '\0';
    }
    
    cout << endl;
    
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_65293439/article/details/129848112