C 如何检查输入的字符串中是否存在模式?
我有一个任务,用户在一个函数中输入一个字符串,然后输入一个模式,然后必须检查该模式是否存在于字符串中,它出现了多少次,以及偏移量是多少。我被难住了,我的同学们一直给我一些神秘的暗示。下面是我的get函数C 如何检查输入的字符串中是否存在模式?,c,C,我有一个任务,用户在一个函数中输入一个字符串,然后输入一个模式,然后必须检查该模式是否存在于字符串中,它出现了多少次,以及偏移量是多少。我被难住了,我的同学们一直给我一些神秘的暗示。下面是我的get函数 int getNums() { printf("Please enter a number: "); //Initial printf int count, patcount; int torf; char len_num[31]; //T
int getNums()
{
printf("Please enter a number: "); //Initial printf
int count, patcount;
int torf;
char len_num[31]; //The character array for the initial entered string
char pat_num[6]; //The character array for the entered pattern after initial string
char *lenptr = len_num; //pointer to the address of the first element of len_num
char *patptr = pat_num; //pointer to the address of the first element of len_num
scanf("%s", len_num); //Where the user scans in their wanted number, which is treated as a string
printf("\n");
printf("%s\n", lenptr);
int len = stringLength(lenptr); //Checks how long string is
int valid = isValid(len_num); //Checks if string is valid
for(count=0; count<len_num[count]; count++) //Checks if length of string is within appropriate range
{
if(len>=10 && len<=30) //Continues to pattern get if within range
{
torf=1;
}
else //Denies continuation if string is outside of range
{
torf=0;
printf("Not within range! Try again!\n");
return (1);
}
}
printf("Please enter a pattern: "); //Initial entry statement for pattern
scanf("%s", pat_num); //User scans in pattern
printf("\n");
printf("%s\n", pat_num);
len = stringPattern(patptr); //Check how long pattern is
valid = isValid(pat_num); //Checks if pattern is valid
for(patcount=0; patcount<pat_num[patcount]; patcount++) //Checks if length of pattern is within appropriate range
{
if(len>=2 && len<=5) //Continues to pattern check if within range
{
torf=1;
}
else //Denies continuation if pattern is outside of range
{
torf=0;
printf("Pattern not within range! Try again!\n");
return (1);
}
}
checkPattern();
}
intgetnums()
{
printf(“请输入一个数字:”;//初始printf
int计数,patcount;
int torf;
char len_num[31];//初始输入字符串的字符数组
char pat_num[6];//初始字符串后输入模式的字符数组
char*lenptr=len_num;//指向len_num的第一个元素的地址的指针
char*patptr=pat_num;//指向len_num的第一个元素的地址的指针
scanf(“%s”,len_num);//其中用户扫描他们想要的号码,该号码被视为字符串
printf(“\n”);
printf(“%s\n”,lenptr);
int len=stringLength(lenptr);//检查字符串的长度
int valid=isValid(len_num);//检查字符串是否有效
对于(count=0;count=10&&len由于您要求使用模式匹配函数,我没有检查您的字符串输入函数。您可以使用以下简单的驱动程序代码来测试我的解决方案:
#include <stdio.h>
void findPattern(char* input, char* pattern);
int main()
{
char input[31], pattern[6];
printf("Enter the string: ");
scanf("%s", input);
printf("Enter the pattern: ");
scanf("%s", pattern);
findPattern(input, pattern);
return 0;
}
我必须用指针通过引用,我也被它卡住了
如果是这种情况,则将此函数调用为:
findPattern(&input,&pattern);
您可能在考虑解决方案。您有一个字符串input
,其中包含许多字符,您想计算中的模式的多字符匹配数。字符串的一个好处是您不需要知道它们在上面迭代的时间,因为根据定义,C中的字符串结束使用nul终止字符
这允许您只需在findpattern
函数中保留索引,并且每次input
中的字符与pattern
中的字符匹配时,您都会增加索引(否则,您会将索引归零)。如果您达到pattern[index]的点=='\0'
您已经匹配了模式中的所有字符
您必须始终使用类型声明一个函数,该类型将提供有意义的返回,以指示该函数执行的任何操作的成功/失败(如果该函数只打印输出,则void
可以)
否则,您需要选择一个合理的返回类型,以指示在input
中是否找到了pattern
的匹配项(以及找到了多少个匹配项)。这里可以使用一个简单的int
类型(这将限制可以返回到2147483647
的匹配项的数量,这应该足够了)
将这些部分放在一起,您可以将功能简化为类似于:
int findpattern (const char *input, const char *ptrn)
{
int n = 0, idx = 0; /* match count and pattern index */
while (*input) { /* loop over each char in s */
if (*input == ptrn[idx]) /* if current matches pattern char */
idx++; /* increment pattern index */
else /* otherwize */
idx = 0; /* zero pattern index */
if (!ptrn[idx]) { /* if end of pattern - match found */
n++; /* increment match count */
idx = 0; /* zero index for next match */
}
input++; /* increment pointer */
}
return n; /* return match count */
}
添加一个简短的示例程序,允许您输入模式
和输入
作为程序的前两个参数(如果未提供一个或两个参数,则使用显示的默认值):
请注意,提供有意义的返回如何允许您(1)验证是否找到匹配项;(2)提供通过返回找到的匹配项的数量。完整的代码只需要标题stdio.h
,例如
#include <stdio.h>
int findpattern (const char *input, const char *ptrn)
{
int n = 0, idx = 0; /* match count and pattern index */
while (*input) { /* loop over each char in s */
if (*input == ptrn[idx]) /* if current matches pattern char */
idx++; /* increment pattern index */
else /* otherwize */
idx = 0; /* zero pattern index */
if (!ptrn[idx]) { /* if end of pattern - match found */
n++; /* increment match count */
idx = 0; /* zero index for next match */
}
input++; /* increment pointer */
}
return n; /* return match count */
}
int main (int argc, char **argv) {
char *pattern = argc > 1 ? argv[1] : "my",
*input = argc > 2 ? argv[2] : "my dog has fleas, my cat has none";
int n;
if ((n = findpattern (input, pattern)))
printf ("'%s' occurs %d time(s) in '%s'\n", pattern, n, input);
else
puts ("pattern not found");
}
单一匹配:
$ ./bin/findpattern fleas
'fleas' occurs 1 time(s) in 'my dog has fleas, my cat has none'
找不到模式
$ ./bin/findpattern gophers
pattern not found
所有相同的模式:
$ ./bin/findpattern my "mymymy"
'my' occurs 3 time(s) in 'mymymy'
函数本身的输出
虽然最好提供一个返回值来指示匹配的数量(这将允许以多种不同的方式重用函数),如果您只是想使其成为每次调用时都输出结果的输出函数,那么只需将输出移到该函数中,并声明另一个指向input
的指针,这样input
将保留下来,以便在最后打印
变化很小,例如:
#include <stdio.h>
void findpattern (const char *input, const char *ptrn)
{
const char *p = input; /* pointer to input */
int n = 0, idx = 0; /* match count and pattern index */
while (*p) { /* loop over each char in s */
if (*p == ptrn[idx]) /* if current matches pattern char */
idx++; /* increment pattern index */
else /* otherwize */
idx = 0; /* zero pattern index */
if (!ptrn[idx]) { /* if end of pattern - match found */
n++; /* increment match count */
idx = 0; /* zero index for next match */
}
p++; /* increment pointer */
}
if (n) /* output results */
printf ("'%s' occurs %d time(s) in '%s'\n", ptrn, n, input);
else
puts ("pattern not found");
}
int main (int argc, char **argv) {
char *pattern = argc > 1 ? argv[1] : "my",
*input = argc > 2 ? argv[2] : "my dog has fleas, my cat has none";
findpattern (input, pattern);
}
#包括
void findpattern(常量字符*输入,常量字符*ptrn)
{
const char*p=input;/*指向输入的指针*/
int n=0,idx=0;/*匹配计数和模式索引*/
while(*p){/*在s中的每个字符上循环*/
if(*p==ptrn[idx])/*如果当前匹配模式字符*/
idx++;/*增量模式索引*/
else/*otherwize*/
idx=0;/*零模式索引*/
如果(!ptrn[idx]){/*如果模式结束-找到匹配项*/
n++;/*增量匹配计数*/
idx=0;/*下一个匹配的索引为零*/
}
p++;/*增量指针*/
}
如果(n)/*输出结果*/
printf(“%s”在“%s”中出现了%d次\n,ptrn,n,input);
其他的
看跌期权(“未找到模式”);
}
int main(int argc,字符**argv){
char*pattern=argc>1?argv[1]:“我的”,
*input=argc>2?argv[2]:“我的狗有跳蚤,我的猫没有”;
findpattern(输入、模式);
}
(使用和输出同上)
请仔细查看,如果您还有其他问题,请告诉我。如果您允许使用,您可能需要查看strstrstr
函数。除了stdio之外,我们不能使用任何库。您好,我不知道您是否简洁,但是for循环中的if…else
子句没有任何作用。这意味着,循环将没有任何效果。你应该把它放在for循环上面。然后我假设checkPattern()
需要在for循环中。正如旁注:行scanf(“%s”,len_num);
使用不安全,除非输入可以信任。恶意输入可能会导致程序崩溃。有关详细信息,请参阅。但是,这与您遇到的问题无关。您可能需要重新安装
$ ./bin/findpattern gophers
pattern not found
$ ./bin/findpattern my "mymymy"
'my' occurs 3 time(s) in 'mymymy'
#include <stdio.h>
void findpattern (const char *input, const char *ptrn)
{
const char *p = input; /* pointer to input */
int n = 0, idx = 0; /* match count and pattern index */
while (*p) { /* loop over each char in s */
if (*p == ptrn[idx]) /* if current matches pattern char */
idx++; /* increment pattern index */
else /* otherwize */
idx = 0; /* zero pattern index */
if (!ptrn[idx]) { /* if end of pattern - match found */
n++; /* increment match count */
idx = 0; /* zero index for next match */
}
p++; /* increment pointer */
}
if (n) /* output results */
printf ("'%s' occurs %d time(s) in '%s'\n", ptrn, n, input);
else
puts ("pattern not found");
}
int main (int argc, char **argv) {
char *pattern = argc > 1 ? argv[1] : "my",
*input = argc > 2 ? argv[2] : "my dog has fleas, my cat has none";
findpattern (input, pattern);
}