Thread: String comparison using bitwise logic?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It sounds like you just want to do something like what strcmp does to produce a yes/no result concerning the question of whether the strings are equal. If so, then it could be as simple as:
    Code:
    const char *a = "car";
    const char *b = "carb";
    if (strcmp(a, b) == 0)
    {
        puts("yes");
    }
    else
    {
        puts("no");
    }
    If not, then you need to explain what is this "bitwise logic" and why do you want to use it to "compare char *".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #2
    Registered User
    Join Date
    Jan 2022
    Posts
    7

    Why?

    I just don't want to use any built-in functions. Also, want to learn something.

    Otherwise, I can just use if (a == b).

  3. #3
    Registered User
    Join Date
    Oct 2019
    Posts
    82
    Quote Originally Posted by tibegato View Post
    I just don't want to use any built-in functions. Also, want to learn something.

    Otherwise, I can just use if (a == b).
    No, that way you would be comparing pointers. A pointer is equal to the other point if they point to the same memory location.

    Ha! I quickly put together an implementation of strcmp that even I do not have a very clear idea of how it works o.O

    Code:
    include <stdio.h>
    
    
    int strcmp(const char *a, const char *b)
    {
        while(*a++ == *b++)
    
    
        return *a - *b;
    }
    
    
    int main(int argc, char *argv)
    {
        char *first = "Carb";
        char *second = "Carb";
    
    
        if (strcmp(first, second) == 0)
        {
            printf("Equal\n");
        }else
        {
            printf("Not equal\n");
        }
    
    
        return 0;
    }
    I'm honestly not sure how the the while condition gets evaluated such that the loop comes to an end at the end of the string(s). Butt, it seems to work.

    (Probably not the best of answers there could be, but I contributed to this thread too hoping to learn).
    Last edited by ghoul; 04-11-2022 at 11:13 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. std::string comparison versus int comparison
    By leeor_net in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2009, 07:28 AM
  2. String Comparison
    By kpreston in forum C Programming
    Replies: 16
    Last Post: 10-20-2008, 07:43 PM
  3. string comparison
    By sj999 in forum C Programming
    Replies: 4
    Last Post: 05-23-2008, 03:10 AM
  4. please help with a string comparison.
    By MegaManZZ in forum C Programming
    Replies: 5
    Last Post: 02-18-2008, 01:33 PM
  5. Bitwise & logic operations...
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-26-2001, 08:18 PM

Tags for this Thread