Search code examples
csearchsubstringc-stringsfunction-definition

Find String Function in [ C ]


Needing a function that would check me for the existence of a substring and give me the location I created this function, I wanted to know if something similar already exists in the C headers and how it works.

This own function give the position where start the substring Hello world! if i search world give 6 If the string is not found then give -1

#include <stdio.h>
#include <string.h>

int findfstr(const char mainstring[], const char substring[]){
    // int = findstring(mainstring,substring) give position if found and -1 if not

    int main_length = strlen(mainstring); // Read the mainstring length
    int subs_length = strlen(substring); // Read the substring length
    int where = 0; // Set to 0 the var of start position
    int steps = (main_length - subs_length); //Retrive the numbers of the chars without substring
    int cicle = 0; // Set to 0 the var used for increment steps
    
    
    char found_string[subs_length]; // Set the Array to the substring length
    
    if ( subs_length <= main_length){ // If substring is bigger tha mainstring make error
        while (where == 0){ //loop until var "where are equal to 0"
            
            //Stop loop if and when  cicle is bigger than steps
            if (cicle >= steps && where == 0){ where = -1;} 
            
            //retrive the substring and store in found_string
            strncpy(found_string, mainstring+cicle, subs_length); 
            
            found_string[subs_length] = '\0'; //Add terminator char to end string
            
            //If retrived string are equal to substring then set where with clicle value
            if ((strcmp(found_string, substring) == 0 )) { 
                    where = cicle;
                
            }
            cicle++; //add +1 to cicle
            
            
        }
    
    }else{ printf("\n substring is to big \n"); } //error message
    
    return where; 
}

int main(){

    int fs = 0;

    // This is how use the function
    fs = findfstr("Hello world!","world");


    if ( fs > 0 ){ printf("\n String found and start in: %d", fs);}
    if ( fs < 0 ){ printf("\n String not found value: %d", fs);}
return 0;

}

Output:

String found and start in: 6


Solution

  • I wanted to know if something similar already exists in the C

    Yes, there is.

    strstr is what you are looking for. See https://man7.org/linux/man-pages/man3/strstr.3.html

    If you want the offset from the start of the string just use pointer arithmetic.

    Something like:

    #include <stdio.h>
    #include <string.h>
    int main(void){
        char* str ="Hello";
     
        char* needle = strstr(str, "ell");  // Search for "ell" in str
        if (needle)
        {
            int offset = needle - str;  // Calculate the offset
            printf("offset is %d\n", offset);
        }
        else
        {
            // not found...
        }
        return 0;
    }
    

    Output:

    offset is 1