0

here i have a string but i need to remove the lower case letter only i.e., mi.

primitive code is:

bool check(char c) { return !(std::isdigit(c) ||std::isalpha(c)); }

int main() {

    std::string  str = "m_ivecCadCurveEdges_Pattern";

    str.erase(std::remove_if(str.begin(), str.end(), check),str.end());

    for (std::string::size_type i=0; i<str.length(); ++i)
    {
       //if (!std::islower(str.at(i) && str.at(i) != '_')
       if (!std::islower(str.at(i)))
        {
            str.erase(0, i);
            break;
        }
    }

    if (std::isdigit(str.at(0)))
        str= "N" + str;

    std::cout<<str;
}

I want to break the loop once it reach B in the string.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
shivi
  • 5
  • 6
  • 2
    Use the [*erase-remove idiom*](http://en.wikipedia.org/wiki/Erase-remove_idiom). – juanchopanza Apr 17 '14 at 12:12
  • ... such as [this example](http://stackoverflow.com/questions/6319872/how-to-strip-all-non-alphanumeric-characters-from-a-string-in-c/6319898#6319898) of such usage here on SO. – WhozCraig Apr 17 '14 at 12:14
  • User [remove_if](http://www.cplusplus.com/reference/algorithm/remove_if/) followed by erase. – Mohit Jain Apr 17 '14 at 12:15
  • 1
    @juanchopanza not sure wether it'll work in case of strings properly coz the removal is not in d way i want.. – shivi Apr 17 '14 at 12:17
  • @shivi Then you should explain what you want better. Read the title of the question again. – juanchopanza Apr 17 '14 at 12:20

2 Answers2

0

Looking at the title I suggest you use a combination of remove_if and islower. However the question body implies that you may need to remove only the longest prefix of lower case characters. If that is the case you will need a combination of std::string#erase and find_if for isupper.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
0

erase-remove is good if you wanted the whole string processed, but you ask to have only the leading lower-case character changed. For that, just change your "if" code:

if (!std::islower(str.at(i))
{
    str.erase(0, i);
    break;
}

Your cout should be after the for loop.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
  • thanks its working.. but in case of string like "m_ivecCadCurveEdges_Feature" ... it just removing the m..:( ny idea..?? – shivi Apr 17 '14 at 12:27
  • @shivi you just need to edit the `if` statement to catch the first character you don't want removed, so if you want to get rid of underscores too: `if (!std::islower(str.at(i) && str.at(i) != '_') { ...`. – Tony Delroy Apr 17 '14 at 12:31
  • @shivi "weird things" is not enough information for me to help. Why don't you post your complete current program on ideone.com then copy/paste the URL here so I can see what you're doing, or append it to your question? (you should leave the current code there so people can see what you've originally asked and understand the answers) – Tony Delroy Apr 17 '14 at 12:44
  • @Tonyd but this will have much greater complexity than the solution proposed in my answer. Performing a find_if and erase will be linear, while your solution will be linear times the length of the prefix – Ivaylo Strandjev Apr 17 '14 at 12:55
  • 1
    @IvayloStrandjev actually, my code is optimal... it searches linearly, which is necessary, then issues one erase call for the leading section of the string, which will copy each of the remaining characters from their original position directly to their final position. The second part of your answer (after you realised it was the leading bit that needed erasing) does the same processing but using find_if rather than shlvl's hand-coded loop... no difference - but !islower is not the same as isupper so your logic there's a bit broken, but then shlvl hasn't clarified the leading chars to erase. – Tony Delroy Apr 17 '14 at 13:08
  • @shlvl: you can't just combine different people's solutions. The erase/remove_if suggestion from juanchopanza was based on your original question title, which did not describe your actual requirement, so remove that completely. Then, you've commented out the code I gave you above... why? – Tony Delroy Apr 17 '14 at 13:13