此页面上的内容需要较新版本的 Adobe Flash Player。

获取 Adobe Flash Player

您现在的位置: 智可网 - 程序开发 - 微软开发专区 - Visual C - 正文
字符串处理
教程录入:admin    责任编辑:admin 作者:佚名 文章来源:互联网

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), warmbupt@gmail.com
* 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), warmbupt@gmail.com
* Company: IBM CDL
*
* =====================================================================================
*/

#include
#include
using namespace std;
int main(void) {
const

[1] [2] [3] [4] [5] [6] [7] [8] [9] [10] 下一页

分享
打赏我
打开支付宝"扫一扫" 打开微信"扫一扫"
客户端
"扫一扫"下载智可网App
意见反馈
字符串处理
作者:佚名 来源:互联网

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), warmbupt@gmail.com
* 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), warmbupt@gmail.com
* Company: IBM CDL
*
* =====================================================================================
*/

#include
#include
using namespace std;
int main(void) {
const

[1] [2] [3] [4] [5] [6] [7] [8] [9] [10] 下一页