[Huawei Machine Test] String Processing

Introduce Niuke - the string content in Huawei machine test, especially the use of some common library functions

Summary of common functions

字符串转数字
int i = stoi(str);

数字转字符串
string s=to_string(n);

HJ1 The length of the last word of the string

while(cin >> s);, as long as there is a space, each input can be separated, so the last input is the last word

#include<bits/stdc++.h>

using namespace std;

int main() {
    
    
    string s;
    while(cin >> s);
    cout << s.size();
    return 0;
}

HJ2 counts the number of occurrences of a character

method 1

getline(cin, s); can input cin into s (cin will be separated by spaces, but getline will not) The
return value of getchar() is the character
tolower(ch) converted to lowercase

The count_if function is used (count_if is similar to count, the difference is that the third parameter, count needs to correspond to the type in the container, and count_if can be an expression) and lambda expression
C++11 lambda expression intensively talks about
C++ lambda expression and function object

#include <algorithm>
#include <iostream>
#include <string>
 
using namespace std;
 
int main()
{
    
    
    string s;
    getline(cin, s);
    char c = tolower(getchar());
    cout << count_if(s.begin(), s.end(), [c](char i) {
    
     return towlower(i) == c; }) << endl;
}

Unsigned short int is actually equal to uint16_t;
type alias analysis of uint8_t / uint16_t / uint32_t /uint64_t in C language

#include <iostream>
#include <string>

using namespace std;

int main()
{
    
    
    string s;
    getline(cin, s);

    char c = tolower(getchar());

    uint16_t n = 0;
    for (auto i : s) {
    
    
        if (tolower(i) == c) {
    
    
            ++n;
        }
    }
    cout << n << endl;
}

Method 2 map

The number of occurrences should be thought of as a map

#include <iostream>
#include <string>
#include <unordered_map>

using namespace std;

int main(int, char **)
{
    
    
    string s;
    char c;
    getline(cin, s) >> c;

    unordered_map<char, size_t> unorderedMap;
    for (auto i : s) {
    
    
        ++unorderedMap[tolower(i)];
    }
    cout << unorderedMap[tolower(c)] << endl;
}

HJ4 string delimited

Method 1 direct string manipulation

str.substr(i, 8) Starting from the i-th of str, intercept the string of eight characters
str.append(count, '0'); add count '0' characters after the string

#include <iostream>
#include <string>
using namespace std;

int main()
{
    
    
    string str;
    while (cin >> str)
    {
    
    
        // 补0
        int len = str.size();
        if (len % 8 != 0)
        {
    
    
            int count = 8 - len % 8;
            str.append(count, '0');
        }

        // 按格式输出
        int newLen = str.size();
        for (int i = 0; i < newLen; i += 8)
        {
    
    
            cout << str.substr(i, 8) << endl;
        }
    }
    return 0;
}

Method 2 cout member function

Use the member function width() of the cout object to specify the width of the output field , and
use the member function fill() to specify the fill character.
Use the stream operator left to specify left alignment
so that the remaining string length of str is less than 8, and str.substr(0, 8) also The remaining characters can be obtained normally without error.

#include <iostream>
#include <string>
using namespace std;

int main()
{
    
    
    string str;
    while (cin >> str)
    {
    
    
        int len = str.size();
        for (int i = 0; i < len; i += 8)
        {
    
    
            cout.width(8);
            cout.fill('0');
            cout << left << str.substr(i, 8) << endl;
        }
    }
    return 0;
}

HJ5 hexadecimal conversion

Method 1 using ASCII code

When a character is forced to int type, it will become the corresponding ASCII code. The ASCII code
value of the space is 32; the
ASCII code value of the numbers 0 to 9 is 48 to 57 respectively;
the ASCII code value of the uppercase letters "A" to "Z" The values ​​are 65 to 90, respectively;
the ASCII code values ​​of the lowercase letters "a" to "z" are 97 to 122, respectively.

#include <iostream>
using namespace std;

int main() {
    
    
    std::string a;
    std::cin >> a;
    int b = 0;
    int c = 1;
    for (int i = a.size() - 1; i >= 2; i--) {
    
    
        int temp = (int)a[i];
        if (temp >= 65) temp = temp - 55;
        if (temp >= 48) temp = temp - 48;
        b += temp *  c;
        c = 16 * c;
    }
    std::cout << b;
}
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
int main()
{
    
    
    string str;
    while (cin >> str)
    {
    
    
        int len = str.size();
        int sum = 0;
        //十六进制转换为十进制
        for (int i = len - 1; i >= 0; --i)
        {
    
    
            // 数字字符的转换,ASCII码:'0'——>48,十六进制:0——>0
            if (str[i] >= '0' && str[i] <= '9')
            {
    
    
                sum += (str[i] - 48) * pow(16, len - 1 - i);
            }
            // 字母字符的转换,ASCII码:A——>65,十六进制:A——>10
            else if (str[i] >= 'A' && str[i] <= 'F')
            {
    
    
                sum += (str[i] - 55) * pow(16, len - 1 - i);
            }
        }
        cout << sum << endl;
    }
}

Method 2 C++ comes with hexadecimal input

#include <iostream>
 
using namespace std;
 
int main(int, char**)
{
    
    
    uint32_t n;
    cin >> hex >> n;
    cout << n << endl;
}

Method 3 stoi function

#include<iostream>
#include<string>
using namespace std;
int main()
{
    
    
    string s;
    while(getline(cin,s))
    {
    
    
        cout<<stoi(s,0,16)<<endl;//stoi(字符串,起始位置,n进制),将 n 进制的字符串转化为十六进制
        //将字符串 s 从 0 位置开始到末尾的 2 进制转换为十六进制.
    }
 
    return 0;
}

HJ11 reverse string

Method 1 reverse

#include<bits/stdc++.h>

using namespace std;


int main(){
    
    
    
    string str;
    cin>>str;
    reverse(str.begin(),  str.end());
    cout<<str;
    
    
    return 0;
}

Method 2

copy(str.rbegin(),str.rend(),ostreambuf_iterator(cout));

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(){
    
    
    string str;
    getline(cin, str);
    copy(str.rbegin(),str.rend(),ostreambuf_iterator<char>(cout));
}

HJ17 coordinate movement

Complicated, focus on review

Method 1 C++ regular expression

#include <iostream>
#include <string>
#include <vector>
#include <set>
#include<regex>
using namespace std;

int main(){
    
    
    string s;
    getline(cin,s);
    int x=0,y=0;
    int j=0;
    for(int i=0;i<s.size();++i)
    {
    
    
        if(s[i]==';')
        {
    
    
            string t=s.substr(j,i-j);
            regex pattern("^([ASWD]{1})(\\d{1,2})$");
            smatch result;
            bool flag=regex_match(t,result,pattern);
            if(flag)
            {
    
    
                if(result[1]=='A') x-=stoi(result[2]);
                else if(result[1]=='D') x+=stoi(result[2]);
                else if(result[1]=='S') y-=stoi(result[2]);
                else if(result[1]=='W') y+=stoi(result[2]);
            }
            while(i<s.size()&&s[i+1]==';') ++i;
            j=i+1;
        }
    }
    cout<<x<<','<<y<<endl;
}

Guess you like

Origin blog.csdn.net/m0_51371693/article/details/130077327