我正在通过指针算术学习我的方法,并试图使用在大海捞针中找到第一次出现的字符串,strstr()
并从那里提取任何第一组数字(如果有的话)。例如:
Needle: SPAM
Input: Sept 10 2012; undefined SPAM (uid AUIZ); 03_23_1 user FOO 2012_2
Output: SPAM 03_23_1
Needle: BAR
Input: Oct 10 2012; variable BAR; 93_23_1; version BAZ
Output: BAR 93_23_1
Needle: FOO
Input: Oct 10 2012; variable FOOBAZ; version BAR
Output: FOOBAZ
我怎样才能做到这一点?谢谢。
这是我开始的,但不知道如何继续。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main ()
{
char *first,*second;
const char *str = "Sept 10 2012; undefined SPAM (uid AUIZ); 03_23_1 user FOO 2012_2";
char *find = "SPAM";
first = strstr(str, find);
if (first != NULL)
{
second = first;
while(*second != '\0')
{
if (isdigit(second[0]))
{
break;
}
second++;
}
}
return 0;
}