check if a char is a single quote. c++

20,793

Solution 1

Your code snippet is invalid. Instead of

char mychar;
if(char=='\'')// is that how we check this char is a single quote?
{
  cout<<"here is a quote"<<endl;
}

there must be

char mychar;
if(mychar=='\'')// is that how we check this char is a single quote?
{
  cout<<"here is a quote"<<endl;
}

And moreover object mychar must be initialized.

As for other then indeed you have to use character literal that contains escape symbol of single quote.

Or if you have a string literal like

const char *quote = "'";

then you can write either as

if( mychar == *quote )

or

if( mychar == quote[0] )

Solution 2

Yes. (Assuming you fix the typo where you have char instead of mychar.)

Share:
20,793
user3369592
Author by

user3369592

current getting my master degree in Computer Science.

Updated on July 26, 2020

Comments

  • user3369592
    user3369592 almost 4 years

    I want to check if a char is a single quote. Here is my code.

    char mychar;
    if (mychar == '\'')  // Is that how we check this char is a single quote?
    {
      cout << "here is a quote" << endl;
    }