Search code examples
stringc++11substringerase

Deleting substring of your string


I have problem to solve. I must create function that recives 2 parametres, one is string type and other is int. I must input sentence, then one number. My program must delete word that is equal in order to that number. I wrote much of my code, but i have problem, for example if i input number one, or if my sentence contains much blank spaces between words, it does't delete them. I mustn't use iterators. This is my code.

#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;
}

Solution

  • You can achieve that by iterating your string.

    String iteration can be done with different ways: (you want the third way, but if you could, I'd recommend the first or the second)

    // 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();
    }
    

    Hope it helps