Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

char

equals

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;
}
like image 569
user3369592 Avatar asked Sep 16 '14 04:09

user3369592


1 Answers

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] )
like image 195
Vlad from Moscow Avatar answered Sep 30 '22 07:09

Vlad from Moscow