程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> VC >> vc教程 >> 字符串處理

字符串處理

編輯:vc教程

1.本章思維導圖:

C++學習:字符串處理(圖一)

C++學習:字符串處理(圖二)

點擊查看大圖

C++學習:字符串處理(圖三) 

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] 下一頁

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved