郎朗坤
Example of strcmp/* STRCMP.C */#include #include char string1[] = "The quick brown dog jumps over the lazy fox";char string2[] = "The QUICK brown dog jumps over the lazy fox";void main( void ){char tmp[20];int result;/* Case sensitive */printf( "Compare strings:\n\t%s\n\t%s\n\n", string1, string2 );result = strcmp( string1, string2 );if( result > 0 )strcpy( tmp, "greater than" );else if( result < 0 )strcpy( tmp, "less than" );elsestrcpy( tmp, "equal to" );printf( "\tstrcmp: String 1 is %s string 2\n", tmp );/* Case insensitive (could use equivalent _stricmp) */result = _stricmp( string1, string2 );if( result > 0 )strcpy( tmp, "greater than" );else if( result < 0 )strcpy( tmp, "less than" );elsestrcpy( tmp, "equal to" );printf( "\t_stricmp: String 1 is %s string 2\n", tmp );}OutputCompare strings:The quick brown dog jumps over the lazy foxThe QUICK brown dog jumps over the lazy foxstrcmp: String 1 is greater than string 2_stricmp: String 1 is equal to string 2Example of Strcpy/* STRCPY.C: This program uses strcpy* and strcat to build a phrase.*/#include #include void main( void ){char string[80];strcpy( string, "Hello world from " );strcat( string, "strcpy " );strcat( string, "and " );strcat( string, "strcat!" );printf( "String = %s\n", string );}OutputString = Hello world from strcpy and strcat!