What is the difference between a character and a string in C++?
A character (char) holds a single letter or symbol, while a string (std::string) holds a sequence of characters, like a word or sentence.
Click to reveal answer
beginner
Can you compare a char and a std::string directly using == in C++?
No, you cannot directly compare a char and a std::string with == because they are different types. You must convert the char to a string or compare the char to a single character in the string.
Click to reveal answer
beginner
How do you compare a single character to the first character of a std::string?
You can compare a char variable to the first character of a string using: if (myChar == myString[0]) { ... }
Click to reveal answer
intermediate
What happens if you compare a char to a string literal using == in C++?
Comparing a char to a string literal (like 'a' == "a") is invalid because 'a' is a char and "a" is a const char array (string literal). They are different types and cannot be compared directly.
Click to reveal answer
intermediate
How can you convert a char to a std::string in C++?
You can convert a char to a std::string by using the string constructor: std::string s(1, myChar);
Click to reveal answer
Which type holds a single character in C++?
Achar
Bstd::string
Cint
Dfloat
✗ Incorrect
A char holds a single character, while std::string holds multiple characters.
What is the correct way to compare a char 'c' to the first character of a string 'str'?
Aif (c == str)
Bif (c == str.compare())
Cif (c == str[0])
Dif (c == str.length())
✗ Incorrect
You compare the char to the first character of the string using indexing: str[0].
Can you directly compare a char and a std::string using ==?
AOnly if the string has one character
BNo, they are different types
CYes, always
DOnly if the char is lowercase
✗ Incorrect
Direct comparison is invalid because char and std::string are different types.
How do you convert a char to a std::string?
Astd::string s(1, c);
Bstd::string s = c;
Cchar s = std::string(c);
DYou cannot convert char to string
✗ Incorrect
Use the string constructor with count 1 and the char to create a string.
What type is a string literal like "a" in C++?
Aint
Bchar
Cstd::string
Dconst char array
✗ Incorrect
String literals are const char arrays, not std::string or char.
Explain how to compare a single character with a string in C++.
Think about the types and how to access characters inside a string.
You got /4 concepts.
Describe why comparing a char to a string literal directly is invalid in C++.
Consider the types involved and what a string literal really is.
You got /4 concepts.
Practice
(1/5)
1.
Which of the following correctly compares a character ch with another character 'a' in C++?
easy
A. if (ch == string('a'))
B. if (ch == "a")
C. if (ch == 'a')
D. if (ch == char("a"))
Solution
Step 1: Understand character vs string literals
Characters use single quotes like 'a', strings use double quotes like "a".
Step 2: Compare character with character
Comparing ch (char) with 'a' (char) is valid and correct.
Final Answer:
if (ch == 'a') -> Option C
Quick Check:
char == char comparison works directly [OK]
Hint: Use single quotes for chars, double quotes for strings [OK]
Common Mistakes:
Using double quotes to compare char and string
Trying to convert char to string incorrectly
Confusing char and string types
2.
Which of the following is the correct syntax to compare a char variable c with a std::string variable s in C++?
easy
A. if (c == s)
B. if (string(1, c) == s)
C. if (c == s[0])
D. if (c == char(s))
Solution
Step 1: Recognize type mismatch
Directly comparing char and std::string is invalid.
Step 2: Convert char to string for comparison
Use string(1, c) to create a string from the char, then compare with s.
Final Answer:
if (string(1, c) == s) -> Option B
Quick Check:
Convert char to string before comparing with string [OK]
Hint: Convert char to string with string(1, c) before comparing [OK]
Common Mistakes:
Comparing char directly with string
Trying to cast string to char
Using s[0] without checking string length
3.
What will be the output of the following C++ code?
Comparing char and std::string directly is not allowed in C++.
Step 2: Check compilation rules
This causes a compilation error because no operator== exists for char and string.
Final Answer:
Compilation error -> Option A
Quick Check:
char == string causes compile error [OK]
Hint: char and string can't be compared directly; causes compile error [OK]
Common Mistakes:
Assuming char and string compare equal
Expecting runtime error instead of compile error
Ignoring type mismatch in comparison
4.
Identify the error in the following code snippet and choose the correct fix:
char c = 'b';
std::string s = "b";
if (c == s[0]) {
std::cout << "Match";
}
medium
A. No error, code works correctly
B. Change s[0] to s
C. Convert c to string before comparison
D. Use c == s.at(1) instead
Solution
Step 1: Understand types in comparison
c is a char, s[0] is a char (first character of string).
Step 2: Check if comparison is valid
Comparing two chars is valid and works correctly.
Final Answer:
No error, code works correctly -> Option A
Quick Check:
char == s[0] compares chars correctly [OK]
Hint: Compare char with s[0] to compare characters safely [OK]
Common Mistakes:
Trying to compare char with whole string
Using s.at(1) which may be out of range
Converting char to string unnecessarily
5.
You want to check if a char variable ch matches any character in a std::string variable word. Which of the following is the best way to do this in C++?
hard
A. if (ch == word)
B. if (word == string(1, ch))
C. if (ch == word[0])
D. if (word.find(ch) != std::string::npos)
Solution
Step 1: Understand the goal
We want to check if ch appears anywhere inside word.
Step 2: Use string find method
word.find(ch) returns position of ch or std::string::npos if not found.
Step 3: Check if found
If result is not npos, ch is in word.
Final Answer:
if (word.find(ch) != std::string::npos) -> Option D
Quick Check:
Use string::find to check char in string [OK]
Hint: Use word.find(ch) != npos to check char in string [OK]
Common Mistakes:
Comparing whole string to single char
Checking only first character with word[0]
Converting char to string and comparing whole string
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.