我想比较两个字符串与strcmp,为arduino。使用strcmp C/C++为Arduino时的数字通配符
我只是想知道是否有一个数字的C/C++/Arduino通配符。
我的输入将是LP后跟5个未知数字。
E.g.像
a_string[]="LP*****";
if(strcmp(input,a_sting) == 0)
{
// run this
}
我想比较两个字符串与strcmp,为arduino。使用strcmp C/C++为Arduino时的数字通配符
我只是想知道是否有一个数字的C/C++/Arduino通配符。
我的输入将是LP后跟5个未知数字。
E.g.像
a_string[]="LP*****";
if(strcmp(input,a_sting) == 0)
{
// run this
}
int i,j,k,l,m;
if(strlen(input)==7 &&
sscanf(input, "LP%1d%1d%1d%1d%1d", &i,&j,&k,&l,&m)==5)
{
//run this
}
strcmp()
不会这样做。 scanf()
会做些什么关闭:
int i ;
if(scanf("LP%d", &i) == 1)
{
printf("match") ;
}
else
{
printf("no match") ;
}
但不是特别的5位数字。显而易见的解决方案是使用正则表达式库,但这对于Arduino来说可能有点重量级,并且这个简单的要求 - 将自己的函数编写为符合您的特定要求会很容易。
这是一段可能适合您的要求的代码,该代码复制从[0]到[1]的前两个字母。此外,它会检查输入的字母是否确实是L和P
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
char* a[2] ;
int size = 7;
a[0] = (void *) malloc(sizeof(char) * (size + 1));
a[1] = (void *) malloc(sizeof(char) * 2);
printf("Enter device\n");
fgets(a[0], 8, stdin);
//strcpy(a[0], "LPC1768");
puts(a[0]);
printf("\n");
//printf("a[0] = %s\n", a[0]);
memcpy(a[1], a[0], 2);
if (*(a[1]) == 'L' && *(a[1] + 1) == 'P')
{
puts(a[1]);
}
free(a[0]);
free(a[1]);
}
难道你不能只用'strncmp()'比较前两个字符吗? – 2014-12-01 20:32:52
其他输入可能以LP开头,但不能跟随数字 – user2690146 2014-12-01 20:36:50
'strcmp'函数不使用通配符。你将不得不使用正则表达式函数或者'isnum'函数。 – 2014-12-01 20:37:43