Restarting while loop in c without integers

我的未来我决定 提交于 2019-12-11 17:09:25

问题


I'm trying to get this code to work, but I have no idea how to restart the inner while loop. How would I do it?

/*
 * 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
}

回答1:


Use a temporary variable for string pointer, and use this temp variable instead inside the inner loop.

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

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

    }   
    stop++; 
} 



回答2:


This is relatively simple using nothing but a character pointer to the string and a pointer to stop. For each character in your string, you compare against each character in stop, returning the character in string on match, or NULL if no match is found:

#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;
}

Output

$ ./bin/find_substr_str thisIsAstring mase

 string: thisIsAstring
 stop  : mase

 first char in string matching a char in stop: sIsAstring



回答3:


Here is a demonstrative program that shows how the function can be written

#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;
}

The program output is

9de

Only I would name the function find_any_char instead of find_any_ptr:)




回答4:


This is my implementation:

#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;
}

I think that is better to use also the following way to implement the function 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;
}

You may observe the difference between the two functions adding the function _findany in the code above and to call the new function adding the following code after (or before) the printf in the main above.

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


来源:https://stackoverflow.com/questions/29587005/restarting-while-loop-in-c-without-integers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!