跳转至

字符串进阶(C++中的string类)

复习:C风格字符串相关函数的实现

#include<iostream>
using namespace std;

int strlen(char *s){
    char *start = s;
    while(*s != '\0'){
        s++;
    }
    return s - start;
}

char * strcpy(char *d, char *s){
    char *r = d;
    while(*s != '\0'){
        *d++ = *s++;
    }
    *d = '\0';
    return r;
}

char * strcat(char *d, char *s){
    char *r = d;
    while(*d != '\0'){
        *d++;
    }
    while(*s != '\0'){
        *d++ = *s++;
    }
    *d = '\0';
    return r;
}

int strcmp(const char *s1, const char *s2){
    while(*s1 != '\0' && *s2 != '\0'){
        if(*s1 != *s2) return *s1 - *s2;
        s1++;
        s2++;
    }
    return *s1 - *s2;
}

char * strstr(char *s1, char *s2){
    while(*s1 != '\0'){
        char *p1 = s1;
        char *p2 = s2;
        while(*p2 != '\0'){
            if(*p2 != *p1) break;
            *p1++;
            *p2++;
        }
        if(*p2 == '\0') return s1;
        s1++;
    }
    return NULL;
}

int main(){
    char s[105] = "anjfkdwgdxyzcs";
    char d[205] = "xyz";
    cout << strlen(s) << endl;
    cout << strcpy(d, s) << endl;
    cout << strcat(s, d) << endl;
    cout << strcmp(s, d) << endl;
    cout << strstr(s, d) << endl;
    return 0;
}

string应用

  1. P1597 语句解析
  2. P1781 宇宙总统
#include<iostream>
#include<string>
using namespace std;
int main(){
    int n;
    cin >> n;
    string max_s = "0";
    int idx;
    for(int i = 1; i <= n; i++){
        string s;
        cin >> s;
        if(s.size() > max_s.size()){
            max_s = s;
            idx = i;
        }else if(s.size() == max_s.size()){
            if(s > max_s){
                idx = i;
                max_s = s;
            }
        }
    }
    cout << idx << endl << max_s;
    return 0;
}
  1. P1553 数字反转(升级版)

注意 : * 0 和‘0’(判断字符'0'切勿写成数字0) * 小数点后要特殊处理(先除0后翻转),其他都按整数处理(先翻转后除0) * 为了减低编写复杂度, 使函数接口统一化

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

string rmv_pre_zero(string s){
    int i = 0;
    while(i < s.size() - 1 && s[i] == '0'){
        i++;
    }
    return s.substr(i);
}

string reverse(string s){
    reverse(s.begin(), s.end());
    return s;
}

int main(){
    string s;
    cin >> s;
    if(s.find('.') != string :: npos){
        int pos = s.find('.');
        cout << rmv_pre_zero(reverse(s.substr(0, pos))) + '.' + reverse(rmv_pre_zero(s.substr(pos + 1)));
    }else if(s.find('/') != string :: npos){
        int pos = s.find('/');
        cout << rmv_pre_zero(reverse(s.substr(0, pos))) + '/' + rmv_pre_zero(reverse(s.substr(pos + 1)));
    }else if(s.find('%') != string :: npos){
        cout << rmv_pre_zero(reverse(s.substr(0, s.size() - 1))) + '%'; 
    }else{
        cout << rmv_pre_zero(reverse(s));
    }
    return 0;
}
  1. P1012 [NOIP1998 提高组] 拼数
  2. P1101 单词方阵
  3. P1203[USACO1.1] 坏掉的项链 Broken Necklace
# 体会一下python的简洁,注:python的注释是 '#',不是 '//'
def solve(s):
    if s.isdigit():
        print(int(s[::-1]))
    else:
        for c in ['%', '.', '/']:
            if c in s:
                i = s.index(c)
                s1 = s[:i]
                print(str(int(s1[::-1]))+s[i],end="")
                if c != '%':
                    s2 = s[i+1:].strip('0')
                    if len(s2) == 0: s2='0'
                    print(int(s2[::-1]))


s = input()
solve(s)
678000.00983
876.389