0

I have to compare 2 char strings... ignoring the whitespaces in it, so e.g:

int cmp("a b c ", "abc") == 0;

if both are same , return 0;

else if s1 is bigger than s2, return 1; else return -1; e.g:
int cmp(" aaab", "aaa") == 1;

int cmp("aaa" , "aa  ab") == -1;

how can I realise this with passing the strings as pointers and with pointerarithmetics ?

#include <iostream>

using namespace std;

int strcmp_ign_ws(const char * s1, const char * s2) {
    int count1(0);
    int count2(0);  

    while (*s1 != '\0' || *s2 != '\0') {            

    if (*s1 == '\0') //if s1 finished, do nothing
    {
        continue;
    }
    if (*s2 == '\0') //if s2 finished, do nothing
    {
        continue;
    }

    if ( *s1 == ' ' ) {
        s1++;           //if whitespace, go on to next char
    }

    if (*s2 == ' ') {
        s2++;           //if whitespace, go on to next char
    }

    if (*s1 == *s2) {  //if same chars, increase counters;go to next char
        s1++;
        s2++;
        count1++;
        count2++;
    }
    if (*s1 > *s2) {
        count1++;
        s1++;
        s2++;
    }
    if (*s1 < *s2) {
        count2++;
        s1++;
        s2++;
    }
    /**
        while (*s1 == *s2) {
            if (*s1 == 0) 
            {
                return 0;
            }
            s1++;
            count1++;
            s2++;
            count2++;
        }**/


}
    return (count1 - count2);

}

int main() {

char a[] = "Hallo Welt!!!";
char b[] = "Hallo Welt";

int result(0);

result = strcmp_ign_ws(a,b);

cout << result << endl;

return 0;

}

EDIT: I may only use strlen , no other inbuilt functions... or strings

MMMM
  • 3,320
  • 8
  • 43
  • 80

2 Answers2

3

At the first step, replace those two first continue by break. Otherwise it goes to an infinite loop. You're increment-ing those pointers in the loop but once check them against \0. To skip white space you need to use inner loops too.

Based on this strcmp implementation, I've made some changes and then:

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

    while (*p1)
    {
        while (isspace(*p1)) p1++;
        if (!*p1) break;

        while (isspace(*p2)) p2++;
        //if (!*p2) break;

        if (!*p2) return  1;
        if (*p2 > *p1) return -1;
        if (*p1 > *p2) return  1;

        p1++;
        p2++;
    }

    if (*p2) return -1;

    return 0;
}
Community
  • 1
  • 1
masoud
  • 55,379
  • 16
  • 141
  • 208
1

The usual idea for this algorithm is:

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

  while (true) {
    while (*p1 != '\0' && isspace((unsigned char)*p1)) p1++;
    while (*p2 != '\0' && isspace((unsigned char)*p2)) p2++;
    if (*p1 == '\0' || *p2 == '\0') {
      return (*p2 == '\0') - (*p1 == '\0');
    }
    if (*p1 != *p2) {
      return (unsigned char)*p2 - (unsigned char)*p1;
    }
    p1++;
    p2++;
  }
}
Roland Illig
  • 40,703
  • 10
  • 88
  • 121
  • thanks that worked for me, but what if I now want to compare the content of the chars with eachother ? lets say aac and aab , => aac>aab => return 1 ?? – MMMM Nov 16 '13 at 10:36
  • I don't understand your question. Comparing the characters is exactly what the last `if` condition does. – Roland Illig Nov 16 '13 at 15:38
  • the last if case is never being executed, is that right? looks like poor code... – MMMM Dec 01 '13 at 18:43