2015-04-12 73 views
0

我试图让这段代码工作,但我不知道如何重新启动inner while while循环。我会怎么做?在没有整数的c中重新启动while循环

/* 
* Return a pointer to the first occurrence of any character in <stop> 
* in the given <string> or NULL if the <string> contains no characters 
* in <stop>. 
***** 
* 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 
} 

回答

2

使用临时变量string指针,并使用此临时变量代替内循环的内部。

while(*stop != '\0'){ 
    char *p = string; 
    while (*p != '\0') { 

     ... /* use 'p' in place of 'string' */ 

    } 
    stop++; 
} 
2

这是一个相对简单的使用只是一个字符指针string和指向stop。对于您的string每个字符,你stop比较针对每个字符,在string上比赛,或者NULL返回字符,如果没有发现匹配:

#include <stdio.h> 

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 
0

这里一个演示程序,显示如何写入功能

#include <stdio.h> 

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_ptr :)

0

这是我实现我的名字,而不是find_any_char功能:

#include <stdio.h> 
#include <stdlib.h> 

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<Enter> 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");