mercredi 22 avril 2015

Function to search for string in array of strings

I am trying to write a function that searches for the unique part(maximum two characters) of a string in an array of strings. Although strstr and strchr are not working and crash my program for some reason. So I have resorted to trying to create something that resembles their function.

My question is this:
Why is strstr not working (something like strstr(lex[j],word)) and what am I doing wrong here?

Here is the code for the function that searches for two unique characters within an array of strings :

void convert(char word[]){
int i;

for(i=0;i<=strlen(word);i++){
    if(word[i]>=65 && word[i]<=90){
    word[i]=word[i]+32;
    }
}
}


int  twochar(char lex[50][50],char word[], int size,char temp[3]){
int i,j,k,count,totlen;
convert(word);

for(i=0;i<strlen(word)-1;i++){
    count=0;
    totlen=0;
    for(j=0;j<size;j++){
        convert(lex[j]);
        totlen+=strlen(lex[j])-1;
        for(k=0;k<strlen(lex[j])-1;k++){
            if(word[i]!=lex[j][k] || word[i+1]!=lex[j][k+1]){
                count++;
            }   
        }
    }
    if(count==totlen){
        temp[0]=word[i];
        temp[1]=word[i+1];
    }
}
}



int main(int argc, char *argv[]) {
char lex[50][50]={"","word2","word3","word4"}, word[]="test";
char p[3];

twochar(lex,word,4,p);
printf("%c%c\n",p[0],p[1]);
return 0;
}

Aucun commentaire:

Enregistrer un commentaire