任意输入长字符串与需要检测的 短字符串, 找出长字符串中含有多少个短字符串, FPGA里面的码流检测
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
char long_char[256];
char short_char[256];
int cnt = 0;
int long_i = 0;
int cmp_cnt = 0;
int long_length, short_length;
printf("/n test char string /n");
printf("/n string numb %d, /n" , argc);
printf("/nPlease input long char string : ");
scanf("%s", long_char);
printf("/n long char string is %s , length = %d/n", long_char, strlen(long_char));
printf("/nPlease input the string you find : ");
scanf("%s", short_char);
printf("/n long char string is %s ,length = %d/n", short_char, strlen(short_char));
long_length = strlen(long_char);
for(long_i = 0; long_i < long_length; long_i++)
{
if( *(long_char + long_i) == *(short_char + cnt) )
{
cnt++;
if(cnt == strlen(short_char))
{
cnt = 0;
cmp_cnt++;
//continue;
printf("/n %d/n", long_i);
}
}
else
{
cnt = 0;
}
}
长字符串一直向前推,当遇见与短字符头相同时,开始比对短字符串,成功记录下来,失败清零复位。
printf("/n the same section = %d/n", cmp_cnt);
return 0;
}