Thread: Help! Sample prog. on user-define string functions

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    2

    Help! Sample prog. on user-define string functions

    Hello! People... i just want to know how to program a string function.
    example: strcmp();
    or strcpy();without using their function. User define funtion..tnx
    Last edited by orbandjay; 08-17-2002 at 01:32 AM.

  2. #2
    Sebastiani
    Guest
    easy. if(a[x++] != b[x++])return false;

  3. #3
    Unregistered
    Guest
    Becauce when you are working with string(char *) you have to have a solid understanding how pointers (and arrays) work.

    I suggest you learn pointerarithmetic in depth.

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    2

    ???

    how about the length?
    strlen();

  5. #5
    Unregistered
    Guest
    Code:
    int strlen(char *something)
    {
    int i;
    
           for (i = 0; *something != '\0'; something++)
               i++;
           return i;
    }

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    String length can also be written as...

    Code:
    int StringLenth( char *pString )
    {
      int length = 0;
    
      if( !pString ) // If invalid string, return 0 length
        return( length );
      
      while( *pString++ ) // go through each element
        ++length;
    
      return( length );
    }
    Last edited by MrWizard; 08-17-2002 at 03:22 AM.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  7. #7
    Sebastiani
    Guest
    And finally, how it's done with a char pointer!

    Code:
    int stringlen( char *string )
     {
       char *length = string;
    
       while( *(length++) ) ;
    
       return length - string;
     }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer within a Struct
    By Bladactania in forum C Programming
    Replies: 11
    Last Post: 04-03-2009, 10:20 PM
  2. Help me with function call
    By NeMewSys in forum C++ Programming
    Replies: 16
    Last Post: 05-22-2008, 01:53 PM
  3. Accessing syscalls from C
    By lilcoder in forum C Programming
    Replies: 17
    Last Post: 09-19-2007, 02:27 PM
  4. C Programming 2d Array Question
    By jeev2005 in forum C Programming
    Replies: 3
    Last Post: 04-26-2006, 03:18 PM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM