几个操作字符串的函数
来源:互联网 发布:数据库设计shijian报告 编辑:程序博客网 时间:2024/04/30 11:25
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 12345678901234567890123456789012345678901234567890Search char: rResult: first r found at position 12Result: 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 12345678901234567890123456789012345678901234567890Search char: rResult: first r found at position 12Result: 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 12345678901234567890123456789012345678901234567890Search char: rResult: first r found at position 12Result: 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 12345678901234567890123456789012345678901234567890Search char: rResult: first r found at position 12Result: 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!
0 0
- 几个操作字符串的函数
- 字符串操作函数的几个基本函数
- 几个字符串操作的汇编函数
- 关于几个字符串操作的函数
- 几个字符串简单操作函数
- 字符串的几个操作
- MFC中几个有用的字符串操作函数
- MFC中几个有用的字符串操作函数
- 几个常用的字符串操作
- 几个字符串和内存操作函数
- 关于字符串的几个函数
- 几个字符串函数的实现
- 几个字符串函数的实现
- 位操作的几个函数
- 文件操作的几个函数
- 文件操作的几个函数
- 操作字符串的函数
- 字符串操作的函数
- 十大优秀免费空间排行榜-基于稳定实用可靠因素分析
- 自动检测iOS网络并可跳转至设置界面设置网络
- 个人分享 站长们容易忽略的代码优化
- Ubuntu for Arm根文件系统制作与SPICE安装
- Qt Model/View 学习笔记 (二)
- 几个操作字符串的函数
- 使用maven打包及发布源码
- SQL常用函数
- 十大免费域名排行榜-免费顶级域名,免费二级域名申请使用总结
- DCMTK+VS2012编译简明教程
- ajax同步与异步
- Qt Model/View 学习笔记 (三)
- 局部变量前不能放置任何访问修饰符
- [转]如何修改MyEclipse中的author的默认名字