I am experiencing problem with following statement
int comp = strncmp((array+middle),target,strlen(target));

I am trying to compare the target (player name) with the player name at array+middle position
However, by using strncmp, I do not get expected result because of 'strlen(target)'

For instance,

array+middle  --> super_player
target --> su

this two are not same but in fact the statement return 0 since I am only comparing su with super_player

I am wondering how can I compare su with the entire "super_player" instead of su

This statement is for comparison in my  binary search statement.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

you don't need strncmp for that.

array+middle is a perfectly valid string (e.g. if array == "the super_player", array+4=="super_player")
so in fact you can use

strcmp(array+middle, target)
icysmartyAuthor Commented:
strcmp does not work for my case cause the array has fixed-length record of playername

array ={superman--ultraman--hero------andsoon---}
- stands for space
for most scenarios, it would be best to split up the arreay (e.g. using strtok). If this is not an option, or the comparison the only problem, you can do the following:

   int targetLen = strlen(target);
   char * spacePosPtr = strchr(array+middle);  // find space position
   // calculate comparison length
   int cmpLen = spacePosPtr ? spacePosPtr-(array+middle) : strlen(array+middle);
   // compare only if lengths are the same
   bool equal  (cmpLen == targetLen) ? strncmp(array+middle,target,targetLen)==0 : false;


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
icysmartyAuthor Commented:
Yeah, you are right.
I should do that.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.