String Compare Function

I guess i should use a now topic to describe the problem

That's what i wrote:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

int main() 
{
	char text[128];
	cin.getline(text, 128);

	char abc[]="ABC";

	if (myStrCmp(text, abc)==0)
	{
		cout<<"Your string equals ABC"<<endl<<endl;
	}
	else 
	{
		cout<<"Your string does not equal ABC"<<endl<<endl;
	}
}

int myStrCmp(const char text[], const char abc[])
{
	int i;
	int res;
	int temptext,tempabc;
	for(i = 0; abc[i] != '\0';i++)
	{
		temptext=text[i];
		tempabc=abc[i];
		res=tempabc-temptext;
		if (res!=0)
			return 1;
	}
	return 0;
}


The problem is if the Input has longer length such as ABCD, since it only checked the string length of ABC, it won't know there is a 4th one behind,
The result will be "Your string equals ABC", which is wrong.
1
2
3
4
5
6
7
int myStrCmp(const char text[], const char abc[])
{
    int i = 0;
    while (text[i] && abc[i] && text[i] == abc[i])
        ++i;
    return text[i] == abc[i] ? 0 : (text[i] > abc[i] ? 1 : -1);  
}


the while loop is used to skip all of the similar characters, and stop if it gets to the end of any string. Then simply return the comparison of the current different chars.

Ex:
"Ab"
"ABC"
int i will stop at 1, and myStrCmp will return 1 because 'b' > 'B'

"ABCdE"
"ABCDE"
int i will stop at 3, and myStrCmp will return 1 because 'd' > 'D'

"AB"
"ABC"
int i will stop at 2, and myStrCmp will return -1 because '\0' < 'C'

"ABC"
"ABC"
int i will stop at 3, and myStrCmp will return 0 because '\0' == '\0'

"ABCD"
"ABC"
int i will stop at 3, and myStrCmp will return 1 because 'D' > '\0'
i can't change in const, I have to make temp to store values, and i think it will cause some initialization warnings from compiler.

And could you simplify the return part, i haven't learn that kind return yet. I am a very new beginner :\
text[i] and abc[i] only access character at index i, they don't change text and abc. You don't need an extra variable temp.

1
2
3
4
5
temptext=text[i];
tempabc=abc[i];
res=tempabc-temptext;
if (res!=0)
    return 1;


can be rewritten as
1
2
if (text[i]!=abc[i])
    return 1;


the tenary operator ? : is if else clause simplified.

x = a>b ? a : b;

is the same as

1
2
3
4
if (a > b)
    x = a;
else
    x = b;


x = a==b ? 0 : (a>b ? 1 : -1);

is the same as
1
2
3
4
5
6
7
if (a==b)
    x = 0;
else
    if (a>b)
        x = 1;
    else
        x = -1;
Thank you so much! it's working now!

and b/c of my teacher's requirement that we have to test null character in abc[i], so I changed
1
2
3
4
//...
 while (text[i] && abc[i] &&  abc[i]!='0')//changed: text[i] == abc[i]
        ++i;
//... 


It's working.
But i am not sure why it's working as same as text[i] == abc[i]...
Last edited on
What I was aiming for when I made the earlier suggestion:

1
2
3
4
5
6
7
8
9
    int i = 0 ;
    while ( text[i] != '\0'  &&  abc[i] != '\0' )
    {
        if ( text[i] != abc[i] )
            break ;
        ++i ;
    }

    return text[i] == abc[i] ;


Assuming, of course, as your original code indicated the function should return true if they're equal and false if they're not as opposed to the return value required by the C library for the analogous function.

How I would've written the same:

1
2
3
4
5
6
7
    const char* t1 = text ;
    const char* t2 = abc ;

    while ( (*t1 && *t2) && *t1 == *t2 )
        ++t1, ++t2 ;

    return *t1 == *t2 ;
Thank you very much! :)

Can return text[i] == abc[i] ; return as a 0 or 1 or some other numbers? Because the int main is fixed, i can't change.
Last edited on
You could go with

1
2
3
4
    if ( text[i] == abc[i] )
        return 1 ;
    else
        return 0 ;


The compiler should generate the same code. They're equivalent.
1
2
3
4
5
6
7
8
9
    int i = 0 ;
    while ( text[i] != '\0'  &&  abc[i] != '\0' )
    {
        ++i ;
    }
if ( text[i] == abc[i] )
        return 1 ;
    else
        return 0 ;


You mean like this?
Here is a link to Kernighan & Ritchie C Programming:

http://zanasi.chem.unisa.it/download/C.pdf


It's really old (1984), but it is written by the guys who invented C. It has some really good reference material for the C language.

HEre is some of their code:
1
2
3
4
5
6
7
8
9
/* strcmp: return <0 if s<t, 0 if s==t, >0 if s>t */
int strcmp(char *s, char *t)
{
int i;
for (i = 0; s[i] == t[i]; i++)
     if (s[i] == '\0')
           return 0;
return s[i] - t[i];
}


This is the pointer version:
1
2
3
4
5
6
7
8
9
10
11
12
/* strcmp: return <0 if s<t, 0 if s==t, >0 if s>t */
int strcmp(char *s, char *t)
{
for ( ; *s == *t; s++, t++)
     if (*s == '\0')
         return 0;
return *s - *t;
}

Hope this is not cheating too much !!!!


Thank you, IdeaMan! :)
No worries, pleased to help.

I should point out that that book is written with ANSI C, there have been quite a few changes to the standard since then, but their code should still work.

Good Luck !!!!

Topic archived. No new replies allowed.