Link to home
Start Free TrialLog in
Avatar of xterra
xterra

asked on

Is it in quotes???

Hi,
 I have a string variable like this:
     
 This is a "/"

 I'd like to see if there is one backslash that is NOT inside quotes. For example, the above example would return false.  However:

 This is a "/" /
 
Would return true.  The following would also return true:
 This is a test /

So whether or not there is a backslash inside quotes, it doesn't matter.  I just need to know if there is at least one backslash not in quotes.

Thankyou in advance.
ASKER CERTIFIED SOLUTION
Avatar of calebS
calebS

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of calebS
calebS

Sorry, didn't tidy my psudeo code.


stringLength should read myS.length()

ie
bool findSlash(string myS)
{
   bool inQuotes = false;
   bool foundSlash = false;

    for(int ii = 0; ii <myS.length(); ii++)
   {
        if(myS[ii] == '"')    //ie if the char is a "
           inQuotes++;     //Flips the boolean switch
   
        if(myS[ii] == '/')    //ie if the char is a /
           if(!inQuotes)
               foundSlash = true;
   }
   return foundSlash;
}
Hi,

I am using MFC....therefore if I use a " in a string it must be \" so that the compiler doesnt use that " within the string to terminate the full string - eg     \"/\"   is just "/"


      CString  str;
      str  = "This is a \"    \"/   \"/\"    \"/\"";
      int size = str.GetLength();
      for(int x =0; x <= size - 3; x++)
      {
 
            if((str[x] == '"') && (str[x + 1] == '/') && (str[x + 2] == '"'))
                  AfxMessageBox("Found \"/\"! ");
      }
CString is MFC. this compiles fine and works.
Avatar of xterra

ASKER

Thanks for the replies. calebS yours seems to work the best right now.  However, it seems to ignore if there is also a "/" outside the quotes if one has been previously found in the quotes.  For example if theres a "/" inside the quotes, it will return false.  But, the same string might have a "/" outside the quotes as well, which is what is important.


Thanks again.
Hmm, well the logic of the code looks good to me.

Have you tried error testing it?

ie.

put brackets at the various 'if then' statements that will also do a cout.

ie bring to screen the values of the booleans and the current char.

bool findSlash(string myS)
{
  bool inQuotes = false;
  bool foundSlash = false;

   for(int ii = 0; ii <myS.length(); ii++)
  {
       cout << end << myS[ii] << ": ";
        if(myS[ii] == '"')    //ie if the char is a "
            inQuotes++;     //Flips the boolean switch

       if(myS[ii] == '/')    //ie if the char is a /
          if(!inQuotes)
              foundSlash = true;

       //for testing only
      if(foundSlash)
           cout << "found outside \t";
      else
           cout << "none found yet\t";
     
      if(inQuotes)
           cout << "(within quotes)";
     

  }
  return foundSlash;
}


That type of testing.

Can you tell that I am doing this at work without the benefit of a compiler!

I may have a look at this when I get home tonight.

Regards,
Cassandra
Avatar of xterra

ASKER

calebS,
 Thanks for the reply!  I tested the second code with the following string, and it still returns false (but it should return true, because there is a / outside the quotes)

this is a test "in quotes / blah blah"    outside quotes /  outside quotes

That should return true, regardless of the slash in quotes, but there is one outside.


And I used your second code.


Thanks in advance,
Robert.