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
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.
easy. if(a[x++] != b[x++])return false;
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.
how about the length?
strlen();
Code:int strlen(char *something) { int i; for (i = 0; *something != '\0'; something++) i++; return i; }
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
And finally, how it's done with a char pointer!
Code:int stringlen( char *string ) { char *length = string; while( *(length++) ) ; return length - string; }