Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 任何数字/数字在不带外循环的字符串中首次出现_C_String_Loops - Fatal编程技术网

C 任何数字/数字在不带外循环的字符串中首次出现

C 任何数字/数字在不带外循环的字符串中首次出现,c,string,loops,C,String,Loops,假设我有一根绳子 char *Wstr = "TEMP =20C ALT=12D" char *temp_pos = NULL; char *tpos = NULL; char temp[10] = {'\0'}; 我正在尝试获取值20并将其存储。 TEMP和20C之间的间距可能不一致,即: TEMP=20C或TEMP=20C或TEMP=20C或TEMP=20C目前我为TEMP调用strstr,然后将while循环与isdigit结合使用: if ((temp_pos = strstr(Wst

假设我有一根绳子

char *Wstr = "TEMP =20C ALT=12D"
char *temp_pos = NULL;
char *tpos = NULL;
char temp[10] = {'\0'};
我正在尝试获取值20并将其存储。
TEMP
20C
之间的间距可能不一致,即:
TEMP=20C
TEMP=20C
TEMP=20C
TEMP=20C

目前我为
TEMP
调用
strstr
,然后将while循环与
isdigit
结合使用:

if ((temp_pos = strstr(Wstr, "TEMP"))) {
   tpos = temp_pos+4;

   while(isspace(*tpos) || (*tpos == '='))
      tpos++;
   while ((isdigit(*tpos)) && (*tpos!='\0')) {
      temp[i] = *tpos;
      i++;
      tpos++;
   }
}

有没有一种方法可以让我立即得到数字,即在本例中,在
strstr
之后20,我想最小化代码中的循环数,有没有更好的方法,也就是更好地编写代码/逻辑?

为什么不先搜索
=
而不是
TEMP
?我列出的字符串是较大字符串的一部分,比如:LAT=xyz LONG=yyyy等等。。。。。TEMP=20cm可能他想找到“TEMP=20”,字符串可以包含其他方程。实际上,在一天结束时,你不能避免使用迭代(循环)。谢谢!是否会在代码中对其进行检查为什么不先搜索
=
而不是
TEMP
?我列出的字符串是较大字符串的一部分,例如:LAT=xyz LONG=yyyy等。。。。。TEMP=20cm可能他想找到“TEMP=20”,字符串可以包含其他方程。实际上,在一天结束时,你不能避免使用迭代(循环)。谢谢!将在代码中对相同的内容进行检查