How can I compare 2 strings without using C library function? I'm confused on how to code that "aardvark" is "greater than" "bog" for example. I need to bubble sort a 2D array of strings by comparing their chars...which is my problem. Do I use strlen() to compare the string lengths and then compare the chars? Please help because I'm kind of confused. Thanks
3 Answers
Use strcmp("aardvark","bog");
This returns 0 if they are the same, a negative number if aardvark is less than bog (which it is) and a positive number if it is greater.
Here is a link to the documentation:
http://www.cplusplus.com/reference/cstring/strcmp/
The way "less than" or "greater than" is calculated relies not on string length but on an ASCII table. A is less than b, so aardvark is less than bog. Salamander is greater than salad because they match up until the m/d character by character, until you compare the m and d. M comes after d. So it returns a positive number if salamander is your first argument and salad comes second.
If, for some reason, you really can't use c library functions, you can reinvent the wheel by comparing the strings character by character by treating them as arrays.
Something similar to the following:
int compare(char* a, char* b)
{
assert(a!=NULL&&b!=NULL);
int i = 0;
while ( a[i] != '\0' )
{
if( b[i] == '\0' ) { return 1; }
else if( a[i] < b[i] ) { return -1; }
else if( a[i] > b[i] ) { return 1; }
i++;
}
if(b[i]=='\0')
return 0;
else
return -1;
}
Again, here I am using less than and greater than to compare individual characters because they hold an integer value as defined by ASCII standards.
To see what I am talking about, try casting a character to an int and printing it.

- 63
- 8
-
1You missed the "without using C library functions" part. – R Sahu Oct 14 '16 at 05:13
-
Actually I didn't, I sent a request to the user to explain what his issue with c library functions is. If it were part of the problem and not avoidance due to difficulty understanding, he would not be suggesting strlen. But I'll update to match the question until he responds. – taigrr Oct 14 '16 at 05:15
-
1@user3386109 Fixed. – taigrr Oct 14 '16 at 05:31
I think you should learn about Lexicalgraphic Order to write your own string comparision. http://mathworld.wolfram.com/LexicographicOrder.html

- 626
- 6
- 15
How about:
int mystrcmp(char *s1, char *s2)
{
int i = 0;
int c1, c2, ret_val = 0;
while ((c1 = s1[i]) && (c2 = s2[i]) && (ret_val = c2 - c1) == 0)
i++;
if (ret_val)
return ret_val;
else if (c1 == '\0')
c2 = s2[i];
return c2 - c1;
}
Using the integer values of the characters from the two strings s1
and s2
(which are probably ASCII or UTF-8 values), this function returns a positive integer if s1
precedes s2
, a negative value if s2
precedes s1
, and zero if s1
and s2
are equivalent strings.
As a point of interest regarding portability, the C standard does NOT guarantee that the numeric value associated with 'b'
is larger than that associated with 'a'
, for example. In practice, this is not really an issue for most people. But, there are other encoding schemes, such as EBCDIC, which are sometimes found (on, say, old IBM mainframes). With ASCII values, we would have mystrcmp("Aardvark", "aardvark") --> 32
, i.e., capital letters precede lowercase letters. But with EBCDIC values we would have mystrcmp("Aardvark", "aardvark") --> -64
because in this scheme capital letters follow lowercase letters. There are discussions of this issue here and here.

- 1
- 1

- 19,498
- 5
- 37
- 60