C 比较固定大小的字符数组和字符串
我在一个低级嵌入式系统上,将固定大小的字符缓冲区与简单字符串进行比较。缓冲区比字符串大得多,所以我认为这就是为什么比较运算符说两者不相等的原因。这促使我编写了一个简单的函数来比较字符串和缓冲区的开头-这是一个优雅的解决方案吗?谢谢C 比较固定大小的字符数组和字符串,c,C,我在一个低级嵌入式系统上,将固定大小的字符缓冲区与简单字符串进行比较。缓冲区比字符串大得多,所以我认为这就是为什么比较运算符说两者不相等的原因。这促使我编写了一个简单的函数来比较字符串和缓冲区的开头-这是一个优雅的解决方案吗?谢谢 int CompareString(const char* string, int strlen) { //first check to see if the length is the same //if there is a null char
int CompareString(const char* string, int strlen)
{
//first check 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
}
通常,您可以考虑使用
strncmp
,最大长度为固定的缓冲区大小
但这可能是因为您在某种程度上受到了限制,特别是考虑到您在嵌入式环境中操作。它也可能不适用于比较空格填充字符串,因为每个字符串的末尾都有不同数量的空格(包括像“甲醛”这样的字符串没有空格)-strncmp
不适用于比较“Hello”
和{H'、'e'、'l'、'o'、''、''、'}
,如果大小为8
如果是这样的话,我将看到如下内容:
#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;
}
#包括
int compStr(字符*s1,字符*s2,大小sz){
while(sz!=0){
//在两个字符串的末尾,相等。
如果((*s1='\0')&(*s2=='\0'))中断;
//将结束处的空格和结束字符串视为相同。
如果((*s1='\0')&(*s2=''){s2++;sz--;continue;}
如果((*s1='')&(*s2='\0'){s1++;sz--;continue;}
//否则检测差异。
如果(*s1!=*s2)返回0;
s1++;s2++;sz--;
}
返回1;
}
内部主(空){
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));
返回0;
}
这将使字符串与末尾任意数量的空格相匹配,最大可达特定大小,其优点是您可以向其传递任意缓冲区,而不是假设其中一个缓冲区是全局区域
顺便说一句,使用标准库函数(如
strlen
)作为变量名不是一个好主意,因为您可能需要这样做。在某些情况下,可以使用标准库。通常,您可以考虑使用最大长度为固定缓冲区大小的strncmp
但这可能是因为您在某种程度上受到了限制,特别是考虑到您在嵌入式环境中操作。它也可能不适用于比较空格填充字符串,因为每个字符串的末尾都有不同数量的空格(包括像“甲醛”这样的字符串没有空格)-strncmp
不适用于比较“Hello”
和{H'、'e'、'l'、'o'、''、''、'}
,如果大小为8
如果是这样的话,我将看到如下内容:
#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;
}
#包括
int compStr(字符*s1,字符*s2,大小sz){
while(sz!=0){
//在两个字符串的末尾,相等。
如果((*s1='\0')&(*s2=='\0'))中断;
//将结束处的空格和结束字符串视为相同。
如果((*s1='\0')&(*s2=''){s2++;sz--;continue;}
如果((*s1='')&(*s2='\0'){s1++;sz--;continue;}
//否则检测差异。
如果(*s1!=*s2)返回0;
s1++;s2++;sz--;
}
返回1;
}
内部主(空){
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));
返回0;
}
这将使字符串与末尾任意数量的空格相匹配,最大可达特定大小,其优点是您可以向其传递任意缓冲区,而不是假设其中一个缓冲区是全局区域
顺便说一句,使用标准库函数(如
strlen
)作为变量名不是一个好主意,因为您可能需要这样做。在某些情况下,使用标准库。“比较运算符”?你的意思是==等等吗?您不能使用它们来比较字符串,因为它们将比较位置,而不是内容。您必须改用strcmp或strncmp
(我猜您实际问题的答案是,不,这不是一个优雅的解决方案,因为strncmp已经存在。但是,正如其他人所说,您的嵌入式环境可能会使用字符串库带来不便。)比较运算符?你的意思是==等等吗?您不能使用它们来比较字符串,因为它们将比较位置,而不是内容。您必须改用strcmp或strncmp
(我猜对您实际问题的答案是,不,这不是一个优雅的解决方案,因为strncmp已经存在。但是,正如其他人所说,您的嵌入式环境可能会使用字符串库带来不便。)使用使用以下是一些您可以使用的代码。只需将比较字符串和缓冲区作为两个参数传递。本规范还应完全符合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);
}
下面是一些您可以使用的代码。只需将比较字符串和缓冲区作为两个参数传递。本规范还应完全符合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);
}
除了使用strncmp()的错误之外;考虑到MsgBuffer是一个全球性的,那么它就不优雅了;全局是可以接受的。此外,我的空间限制非常小-导入string.h会不会给我一些不需要的bloat?到strncmp群组