Thread: need function like strncmp(str1, str2, n)

  1. #1

    Post need function like strncmp(str1, str2, n)

    Is there a function like strncmp(str1, str2, n) (im not sure if parameters go in that order) but "n" would be the number where the strings would start to be compared not where they would end comparing?

  2. #2
    Registered User alex's Avatar
    Join Date
    Sep 2001
    assuming both strings are at least n characters long:

    strcmp(str1+n, str2+n);


  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    yes your right there is a strncmp. Its prototyped like this :-

    int strncmp(const char* s1,const char* s2,size_t num);

    you will find it in string.h just like the other string functions.It will compare up to num chars from the two null-terminated strings. return values are as for strcmp.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :-

  4. #4


    I have a similar question. If I find a string using strstr(str1, sentence) how would I be able to get the position number of where that word begins?
    eg. strstr("Person walking.", "walking"); /* walking begins at position 7 or 8 depending if counting starts from 0 */
    Then I would assign i=/* the return value of position of that special function */

  5. #5
    Join Date
    Aug 2001
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    strstr return a pointer to the begining of the sub string. At least that's how I remember it. So than assign a pointer to the char array:
    char array[40] = "My Sentence ...";
    char *pstart = array;
    //call strstr
    char *pend = strstr(array,"Sentence");
    int position = pend - pstart;
    At least I think something like that should do the job.
    I compile code with:
    Visual Studio.NET beta2

  6. #6
    True. If you get the actual address were the the start of the word is found and then subtract the address were the original array starts that will tell you how many places into the array the search had to go before finding it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM