在c中重新启动while循环而没有整数

我试图让这个代码工作,但我不知道如何重新启动内部while循环。 我该怎么办?

/* * Return a pointer to the first occurrence of any character in  * in the given  or NULL if the  contains no characters * in . ***** * YOU MAY *NOT* USE INTEGERS OR ARRAY INDEXING. ***** */ char *find_any_ptr(char *string, char* stop) { char *newstring = (char*)0; while(*stop != '\0'){ while(*string != '\0') { if(*string == *stop){ if(newstring < string || newstring != (char*)0){ string++; }else{ newstring = string; string++; } } } stop++; } return newstring; // placeholder } 

string指针使用临时变量,并在内部循环中使用此临时变量。

 while(*stop != '\0'){ char *p = string; while (*p != '\0') { ... /* use 'p' in place of 'string' */ } stop++; } 

除了指向string的字符指针和指向stop的指针外,这一点相对简单。 对于string中的每个字符,您将与stop中的每个字符进行比较,在匹配时返回string的字符,如果未找到匹配项则返回NULL

 #include  char *find_any_index(char string[], char stop[]) { char *p = string; char *sp = NULL; while (*p) { sp = stop; while (*sp) { if (*sp == *p) return p; sp++; } p++; } return NULL; } int main (int argc, char **argv) { if (argc < 3) { printf ("usage: %s string stoplist\n", argv[0]); } printf ("\n string: %s\n stop : %s\n\n", argv[1], argv[2]); printf (" first char in string matching a char in stop: %s\n\n", find_any_index (argv[1], argv[2])); return 0; } 

产量

 $ ./bin/find_substr_str thisIsAstring mase string: thisIsAstring stop : mase first char in string matching a char in stop: sIsAstring 

这是一个演示程序,显示了如何编写函数

 #include  char * find_any_ptr( const char *string, const char* stop ) { const char *p, *q; _Bool found = 0; p = string; do { q = stop; while ( *q && *q != *p ) ++q; } while ( !( found = *q ) && *++p ); return ( char * )( found ? p : NULL ); } int main(void) { const char *p = find_any_ptr( "abc9de", "1234567890" ); if ( p ) puts( p ); return 0; } 

程序输出是

 9de 

只有我会将函数命名为find_any_char而不是find_any_ptr 🙂

这是我的实现:

 #include  #include  char * findany(char *string, char *stop) { char * app; //To avoid segmentation fault! if (stop==NULL || string==NULL || !*stop || !*string) return NULL; do { app=string; while(*app!=0 && *app!=*stop) app++; stop++; } while(*app==0 && *stop!=0); return (*app!=0)?app:NULL; } int main(void) { char string[100]; char stop[100]; char * found; for(;;) { printf("Insert a string without spaces[q to exit]: "); scanf("%s",string); if (!strcmp(string,"q")) break; printf("Insert the chars to search without spaces: "); scanf("%s",stop); printf("Searching any occurence of a char in \"%s\"" " inside \"%s\"\n",stop,string); found=findany(string,stop); printf("%s\n",(found!=NULL)?found:"NULL"); } return 0; } 

我认为最好还使用以下方式来实现findany()函数:

 char * _findany(char *string, char *stop) { char * app; // to start the first loop //To avoid segmentation fault! if (stop==NULL || string==NULL || !*stop || !*string) return NULL; do { app=stop; while(*app!=0 && *app!=*string) app++; string++; } while(*app==0 && *string!=0); return (*app!=0)?(string-1):NULL; } 

您可以观察在上面的代码中添加函数_findany的两个函数之间的区别,并调用新函数在主上面的printf之后(或之前)添加以下代码。

 found=_findany(string,stop); printf("%s\n",(found!=NULL)?found:"NULL");