I am on a low level embedded system comparing a fixed size buffer of chars to a simple string. The buffer is much larger than the string so I think that that is why the comparison operators says that the two are not equal. This prompted me to write a simple function to compare the string to the beginning of the buffer - is this an elegant solution? Thanks
int CompareString(const char* string, int strlen)
{
//first c开发者_如何学Goheck to see if the length is the same
//if there is a null char at string length then
//they are equal
if(MsgBuffer[strlen] != '\0')
{
return 0; //they are not equal
}
strlen = strlen - 1;
while(strlen > -1)
{
if(string[strlen] != MsgBuffer[strlen])
{
return 0; //they are equal
}
strlen = strlen - 1;
}
return 1; //they are equal
}
Normally, you could look into using strncmp
with the maximum length being the fixed buffer size.
But it may be that you're constrained there somewhat, especially given that you're operating in an embedded environment. It may also not be suitable for comparing space-padded strings where each string has a different number of spaces at the end (including none for a string like "formaldehyde") - strncmp
won't work well for comparing "Hello"
and {'H','e','l','l','o',' ',' ',' '}
if the size is 8
.
If that's the case, I'd be looking at something like the following:
#include <stdio.h>
int compStr (char *s1, char *s2, size_t sz) {
while (sz != 0) {
// At end of both strings, equal.
if ((*s1 == '\0') && (*s2 == '\0')) break;
// Treat spaces at end and end-string the same.
if ((*s1 == '\0') && (*s2 == ' ')) { s2++; sz--; continue; }
if ((*s1 == ' ') && (*s2 == '\0')) { s1++; sz--; continue; }
// Detect difference otherwise.
if (*s1 != *s2) return 0;
s1++; s2++; sz--;
}
return 1;
}
int main (void) {
printf ("%d\n", compStr ("Hello", "Hello", 5));
printf ("%d\n", compStr ("Hello", "Hello ", 5));
printf ("%d\n", compStr ("Hello ", "Hello", 5));
printf ("%d\n", compStr ("Hello ", "Hello ", 5));
printf ("%d\n", compStr ("HelloX", "Hello", 5));
printf ("%d\n", compStr ("HelloX", "HelloY", 5));
printf ("%d\n", compStr ("HelloX", "HelloY", 6));
return 0;
}
This will match strings with any number of spaces at the end, up to a specific size and has the advantage that you can pass arbitrary buffers to it rather than assuming one is a global area.
As an aside, it's not a good idea to use a standard library function like strlen
as a variable name since you may want to. at some point, use the standard library.
Here is some code you can use. Simply pass the comparison string and the buffer as two parameters. This code should also be fully compliant with MISRA-C:2004.
sint8 gpfunc_strcmp (const uint8* s1, const uint8* s2)
{
while ((*s1 != '\0') && (*s2 != '\0'))
{
if (*s1 != *s2)
{
break;
}
else
{
s1++;
s2++;
}
}
return (sint8)(*s1 - *s2);
}
Use strncmp
"The comparison operators"? Do you mean == and so on? You can't use those to compare strings, since they will compare the locations, not the contents. You must use strcmp or strncmp instead.
(And I guess the answer to your actual question is that no, this is not an elegant solution, since strncmp already exists. But, as others have said, your embedded environment might make use of the strings library inconvenient.)
精彩评论