Posted on 2004-11-09
Last Modified: 2009-07-29

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.
Question by:icysmarty
    LVL 7

    Expert Comment

    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)

    Author Comment

    strcmp does not work for my case cause the array has fixed-length record of playername

    array ={superman--ultraman--hero------andsoon---}
    - stands for space
    LVL 7

    Accepted Solution

    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;


    Author Comment

    Yeah, you are right.
    I should do that.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Article by: SunnyDark
    This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
    Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now