Removing specific characters in string

so i have this problem.
a word is written

then all characters of the alphabet that are not in the word,are written after it,in alphabetical order.

so lets say the word is
whatislove
the output would be
whatislovebcdfgjkmnpqruxyz
(i used whatislove cause its a sentence with no repeating letters,since thats my problem)
so i have that sorted,the thing is,no two letters can be repeating.
now,i have an idea on how to do this using the erase function
size_t returns the first place where an object was found,so just erase all other objects that are the same as the one found,after the first place it was found at.
problem is, erase function doesn't take size_t as a par,and i can't figure out what it even needs.I know it can take sting.begin() as a paramater,but other than that i am completely confused.


this is the code i have until now
(alphabetType_ is the number of letters in the alphabbet)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

for(int i = 0; i<alphabetType_;i++){
//tochar converts from int to char 0-a,1-b....
            std::size_t foundAt=keyword.find(toChar(i));
          int a= static_cast<int>(foundAt);

        if(foundAt!=std::string::npos){

              //this is where i think i can fit some code to remove letters in
        }else{

        keyword+=toChar(i);

        }

Last edited on
To remove all entries satisfying some condition use erase-remove idiom:
1
2
3
4
5
6
//alphabet contains all letters of alphabet
alphabet.erase(
    //http://en.cppreference.com/w/cpp/algorithm/remove
    std::remove_if(alphabet.begin(), alphabet.end(), <condition>),
    alphabet.end()
)
 

<condition> is a function which takes char as a parameter and checks if satisfies condition. If you use C++11, use lambdas:
1
2
3
4
5
alphabet.erase(
    std::remove_if(alphabet.begin(), alphabet.end(), [keyword](char c)
        {return keyword.find(c) != std::string::npos;}),
    alphabet.end()
)


Else, use functors:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct is_in
{
    is_in(std::string s) : keyword(s) {}
    bool operator()(char c)
    {
        return keyword.find(c) != std::string::npos;
    }
private:
    std::string keyword;
}

//...
alphabet.erase(
    std::remove_if(alphabet.begin(), alphabet.end(), is_in(keyword)),
    alphabet.end()
)
Last edited on
but i don't need to erase the character the first time it appears,only its later appearances in the string,that is my problem
I do not get you. You need:
get the keyword
output the keyword (without changing it)
output modified alphabet string immediately after
 
As there is only one entry of each letter in alphabet, there is no first, or last, characters. Each character is unique.

Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include <algorithm>


int main()
{
    std::string alphabet = "abcdefghijklmnopqrstuvwxyz";
    std::string keyword  = "sabbatical";
    alphabet.erase(
        std::remove_if(alphabet.begin(), alphabet.end(), [&keyword](char c)
            {return keyword.find(c) != std::string::npos;}),
        alphabet.end()
    );
    std::cout << keyword << alphabet;
}
sabbaticaldefghjkmnopqruvwxyz
http://ideone.com/H3kUnu
there can't be two same characters in the string
maybe i didn't word it correctly
there can't be two a,or two bs.only one of each.thats what i'm trying to do.
there can't be two same characters in the string
So what should happen when somebody will enter word with repeating character? Should it reject it? Something else? Some examples of what are you trying to achieve would be nice, as your explanation:
a word is written
then all characters of the alphabet that are not in the word,are written after it,in alphabetical order.
in fact allows repeating letters in original word and requires original word to be outputted as-is without changing it.
the thing is,no two letters can be repeating.
i said that in the op.
i wasn't clear enough,it looked like the assigment ended in the first sentence when that was only part of my explanation.Sorry.

ok
here is what it should look like

input abbbccdd


output
abcdefghijklmnopqrstuwvxyz



so if two letters are repeating leave only one of them,and remove all other instances
So:
1) add alphabet string to keyword string
2) For each character in the new keyword string:
check if it is in the result string. If not, add it to it.
3) Output result.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>


int main()
{
    const std::string alphabet = "abcdefghijklmnopqrstuvwxyz";
    std::string keyword  = "sabbatical";
    keyword += alphabet;
    std::string result;
    for(char c: keyword)
        if(result.find(c) == std::string::npos)
            result.push_back(c);
    std::cout << result;
}
sabticldefghjkmnopqruvwxyz
Topic archived. No new replies allowed.