Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how can you compare two strings punctuation insensitively?

I am busy making a spell checker and when I compare the word I’m to the dictionary version im it returns false. I used strcasecmp to compare them case insensitively but the apostrophe is still a problem. How can I compare the two (punctuation insensitively) and get true as the output?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Write your own routine to skip punctuation:

#include <stdio.h>
#include <ctype.h>

int insenistive_strcmp(char const* s1, char const* s2)
{
    unsigned char const* p1 = (unsigned char const*)s1;
    unsigned char const* p2 = (unsigned char const*)s2;

    for (;; ++p1, ++p2)
    {
        while (ispunct(*p1))
            ++p1;

        while (ispunct(*p2))
            ++p2;
        
        int ch1 = toupper(*p1);
        int ch2 = toupper(*p2);

        if (!ch1 || !ch2 || ch1 != ch2)
            return ch1 - ch2;
    }
}

int main()
{
    printf("%d\n", insenistive_strcmp("I'm", "im"));
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading