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.

7 Years
Discussion Span
Last Post by gerard4143

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);
  std::cout << the_str << std::endl;

  return 0;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.