Case-Insensitive C-String Comparison

How can I implement a case-insensitive string comparison using C-style strings?

Implementing a case-insensitive string comparison for C-style strings requires a custom function, as the standard strcmp() is case-sensitive. We'll create a function that compares strings character by character, converting each to lowercase before comparison.

Here's an implementation:

#include <cctype>
#include <cstring>
#include <iostream>

int strcasecmp(const char* s1, const char* s2) {
  while (*s1 && *s2) {
    int diff{
      std::tolower(*s1) - std::tolower(*s2)
    };  
    if (diff != 0) {
      return diff;
    }
    s1++;
    s2++;
  }
  return std::tolower(*s1) - std::tolower(*s2);
}

int main() {
  const char* str1{"Hello"};
  const char* str2{"hElLo"};
  const char* str3{"World"};

  std::cout << "Comparing "
    << str1 << " and " << str2
    << ": " << strcasecmp(str1, str2) << '\n';
  std::cout << "Comparing "
    << str1 << " and " << str3
    << ": " << strcasecmp(str1, str3) << '\n';
}
Comparing Hello and hElLo: 0
Comparing Hello and World: -15

Let's break down the strcasecmp() function:

  1. We use a while loop to iterate through both strings simultaneously.
  2. For each character, we use std::tolower() to convert to lowercase before comparing.
  3. If we find a difference, we return it immediately.
  4. If we reach the end of one or both strings, we compare the last characters (which might be null terminators).

This function returns 0 for equal strings, a negative value if s1 is lexicographically before s2 (ignoring case), and a positive value otherwise.

Note that this implementation is not optimal for large strings or frequent comparisons, as it converts each character to lowercase on every comparison. For better performance in such scenarios, you might want to consider creating lowercase copies of the strings first, or using a lookup table for case conversion.

Also, be aware that this simple implementation doesn't handle locale-specific case conversions. For more complex scenarios involving different languages or Unicode, you'd need to use more sophisticated libraries or techniques.

Working with C-Style Strings

A guide to working with and manipulating C-style strings, using the library

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Converting C-style Strings to std::string
How can I efficiently convert a C-style string to a std::string in C++?
Thread Safety in C-String Functions
Are there any thread-safety concerns when using functions from the library?
Safely Resizing C-Style Strings
How can I safely resize a C-style string without causing buffer overflow?
Searching for Substrings in C-Strings
How can I efficiently search for a substring within a C-style string?
Efficient C-String Concatenation
What's the most memory-efficient way to concatenate multiple C-style strings?
Implementing a Circular Buffer with C-Strings
How can I implement a circular buffer using C-style strings?
Safely Passing C-Strings Between Threads
How can I safely pass C-style strings between threads in a multithreaded application?
Implementing a Basic Spell-Checker
How can I implement a basic spell-checker using C-style strings and a dictionary?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant