function code?

This is a discussion on function code? within the C Programming forums, part of the General Programming Boards category; Does anybody know where to find the code for the function(s) strcmp() or strcasecmp() . I need the actual code, ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    34

    function code?

    Does anybody know where to find the code for the function(s) strcmp() or strcasecmp(). I need the actual code, not the header file for them. Thanks for any help...

  2. #2
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Greetings,

    There are a couple of ways this function can be written. Some use for loops, while loops, do-while loops, and so forth, but it all comes down to the end of comparing string1 to string2.

    All we really have to do to write such a function is:
    • Loop through string1 and string2
    • Check if current character of string1 is identical to string2
    • Check existance of current character for string1 and string2 [make sure they are valid]
    • Increment both places if they match
    • Return difference of string1 and string2

    If this makes sense, then writing a simple verison shouldn't be difficult. If you have further questions, please feel free to ask.

    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  3. #3
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    /***
    *strcmp.c - routine to compare two strings (for equal, less, or greater)
    *
    * Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
    *
    *Purpose:
    * Compares two string, determining their lexical order.
    *
    ************************************************** *****************************/

    #include <cruntime.h>
    #include <string.h>

    #ifdef _MSC_VER
    #pragma function(strcmp)
    #endif /* _MSC_VER */

    /***
    *strcmp - compare two strings, returning less than, equal to, or greater than
    *
    *Purpose:
    * STRCMP compares two strings and returns an integer
    * to indicate whether the first is less than the second, the two are
    * equal, or whether the first is greater than the second.
    *
    * Comparison is done byte by byte on an UNSIGNED basis, which is to
    * say that Null (0) is less than any other character (1-255).
    *
    *Entry:
    * const char * src - string for left-hand side of comparison
    * const char * dst - string for right-hand side of comparison
    *
    *Exit:
    * returns -1 if src < dst
    * returns 0 if src == dst
    * returns +1 if src > dst
    *
    *Exceptions:
    *
    ************************************************** *****************************/

    int __cdecl strcmp (
    const char * src,
    const char * dst
    )
    {
    int ret = 0 ;

    while( ! (ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)
    ++src, ++dst;

    if ( ret < 0 )
    ret = -1 ;
    else if ( ret > 0 )
    ret = 1 ;

    return( ret );
    }
    The other one does not exist on my machine.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    Way to go posting the homework answers

  5. #5
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    I hardly think "Find where strcmp() is located and then print off the source code would be a homework question"...

    And if the homework question is, "Write a function that compares two strings", how smart would it be to hand-in strcmp() and expect nobody to notice?

    I'm a bit vague as to where the problem is with displaying code that is readily available on anybody's machine that has the C Language installed. Granted, I could have made it a bit more challenging by suggesting a search for "String.c*" on their computer, or to look up the Definition through MSVC++ 6.0, but still, 5 frowny faces seem a bit excessive to me.

  6. #6
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Quote Originally Posted by Salem
    Way to go posting the homework answers
    Sometimes our efforts seem feckless. The fast path to answers will only suffice for a short while. Or so I hope.

    Quote Originally Posted by Epo
    I hardly think "Find where strcmp() is located and then print off the source code would be a homework question"...
    I beg to differ. Check these out:


    Even though the homework deadlines may be 10 years old, roughly, this tells us that there are homework questions like this.

    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  7. #7
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Compared to your two examples of homework assignments where it seems the task is to re-create a similar strcmp() function, how many people out there do you think genuinely would like to know how strcmp() works, so that they actually know what's going on when they use it?

    It's great to know that strcmp() returns 0 if the strings are equal, scanf() can return 0 if the scan fails, or the number of validly entered arguments if it does not fail, but isn't another good question: "Why?".

  8. #8
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    > how many people out there do you think genuinely would like to know how strcmp() works, so that they actually know what's going on when they use it?
    Alot of people. They may not need the experience now, but why wait when you need it?

    Let's make a parallel. "How do computers work?" I'm pretty sure some people know how they work and can build/re-build a machine if they wanted too. If someone else built a machine before-hand does not mean you can too, unless you know how. Though in order to do this, you have to know how it works. One thing misplaced, and the machine may not run optimally, or at all.

    All in all, re-writing a function that already exists can help the programmer understand how it works. Asking a programmer to write a function that compares two strings has the same concept as if they were asked to compare two files. The logic of code is better understood learned rather than given.


    - Stack Overflow
    Last edited by Stack Overflow; 11-16-2004 at 03:54 PM. Reason: More info.
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  9. #9
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by egomaster69
    strcasecmp()
    strcasecmp is the name of the PHP function to compare strings disregarding case. In C it's stricmp (note the 'i') and it's declared in string.h. stricmp is part of ANSI C standard, so don't bother using it.

  10. #10
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Quote Originally Posted by xErath
    stricmp is part of ANSI C standard, so don't bother using it.
    The stricmp() case-insensitive string comparison function is not part of the C standard, but it is a common extension on many C compilers.

    Furthermore, stricmp is NOT part of the C or C++ standard. Many compilers support this function, but it's an addon, or compiler specific function, and not part of either standards.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  11. #11
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Upps.. I though it was.. Been goggling a bit. I did found however some std info on strings and stricmp wasn't there. Thanks Stack Overflow for correcting me

    But still don't bother using stricmp. It's a very popular function.

  12. #12
    </life>
    Join Date
    Oct 2004
    Posts
    83
    Any lecturer would know that this code is not of a student:

    Code:
    int __cdecl strcmp (
    const char * src,
    const char * dst
    )
    {
    int ret = 0 ;
    
    while( ! (ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)
    ++src, ++dst;
    
    if ( ret < 0 )
    ret = -1 ;
    else if ( ret > 0 )
    ret = 1 ;
    
    return( ret );
    }
    And a great deal of people need to see something before they truely undertand it. People learn in different ways, but as an answer to homework this code would not be any benefit by a simple copy and paste, re-writing the function (in a more simplistic way i imagine) would be the outcome and an understanding would be achieved.
    Microsoft is merely an illusion, albeit a very persistant one.

  13. #13
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    Quote Originally Posted by Salem
    Way to go posting the homework answers

    way to go posting intellectual property of someone else you mean...

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well now that depends. I've never taken a C class, but I venture, were I to do so, I could turn in some code the teacher wouldn't expect. For example, here's the answer to one of the more common homework problems:
    Code:
    void O_Oy( char *o_o, int o_O )
    {
            char V_V[BUFSIZ]="%s\n";
            char T_T[BUFSIZ]="%%[^%s]";
            char v_v[BUFSIZ]={0};
            char x_x[BUFSIZ]={0};
    
            while( o_O -- )
            {
                    sprintf( v_v, T_T, o_o + o_O + 1  );
                    sscanf( o_o, v_v, x_x );
                    printf( V_V, x_x );     
            }
    }
    
    int main( void )
    {
            char o_o[] = "1234567890";
    
            O_Oy( o_o, 0x9 );
    
            return 0;
    }
    At any rate, you're intentionally missing the point. Go read the announcements. Read my SIG while you're at it.

    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by jwenting
    way to go posting intellectual property of someone else you mean...
    /***
    *strcmp.c - routine to compare two strings (for equal, less, or greater)
    *
    * Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
    *
    *Purpose:
    * Compares two string, determining their lexical order.
    *
    ************************************************** *****************************/
    The copyright is there...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  4. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 08:10 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

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