从字符串中提取数字,然后按长度和字典序排列

任意一串字符串 字符串里包含数字部分和一般的字符

例如 ad2ef35adx1wewe76
注意这个字符串 里面有4个数字 分别是 1 2 35 76 不考虑大数

将数字按照从小到大排序 组成一个新的字符串

要求制作一个函数来进行处理

假设是 fun(char*src,char*des)
  {
  }

src 输入字符串 ad2ef35adx1wewe76
des 输出字符串 1-2-35-76
第一步,建立一个缓冲,删除所有的字母
留下 2-35-1-76 这个应该没什么难度的.
再一步 使用 strtok ,
如果可以使用容器就比较简单,往容器里塞,然后容器排序
,如果不能使用容器,都要自己造轮子,哪就麻烦一点了

#include <iostream>    // 数据流输入/输出
#include <string>      // 字符串类
#include <algorithm>   // STL 通用算法
#include <vector>      // STL 动态数组容器
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define TOKEN '-'
using namespace std;
bool strsize(const string& i, const string& j);
char* Extraction_sequence(const char* src, char* des);
int main()
{
    char output[100];
    Extraction_sequence("ad2e99f35a0dx1we5465d修正465465w000e76", output);
    printf("%s\n", output);

    return 0;
}

bool strsize(const string& i, const string& j)  // 排序谓语函数
{
    if (i.size() == j.size())   // 如果长度相同,就按字典序
        return (i < j);
    return (i.size() < j.size()); // 默认按长度排
}

char* Extraction_sequence(const char* src, char* des)
{
    char* ret = des;  // 返回指针
    char* buf = new char[strlen(src) + 1];   // 缓冲buf
    char* c_buf = buf;

    // 提取字符串中的数字
    while (*src) {
        if (isdigit(*src)) {
            *c_buf++ = *src;
            if (!isdigit(*(src + 1))) // 数字后面添加 分割标记
                *c_buf++ = TOKEN ;
        }
        src++;
    }
    *(c_buf - 1) = '\0'; // 封闭字符串

    /* 排序数字序列*/
    vector <string> seq;
    vector <string>::iterator iter;
    char* pch;
    pch = strtok(buf, "-");
    while (pch != NULL) {
        seq.push_back(pch);           //2 35 1 76   置入到容器里
        pch = strtok(NULL, "-");
    }
    sort(seq.begin(), seq.end(), strsize); //按数字长度和大小排列
    iter = seq.begin();
    des[0] = '\0'; // des 清零
    while (iter != seq.end()) {
        strcat(des, (iter++)->c_str()) ;
        strcat(des, "-") ;
    }
    des[strlen(des) - 1] = '\0'; // des 消除最后的 标记'-'
    delete[] buf;
    return ret;
}

以下是实现该功能的Python代码: ```python # 读取单词索引表 with open("index.txt", "r") as f: word_list = [word.strip().lower() for word in f.readlines()] # 读取英文文章 with open("in.txt", "r") as f: content = f.read() # 提取出所有的单词 import re words = re.findall(r"[a-zA-Z]+", content) # 检查单词是否在单词索引表出现过,若未出现则写入error.txt with open("error.txt", "w") as f: for word in sorted(set([word.lower() for word in words])): if word not in word_list: f.write(word + "\n") ``` 首先,我们使用`with open()`语句读取单词索引表和英文文章。在读取单词索引表时,我们将每个单词都转换为小写字母,并存储在`word_list`列表。在读取英文文章时,我们将其全部读入并存储在`content`字符串。 然后,我们使用正则表达式提取出所有的单词,并将它们转换为小写字母。这里使用了`re.findall()`函数,该函数可以从字符串提取出所有符合正则表达式的子串,并返回一个列表。 最后,我们对所有单词进行检查,如果某个单词未出现在单词索引表,则将其写入`error.txt`文件。在写入文件时,我们使用`sorted()`函数对所有出错的单词按字典序排序,并使用`set()`函数去除重复的单词。这样可以保证输出的每个单词只出现一次,并且按字典序排序。 注意,为了避免大小写问题,我们在比较单词是否出现在单词索引表时,将所有单词都转换为小写字母。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值