0

I am trying to compare a string (ignoring case) against a specified element in a vector string (I don't want to see if the string exists anywhere in the vector, only if it exists at the specific index).

I have not been successful using a string compare as I normally would. Any help would be appreciated.

1 Answers1

0

strnicmp( ... ) is your tool. the 'n' is for limited length, and the 'i' is for case insensitive. You just need to add the the specific index to the vector: vector+pos or &vector[pos]

int isKeyFoundAtPos( const char* key, const char* vector, int vPos) {

    if( vPos > strlen(vector) )  // if you want to be safe, check for this and deal with it.
        return -1;

    return 0 == strnicmp( key, vector+vPos, strlen(key) );
}

...

const char* myKey = "abc";
const char* myVector = "123abc456ABC";

isKeyFoundAtPos( myKey, MyVector, 3 ); // string found
isKeyFoundAtPos( myKey, MyVector, 9 ); // string also found

I wrote the wrapper routine as an example, but honestly I would just use strnicmp() directly.

Michael
  • 2,118
  • 1
  • 19
  • 25