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