Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR C

find a substring within a string in c and return the index

int findocc(const char* s1, unsigned int n){
    int i =0;
    int j = n;
    // find the size of the number
    while(n > 0){n /=10 ; i++;}

    char buff[i];
    sprintf(buff, "%d", j);

    // Find the first apperance of n in s1;
    char * found = strstr(s1, buff);

    if(!found){
        return 0;
    }

//    int s=0;
//    for (const char *ptr=s1;  *ptr;  ptr++) {
//        if(strncmp(ptr, buff,i) == 0){
//            return s;
//        }else{
//            s++;
//        }
//    }

    return found - s1;
}
 
PREVIOUS NEXT
Tagged: #find #substring #string #return #index
ADD COMMENT
Topic
Name
3+4 =