memcpy:
#include <memory.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
int main()
{
char* src="hello world";
size_t count = strlen(src);
char* des= (char*)calloc(count+1,sizeof(char));
//char* des =(char*) malloc(count);
memcpy(des,src,count);
cout<<strlen(des)<<endl;
cout<<des<<endl;
cout<<count<<endl;
return 0;
}
这里需要注意的就是给字符串分配内存的时候一般需要多加一个字节用于存储NULL terminated character即结束字符'\0'。所以count +1,,否则会去寻找结束字符来结束。
VC语言的空字符是在字符串结尾系统自动加上的‘\0’,以让系统识别出一个字符串的结尾。
如
字符串“china”
实际上在系统内是以“china\0”储存的
strchr:
// crt_strchr.c // // This program illustrates searching for a character // with strchr (search forward) or strrchr (search backward). // #include <string.h> #include <stdio.h> int ch = 'r'; char string[] = "The quick brown dog jumps over the lazy fox"; char fmt1[] = " 1 2 3 4 5"; char fmt2[] = "12345678901234567890123456789012345678901234567890"; int main( void ) { char *pdest; int result; printf_s( "String to be searched:\n %s\n", string ); printf_s( " %s\n %s\n\n", fmt1, fmt2 ); printf_s( "Search char: %c\n", ch ); // Search forward. pdest = strchr( string, ch ); result = (int)(pdest - string + 1); if ( pdest != NULL ) printf_s( "Result: first %c found at position %d\n", ch, result ); else printf_s( "Result: %c not found\n", ch ); // Search backward. pdest = strrchr( string, ch ); result = (int)(pdest - string + 1); if ( pdest != NULL ) printf_s( "Result: last %c found at position %d\n", ch, result ); else printf_s( "Result:\t%c not found\n", ch ); }
String to be searched: The quick brown dog jumps over the lazy fox 1 2 3 4 5 12345678901234567890123456789012345678901234567890 Search char: r Result: first r found at position 12 Result: last r found at position 30strcmp:
// crt_strchr.c // // This program illustrates searching for a character // with strchr (search forward) or strrchr (search backward). // #include <string.h> #include <stdio.h> int ch = 'r'; char string[] = "The quick brown dog jumps over the lazy fox"; char fmt1[] = " 1 2 3 4 5"; char fmt2[] = "12345678901234567890123456789012345678901234567890"; int main( void ) { char *pdest; int result; printf_s( "String to be searched:\n %s\n", string ); printf_s( " %s\n %s\n\n", fmt1, fmt2 ); printf_s( "Search char: %c\n", ch ); // Search forward. pdest = strchr( string, ch ); result = (int)(pdest - string + 1); if ( pdest != NULL ) printf_s( "Result: first %c found at position %d\n", ch, result ); else printf_s( "Result: %c not found\n", ch ); // Search backward. pdest = strrchr( string, ch ); result = (int)(pdest - string + 1); if ( pdest != NULL ) printf_s( "Result: last %c found at position %d\n", ch, result ); else printf_s( "Result:\t%c not found\n", ch ); }
String to be searched: The quick brown dog jumps over the lazy fox 1 2 3 4 5 12345678901234567890123456789012345678901234567890 Search char: r Result: first r found at position 12 Result: last r found at position 30
// crt_strcmp.c #include <string.h> #include <stdio.h> #include <stdlib.h> char string1[] = "The quick brown dog jumps over the lazy fox"; char string2[] = "The QUICK brown dog jumps over the lazy fox"; int main( void ) { char tmp[20]; int result; // Case sensitive printf( "Compare strings:\n %s\n %s\n\n", string1, string2 ); result = strcmp( string1, string2 ); if( result > 0 ) strcpy_s( tmp, _countof(tmp), "greater than" ); else if( result < 0 ) strcpy_s( tmp, _countof (tmp), "less than" ); else strcpy_s( tmp, _countof (tmp), "equal to" ); printf( " strcmp: String 1 is %s string 2\n", tmp ); // Case insensitive (could use equivalent _stricmp) result = _stricmp( string1, string2 ); if( result > 0 ) strcpy_s( tmp, _countof (tmp), "greater than" ); else if( result < 0 ) strcpy_s( tmp, _countof (tmp), "less than" ); else strcpy_s( tmp, _countof (tmp), "equal to" ); printf( " _stricmp: String 1 is %s string 2\n", tmp ); }
Compare strings: The quick brown dog jumps over the lazy fox The QUICK brown dog jumps over the lazy fox strcmp: String 1 is greater than string 2 _stricmp: String 1 is equal to string 2strcpy:
// crt_strchr.c // // This program illustrates searching for a character // with strchr (search forward) or strrchr (search backward). // #include <string.h> #include <stdio.h> int ch = 'r'; char string[] = "The quick brown dog jumps over the lazy fox"; char fmt1[] = " 1 2 3 4 5"; char fmt2[] = "12345678901234567890123456789012345678901234567890"; int main( void ) { char *pdest; int result; printf_s( "String to be searched:\n %s\n", string ); printf_s( " %s\n %s\n\n", fmt1, fmt2 ); printf_s( "Search char: %c\n", ch ); // Search forward. pdest = strchr( string, ch ); result = (int)(pdest - string + 1); if ( pdest != NULL ) printf_s( "Result: first %c found at position %d\n", ch, result ); else printf_s( "Result: %c not found\n", ch ); // Search backward. pdest = strrchr( string, ch ); result = (int)(pdest - string + 1); if ( pdest != NULL ) printf_s( "Result: last %c found at position %d\n", ch, result ); else printf_s( "Result:\t%c not found\n", ch ); }
String to be searched: The quick brown dog jumps over the lazy fox 1 2 3 4 5 12345678901234567890123456789012345678901234567890 Search char: r Result: first r found at position 12 Result: last r found at position 30
// crt_strcmp.c #include <string.h> #include <stdio.h> #include <stdlib.h> char string1[] = "The quick brown dog jumps over the lazy fox"; char string2[] = "The QUICK brown dog jumps over the lazy fox"; int main( void ) { char tmp[20]; int result; // Case sensitive printf( "Compare strings:\n %s\n %s\n\n", string1, string2 ); result = strcmp( string1, string2 ); if( result > 0 ) strcpy_s( tmp, _countof(tmp), "greater than" ); else if( result < 0 ) strcpy_s( tmp, _countof (tmp), "less than" ); else strcpy_s( tmp, _countof (tmp), "equal to" ); printf( " strcmp: String 1 is %s string 2\n", tmp ); // Case insensitive (could use equivalent _stricmp) result = _stricmp( string1, string2 ); if( result > 0 ) strcpy_s( tmp, _countof (tmp), "greater than" ); else if( result < 0 ) strcpy_s( tmp, _countof (tmp), "less than" ); else strcpy_s( tmp, _countof (tmp), "equal to" ); printf( " _stricmp: String 1 is %s string 2\n", tmp ); }
Compare strings: The quick brown dog jumps over the lazy fox The QUICK brown dog jumps over the lazy fox strcmp: String 1 is greater than string 2 _stricmp: String 1 is equal to string 2
// crt_strcpy.c // compile with: /W3 // This program uses strcpy // and strcat to build a phrase. #include <string.h> #include <stdio.h> int main( void ) { char string[80]; // If you change the previous line to // char string[20]; // strcpy and strcat will happily overrun the string // buffer. See the examples for strncpy and strncat // for safer string handling. strcpy( string, "Hello world from " ); // C4996 // Note: strcpy is deprecated; use strcpy_s instead strcat( string, "strcpy " ); // C4996 // Note: strcat is deprecated; use strcat_s instead strcat( string, "and " ); // C4996 strcat( string, "strcat!" ); // C4996 printf( "String = %s\n", string ); }
String = Hello world from strcpy and strcat!
// crt_strchr.c // // This program illustrates searching for a character // with strchr (search forward) or strrchr (search backward). // #include <string.h> #include <stdio.h> int ch = 'r'; char string[] = "The quick brown dog jumps over the lazy fox"; char fmt1[] = " 1 2 3 4 5"; char fmt2[] = "12345678901234567890123456789012345678901234567890"; int main( void ) { char *pdest; int result; printf_s( "String to be searched:\n %s\n", string ); printf_s( " %s\n %s\n\n", fmt1, fmt2 ); printf_s( "Search char: %c\n", ch ); // Search forward. pdest = strchr( string, ch ); result = (int)(pdest - string + 1); if ( pdest != NULL ) printf_s( "Result: first %c found at position %d\n", ch, result ); else printf_s( "Result: %c not found\n", ch ); // Search backward. pdest = strrchr( string, ch ); result = (int)(pdest - string + 1); if ( pdest != NULL ) printf_s( "Result: last %c found at position %d\n", ch, result ); else printf_s( "Result:\t%c not found\n", ch ); }
String to be searched: The quick brown dog jumps over the lazy fox 1 2 3 4 5 12345678901234567890123456789012345678901234567890 Search char: r Result: first r found at position 12 Result: last r found at position 30
// crt_strcmp.c #include <string.h> #include <stdio.h> #include <stdlib.h> char string1[] = "The quick brown dog jumps over the lazy fox"; char string2[] = "The QUICK brown dog jumps over the lazy fox"; int main( void ) { char tmp[20]; int result; // Case sensitive printf( "Compare strings:\n %s\n %s\n\n", string1, string2 ); result = strcmp( string1, string2 ); if( result > 0 ) strcpy_s( tmp, _countof(tmp), "greater than" ); else if( result < 0 ) strcpy_s( tmp, _countof (tmp), "less than" ); else strcpy_s( tmp, _countof (tmp), "equal to" ); printf( " strcmp: String 1 is %s string 2\n", tmp ); // Case insensitive (could use equivalent _stricmp) result = _stricmp( string1, string2 ); if( result > 0 ) strcpy_s( tmp, _countof (tmp), "greater than" ); else if( result < 0 ) strcpy_s( tmp, _countof (tmp), "less than" ); else strcpy_s( tmp, _countof (tmp), "equal to" ); printf( " _stricmp: String 1 is %s string 2\n", tmp ); }
Compare strings: The quick brown dog jumps over the lazy fox The QUICK brown dog jumps over the lazy fox strcmp: String 1 is greater than string 2 _stricmp: String 1 is equal to string 2
// crt_strcpy.c // compile with: /W3 // This program uses strcpy // and strcat to build a phrase. #include <string.h> #include <stdio.h> int main( void ) { char string[80]; // If you change the previous line to // char string[20]; // strcpy and strcat will happily overrun the string // buffer. See the examples for strncpy and strncat // for safer string handling. strcpy( string, "Hello world from " ); // C4996 // Note: strcpy is deprecated; use strcpy_s instead strcat( string, "strcpy " ); // C4996 // Note: strcat is deprecated; use strcat_s instead strcat( string, "and " ); // C4996 strcat( string, "strcat!" ); // C4996 printf( "String = %s\n", string ); }
String = Hello world from strcpy and strcat!