Bird
Raised Fist0
C++programming~20 mins

Character vs string comparison in C++ - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Character vs String Comparison Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of character and string comparison
What is the output of this C++ code snippet?
C++
#include <iostream>
#include <string>

int main() {
    char c = 'a';
    std::string s = "a";
    if (c == s) {
        std::cout << "Equal" << std::endl;
    } else {
        std::cout << "Not Equal" << std::endl;
    }
    return 0;
}
ACompilation error
BNot Equal
CEqual
DRuntime error
Attempts:
2 left
💡 Hint
Think about the types being compared: char and std::string.
❓ Predict Output
intermediate
2:00remaining
Comparing char with string using c_str()
What is the output of this C++ code?
C++
#include <iostream>
#include <string>
#include <cstring>

int main() {
    char c = 'a';
    std::string s = "a";
    if (std::strcmp(&c, s.c_str()) == 0) {
        std::cout << "Equal" << std::endl;
    } else {
        std::cout << "Not Equal" << std::endl;
    }
    return 0;
}
ARuntime error
BNot Equal
CEqual
DCompilation error
Attempts:
2 left
💡 Hint
Consider what &c points to and if it is a proper C-string.
❓ Predict Output
advanced
2:00remaining
Output of comparing char and string with explicit conversion
What is the output of this C++ code?
C++
#include <iostream>
#include <string>

int main() {
    char c = 'a';
    std::string s = "a";
    if (std::string(1, c) == s) {
        std::cout << "Equal" << std::endl;
    } else {
        std::cout << "Not Equal" << std::endl;
    }
    return 0;
}
ANot Equal
BRuntime error
CCompilation error
DEqual
Attempts:
2 left
💡 Hint
Look at how the char is converted to a string before comparison.
❓ Predict Output
advanced
2:00remaining
Comparing char and string with operator== overload
What is the output of this C++ code?
C++
#include <iostream>
#include <string>

int main() {
    char c = 'a';
    std::string s = "a";
    if (c == s[0]) {
        std::cout << "Equal" << std::endl;
    } else {
        std::cout << "Not Equal" << std::endl;
    }
    return 0;
}
ARuntime error
BEqual
CCompilation error
DNot Equal
Attempts:
2 left
💡 Hint
Check what s[0] returns and how it compares to c.
🧠 Conceptual
expert
2:00remaining
Why direct char and string comparison fails in C++
Why does the following code fail to compile in C++? char c = 'a'; std::string s = "a"; if (c == s) { // ... } Choose the best explanation.
ABecause char variables cannot be used in if statements.
BBecause std::string cannot be compared with any other type using operator==.
CBecause char and std::string are different types and no operator== overload exists for comparing them directly.
DBecause the compiler requires explicit casting of char to int before comparison.
Attempts:
2 left
💡 Hint
Think about type compatibility and operator overloading in C++.

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

  1. Step 1: Understand character vs string literals

    Characters use single quotes like 'a', strings use double quotes like "a".
  2. Step 2: Compare character with character

    Comparing ch (char) with 'a' (char) is valid and correct.
  3. Final Answer:

    if (ch == 'a') -> Option C
  4. 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

  1. Step 1: Recognize type mismatch

    Directly comparing char and std::string is invalid.
  2. Step 2: Convert char to string for comparison

    Use string(1, c) to create a string from the char, then compare with s.
  3. Final Answer:

    if (string(1, c) == s) -> Option B
  4. 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?

char ch = 'x';
std::string str = "x";
if (ch == str) {
    std::cout << "Equal";
} else {
    std::cout << "Not Equal";
}
medium
A. Compilation error
B. Equal
C. Not Equal
D. Runtime error

Solution

  1. Step 1: Analyze comparison types

    Comparing char and std::string directly is not allowed in C++.
  2. Step 2: Check compilation rules

    This causes a compilation error because no operator== exists for char and string.
  3. Final Answer:

    Compilation error -> Option A
  4. 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

  1. Step 1: Understand types in comparison

    c is a char, s[0] is a char (first character of string).
  2. Step 2: Check if comparison is valid

    Comparing two chars is valid and works correctly.
  3. Final Answer:

    No error, code works correctly -> Option A
  4. 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

  1. Step 1: Understand the goal

    We want to check if ch appears anywhere inside word.
  2. Step 2: Use string find method

    word.find(ch) returns position of ch or std::string::npos if not found.
  3. Step 3: Check if found

    If result is not npos, ch is in word.
  4. Final Answer:

    if (word.find(ch) != std::string::npos) -> Option D
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard