1.本章思維導圖:
Example1:
char *strcpy(char *target, const char *source) {
char *t = target;
// Copy the contents of source into target.
while(*source) *target++ = *source++;
// Null-terminate the target.
*target = '\0';
// Return pointer to the start of target.
return t;
}
Example2:
void *memmove(void *target, const void *source, size_t count)
這個函數即使是在源和目的字符串有所重疊時操作也能成功,雖然source為const,但是其指向的array也可能被修改。
2. C型字符串操作實例:
Ex1.基本操作
/*
* =====================================================================================
*
* Filename: 2-1.cpp
*
* Description: Fundamental Operations in C Type String
*
* Version: 1.0
* Created: 05/11/2010 10:43:11 AM
* Revision: none
* Compiler: gcc
*
* Author: gnuhpc (http://blog.csdn.Net/gnuhpc), [email protected]
* Company: IBM CDL
*
* =====================================================================================
*/
#include
#include
using namespace std;
int main(int argc, char *argv[])
{
char strA[7]="UP";
char strB[5]="DOWN";
char strC[5]="LEFT";
char strD[6]="RIGHT";
/*Display */
cout << "Here are the strings: " << endl;
cout << "strA: " << strA << endl;
cout << "strB: " << strB << endl;
cout << "strC: " << strC << endl;
cout << "strD: " << strD << "\n\n";
//Display the length of strA.
cout << "Length of strA is " << strlen(strA) << endl;
cout << "Size of strA is " << sizeof(strA) << endl;
//Concatenate strB with strA
cout << "The result of Concatenate is strA::" <
//Copy strC into strB,and partially strD into strA
cout << "The result of Copy is:" < cout << "The result of partially Copy is strA:" <
//Compare strC with strB
if( !strcmp(strC,strB))
{
cout << "strC is equal to strB!"< }
if( !strncmp(strD,strA,3))
{
cout << "strD is equal to strA partially!"< }
return 0;
}
Ex2.搜索匹配相關操作
/*
* =====================================================================================
*
* Filename: 2-2.cpp
*
* Description: Search Operation in C type String
*
* Version: 1.0
* Created: 05/11/2010 11:38:15 AM
* Revision: none
* Compiler: gcc
*
* Author: gnuhpc (http://blog.csdn.Net/gnuhpc), [email protected]
* Company: IBM CDL
*
* =====================================================================================
*/
#include
#include
using namespace std;
int main(void) {
const
[1] [2] [3] [4] [5] [6] [7] [8] [9] [10] 下一頁