简体   繁体   中英

distance between two pointers in C

I have a function that it is supposed to read a string (only with numbers in it) and return the biggest sequence that doenst have repeated numbers. for example: 12345267890

it should return: 345267890

i've experimented the code manually and I believe it should work. but when I run it, and when it reaches this line i=(strchr(v+i, *(v+j)))-v; instead of getting the distance between the pointers I get something like -1046583. can I do this?

char* bigSeq(char *v){

    int i, j;
    char *aux, *bgst;
    aux=(char*) malloc(10*sizeof(char));
    bgst=(char*) malloc(10*sizeof(char));
    for(i=0;i<strlen(v);i++){
        for(j=0;j<strlen(v+i);j++){
            if(strchr(v+i, *(v+j)) != (v+j)){
                if(strlen(strncpy(aux, (v+i),j)) > strlen(bgst))
                    strncpy(bgst, (v+i),j);
                i=(strchr(v+i, *(v+j)))-v;
                break;
            }
        }
    }
    return bgst;
}

I think your trouble is with strchr() and what it returns when it doesn't find the character you are searching for. In that case, it returns 0, not either the start or the end of the string that is passed to it.

You should also review making the input a ' const char * ', and look at why you need to call strlen() on every iteration of the two loops. You can probably do better than that.

Note that if the longest substring of non-repeating digits is all 10 digits, your allocated space is too small - you need to allocate a byte for the NUL '\\0' at the end of the string too. Also, strncpy() does not guarantee to null-terminate your string; I'm not sure whether that is part of your problem here.

if(strlen(strncpy(aux, (v+i),j))

and

strncpy(bgst, (v+i),j);

If j is > 10 you're overwriting memory you don't own - which can lead to all sorts of funny issues.

I'm going to suggest an O(N) algorithm which is also simpler, easier to read, and requires no extra memory other than a few pointers and integers on the stack. These features help remove possibilities for error.

void longest_non_repeating( const char** output_begin, const char** output_end, const char* input )
{
    const char* last_occurrence[10] = { input-1 };
    const char* candidate_begin = input;
    while( *input )
    {
        if( last_occurrence[*input-'0'] < candidate_begin )
        {
            const char* candidate_end = input+1;
            if( candidate_end - candidate_begin > *output_end - *output_begin )
            {
                *output_begin = candidate_begin;
                *output_end = candidate_end;
                if( ( candidate_end - candidate_begin ) == 10 )
                    return;
            }
        }
        else
        {
          input = candidate_begin = last_occurrence[*input-'0'] + 1;
          std::fill( last_occurrence, last_occurrence+10, input-1 );
        }
        last_occurrence[*input-'0'] = input;
        ++input;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM