need function like strncmp(str1, str2, n)

This is a discussion on need function like strncmp(str1, str2, n) within the C Programming forums, part of the General Programming Boards category; Is there a function like strncmp(str1, str2, n) (im not sure if parameters go in that order) but "n" would ...

  1. #1
    evader
    Guest

    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
    Posts
    132
    assuming both strings are at least n characters long:

    strcmp(str1+n, str2+n);

    alex

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    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 :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    evader
    Guest

    Question

    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
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    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:
    Code:
    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
    Unregistered
    Guest
    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, 04:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21