I am playing around manipulating strings and currently I want to take a full name typed in by a user eg. "Mike W. Heintzman" and then take three letters input by the user, eg. M I E and then I want it to find all of those instances in the name. I have upto this point complete. What I cannot figure out how to do is this next part. I want to take the information from above and delete those characters print out the name without the given letters and then reinsert those letters into their previous position this is the code I have so far. Im not looking for an answer I just need help getting in the ready right direction.

//define the lettersearch function
string letterSearch(string fullName) {
    //create an array to hold the users entered variables
    char userLetters[3];
    //create a var to hold the letter positions
    int found;
    //Create a multi dimensional array to hold the letters removed and their posion the first
    int letterPos[3][2];
    //make full name all lowercase since C++ is a case senseative language
    fullName = strLowerCase(fullName);

    //tell the user to enter letters
    cout << "\n Please enter three letters that are in your name.\n This search is case insensitive. " << endl;
    //create a for loop to fill the array with answers
    for (int i = 0; i < 3; i++){

        //set the array with i
        cin >> userLetters[i];
    }

    system("CLS");
    //create a for loop to check for individual letters in the string fullname
    for (int i = 0; i < 3; i++){
        //set the search make make sure everything is lowercase
      found = fullName.find(tolower(userLetters[i]));
      //continue in this while loop untill there are not more instances of a given letter
        while (found != string::npos){
            //display what letter has been found and in what position
             cout << "\n The letter " << userLetters[i] << " has been found at " << found << endl;
             letterPos[i][0] = found;
             char test = atoi(userLetters[i]);
             letterPos[i][1] = test;
             //continue the search from the last position
              found=fullName.find(tolower(userLetters[i]),found+1);
        }

    }


    return "test";
    }

Depending on what you can use I would use a map to store the posistion and the charecter that needs to be deleted. then you can use the map to delete the unwanted values and to restore them as well. I have implemeted a verxion of this that does work for the case you gave but I havent fully tested it. This should help you to see how everything is flowing. If you have a good debugger I would suggest stepping through the code to see what it is doing.

#include <iostream>
#include <string>
#include <map>

using namespace std;

int main()
{
    string name = "Mike W. Heintzman";
    string deleters = "mie";
    map <int, char> erased;

    cout << deleters << endl;
    cout << name << endl;
    // find all of the leters needed to be deleted and get the pos
    for(size_t i = 0; i < name.size(); i++)
    {
        for(size_t j = 0; j < deleters.size(); j++)
        {
            if(tolower(name[i]) == deleters[j])
                erased.insert(pair<int, char>(i, name[i]));
        }
    }
    // remopve the leters.  this will need to be done in revers order to preserve indexes
    map<int, char>::reverse_iterator rit = erased.rbegin();
    map<int, char>::reverse_iterator rend = erased.rend();
    while(rit != rend)
    {
        name.erase(rit->first, 1);
        ++rit;
    }

    cout << name << endl;

    // put leters back
    map<int, char>::iterator it = erased.begin();
    map<int, char>::iterator end = erased.end();
    while(it != end)
    {
        name.insert(it->first, 1, it->second);
        ++it;
    }

    cout << name;

    cin.get();
    return 0;
}

That worked perfect man never heard of maps before now thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.