Hi, may I know if there is an easy way to convert a string into lowercase except for certain parts.

Input: I HaVe A CaT caLLed "SaLly" AnD DoG called "BeLLY"

Output: i have a cat called "SaLly" and dog called "BeLLY"

I do not want the stuff inside the quotes to be converted to lowercase but the rest needs to be. Thanks a lot.

Recommended Answers

All 5 Replies

Yes there is an easy way.

Hint: substr() and find()

Hint: substr() and find()

You mean find first occurrence of " and then substring until " to convert to lowercase. Then find next occurrence of " and set that index to be the start of a substring. And then repeat the whole process until no more " is found ?

issit possible to use some STL or something to do it ? Would it be faster ?

There are many ways to accomplish this...Here's a very simple way

#include <iostream>
#include <string>
#include <cctype>

int main()
{
  const char S_CHAR = '\"';
  bool start_char = false;
  
  std::string the_str("I HaVe A CaT caLLed \"SaLly\" AnD DoG called \"BeLLY\"");
  
  std::string::iterator begin = the_str.begin();
  std::string::iterator end = the_str.end();
  
  while (begin != end)
  {
    if (*begin == S_CHAR)
      start_char =! start_char;
    
    if (!start_char)
      *begin = tolower(*begin);
    
    ++begin;
  }
  
  std::cout << the_str << std::endl;

  return 0;
}
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.