删除字符串的子字符串

时间:2015-08-26 13:36:01

标签: string c++11 substring erase

我有问题需要解决。我必须创建重现2个参数的函数,一个是字符串类型,另一个是int。我必须输入句子,然后输入一个数字。我的程序必须删除该数字相等的单词。我写了很多代码,但是我有问题,例如,如果我输入第一个,或者如果我的句子在单词之间包含很多空格,它就不会删除它们。我不能使用迭代器。 这是我的代码。

#include <iostream>
#include <string>
#include <algorithm>

int Numberofwords(std::string s)
{
    int trigg (0),suff(0);

    int x(0),y(s.length()-1);

    while(s[x] == ' ')
        x++;

    for(int x(y);x>=0;x--)
    {
        if(s[x]==' ') suff++;
        if(s[x] != ' ') break;
    }

    for(;x<s.length()-suff;x++)
    {
        if(s[x] == ' ') trigg++;
        if((s[x] == s[x+1]) && s[x] == ' ') trigg--;
        if((s[x] == ' ' && s[x+1]==' \n')) trigg--;
    }
    trigg++;
    return trigg;
}

std::string Funkcija(std::string recenica, int n)
{
    int br(Numberofwords(recenica)),x(0),y(recenica.length()-1),suff(0),counter(0),trig(0);

    std::string s;

    if(n<1 || n>br) throw "Inputed number must be bigger than 0 and smaller   then number of words in sentence.";

    while(recenica[x] == ' ')
        x++;

    for(int i(y);i>=0;i--)
    {
        if(recenica[i] == ' ') suff++;
        if(recenica[i] != ' ') break;
    }

    int a(x);
    for(;a<recenica.length()-suff;a++)
    {

        if(recenica[a] == ' ') trig++;

        if(trig == (n-1))
        {
            int e(a);
            while(recenica[e+1] != ' ')
            {
                counter++;
                e++;
            }

            recenica.erase(a+1,counter);
        }

    }
    return recenica;
}

1 个答案:

答案 0 :(得分:0)

您可以通过迭代字符串来实现这一目标。

字符串迭代可以用不同的方式完成:(你想要第三种方式,但如果可以的话,我推荐第一种或第二种方式)

// Way #1: Iterators
std::string remove_word_by_number(std::string source, unsigned int word_number)
{
    if ( word_number == 0 )
        throw runtime_error("word_number cannot be 0");

    std::ostringstream out;

    bool word_removed = false;
    bool inside_word = false;
    unsigned int words_parsed = 0;
    for ( std::string::iterator it = source.begin(); it != source.end(); ++it )
    {
        if ( isalnum(*it) )
        {
            if ( !inside_word )
            {
                inside_word = true;
                words_parsed++;
            }
            if ( words_parsed != word_number )
                out << *it;
            else
                word_removed = true;
        }
        else
        {
            inside_word = false;
            out << *it;

            if ( word_removed )
            {
                out << std::string(it, source.end());
                break;
            }
        }
    }

    return out.str();
}

// Way #2: C-style iterators (pointers, not sure if you can use this kind of iterators)
std::string remove_word_by_number(std::string source, unsigned int word_number)
{
    if ( word_number == 0 )
        throw runtime_error("word_number cannot be 0");

    std::ostringstream out;

    bool word_removed = false;
    bool inside_word = false;
    unsigned int words_parsed = 0;
    for ( const char* it = source.data(); it != source.data()+source.size(); it++ )
    {
        if ( isalnum(*it) )
        {
            if ( !inside_word )
            {
                inside_word = true;
                words_parsed++;
            }
            if ( words_parsed != word_number )
                out << *it;
            else
                word_removed = true;
        }
        else
        {
            inside_word = false;
            out << *it;

            if ( word_removed )
            {
                out << source.substr(it-source.data());
                break;
            }
        }
    }

    if ( !word_removed )
        throw runtime_error("word_number out of range");

    return out.str();
}

// Way #3: Using string's operator[]
std::string remove_word_by_number(std::string source, unsigned int word_number)
{
    if ( word_number == 0 )
        throw runtime_error("word_number cannot be 0");

    std::ostringstream out;

    bool word_removed = false;
    bool inside_word = false;
    unsigned int words_parsed = 0;
    for ( std::size_t it = 0; it < source.size(); it++ )
    {
        if ( isalnum(source[it]) )
        {
            if ( !inside_word )
            {
                inside_word = true;
                words_parsed++;
            }
            if ( words_parsed != word_number )
                out << source[it];
            else
                word_removed = true;
        }
        else
        {
            inside_word = false;
            out << source[it];

            if ( word_removed )
            {
                out << source.substr(it);
                break;
            }
        }
    }

    return out.str();
}

希望有所帮助