In this hypothetical order, does "potato" compare greater than positive infinity? What about strings such as "1&24", "124q", "000-124"? Where do they fit in this order?
Line 6: tmp is an uninitialized pointer. At lines 27 and 29 you're going to reference that uninitialized pointer which will cause a crash.
Line 9: main must be type int.
Line 38: main() should return 0.
As helios pointed out, you're comparing apples and oranges. stricmp is a case insensitive alphabetic compare. Line 25 is going to compare entries in alphabetic order, not numeric order. If you want a numeric comparison, use ints.
tmp is a null pointer, so when you copy to it at line 27, the behavior is undefined. Change line 6 to char a[50][50], tmp[50];
As Helios stated, right now you're sorting strings, which is different from sorting numbers. Your question implies that numbers should be sorted numerically. How should strings be be sorted? It sounds like strings should come after numbers?
If there are multiple strings, should they be sorted relative to each other?
Should "-123abc" be interpretted as a string or a number?
What about --234?
If this is a homework assignment, can you post the text of the assignment?
I've did what AbstractionAnon and dhayden told me to it still outputs the same result. I haven't really thought about trying "-123abc" and such, also i can't use ints, it has to be char. That is what's confusing me, but the main problem is just this: it has to display -6 first before -1. As of now, it displays -1,-6,potato which is clearly wrong. Is there like a function or something to modify this?
int strcmp(constchar *a, constchar *b){
for (; *a || *b; a++, b++){
if (*a < *b)
return -1;
if (*a > *b)
return 1;
}
return 0;
}
By this definition, "-6" does indeed compare greater than "-1", because '-' == '-' and '6' > '1'. This order isn't wrong, you're just trying to use a lexicographical comparison function to compare numbers.
There's no built-in function that implements the order you're proposing, since at it is, it's ill-defined. You're on your own.
Not at all. You just have to implement whatever ordering function you want yourself. If you can describe what you want we might be able to help you design it.
Thanks. So one solution I've thought about is removing the negative sign in both those numbers, I think the output would then be: -6,-1, potato thus correct. The problem is how do i do this?
Err, no. Given two strings of the form S1 = x + y and S2 = x + z, where + is the concatenation operator, if S1 < S2 then y < z. In other words, since "-1" < "-6", "1" < "6", so the order would remain the same.
Personally, I don't even see what you're trying to solve. What's the problem? You have a list of strings and you're sorting them with strcmp() and getting back a lexicographically sorted list.
Your problem is that a string is a fully general type. Anything that a computer can represent can be represented in a string. So if you're going to say that some strings are numbers and that they should all be comparable with each other you're going to need to have
1. A full definition of what shape a string may have to be considered a number.
2. A comparison between two numerical strings.
3. A comparison between two non-numerical strings.
4. A comparison between a numerical and a non-numerical string.
Which of these strings are numerical?
* "1"
* "-4"
* " -4" (note the space)
* "--7"
* "1+2"
* "1++2"
* "8.16196e+99"
* "0.999..."
* "pi"
* "1 + pi"
* "lim x -> 0 sin(x)/x"
* "potato + 1 where potato = 1"
* "inf"
How do you handle the cases where a number may be represented in multiple ways?
* cmp("1", "01") = ?
* cmp("0", "-0") = ?
* cmp("0", "+0") = ?
* cmp("1", "1.0") = ?
* cmp("1", "--1") = ?
* cmp("1", "+1") = ?
* cmp("0.999...", "1") = ?
Not necessarily. It depends on how you answer those questions. There's quite a few sets of answers that define an order that's not stupidly complex to implement.