您的位置:首页 > 编程语言 > C语言/C++

C风格类型的字符串在C++中的应用

2012-09-17 19:31 459 查看
/*

*字符串常量(即字符串字面量)的一些问题

*处理字符串的一些函数

*/

#include <iostream>

using namespace std;

#include <cstring>

int main()

{

/* 字符串字面量在内存中也是以字符数组存放,并以'\0'结尾;

如果想输出字符串字面量中的某个字符或从某个字符开始的子字符串时,可将

该字符串字面量当作数组名使用,并配以相应的下标即可。

数组名是常量,不是变量,因此不能用来存放数据,也不能对其赋值,不能自增、自减,

但可以加减某一变量i 。*/

cout << "字符串字面量:" << "I Love You,but Ju.J!" << endl;

cout << "长度:Length = " << sizeof("I Love You,but Ju.J!") << endl;//包括空字符在内

cout << "取其中的某一字符:" << "I Love You,but Ju.J!"[0] << endl;

cout << "取地址:" << &"I Love You,but Ju.J!" << endl;

for(int i = 3; i != sizeof("I Love You,but Ju.J!"); i++)

cout << "I Love You,but Ju.J!"[i];

cout << endl;

//字符串字面量存储在只读存储区,不能给只读位置赋值。

//assignment of read-only location ‘"I Love You,but Ju.J!"[0]’

//(分配、指派)、(只读区域)

//"I Love You,but Ju.J!"[0] = 'J';

// assignment of read-only location ‘"I Love You,but Ju.J!"’

//incompatible types in assignment of ‘const char [8]’ to ‘const char [21]’

//(不相容的类型)

//"I Love You,but Ju.J!" = "Jin.P.P";

char *str = "I Love You,but Ju.J!";

cout << str << endl;

//处理字符串的函数,在这些函数中用到const 是保证该数组的内容在函数调用期间不会被修改

char StrDest[80] = "Love you,but not Ju.J!";

cout << "数组StrDest的长度:" << sizeof(StrDest) << endl;

//字符串长度函数strlen:size_t strlen( const char *string );

//测试字符串的实际长度,不包括'\0'

cout << "字符串StrDest的长度:" << strlen(StrDest) << endl;

char StrSource[] = "Where my love,Jin.P.P?";

cout << "数组StrSource的长度:" << sizeof(StrSource) << endl;

cout << "字符串StrSource的长度:" << strlen(StrSource) << endl;

//字符串比较函数strcmp:int strcmp( const char *string1, const char *string2 );

//自左至右逐个字符相比较,比较的是ASCII码值的大小,以出现的第一个不相同的字符比较结果为准

if(strcmp(StrDest,StrSource) == 0)

cout << "字符串StrDest与StrSource相等。" << endl;

if(strcmp(StrDest,StrSource) > 0)

cout << "字符串StrDest大于StrSource" << endl;

if(strcmp(StrDest,StrSource) < 0)

cout << "字符串StrDest小于StrSource" << endl;

//字符串连接函数strcat:char *strcat( char *strDestination, const char *strSource );

//函数调用完后的返回值就是第一个字符数组的地址

strcat(StrDest,StrSource);

cout << "连接后的字符串:" << StrDest << endl;

//字符串复制函数strcpy:char *strcpy( char *strDestination, const char *strSource );

//源字符串会将目的字符串中相应位置的字符覆盖掉。(结果显示的只有覆盖后的字符串)

strcpy(StrDest,StrSource);

cout << "复制后输出的内容:" << StrDest << endl;

//在字符串中查找字符c(第一次出现):char *strchr( const char *string, int c );

//并且输出以字符c 开头的子字符串,最好是先判断一下strchr 返回的指针是否为空

char *pch = strchr(StrSource,'h');

if(pch != NULL)

{

//cout << "&(*pch)= " << &(*pch) << endl;

cout << "*pch = " << *pch << endl;

cout << "pch = " << pch << endl;

cout << "(pch - StrSource +1) = " << pch - StrSource +1 << endl;

}

//cout << "strchr(StrSource,'J') = " << strchr(StrSource,'J') << endl;

//在字符串中查找字符c(从字符串的右边往左查找,直到第一次出现c),然后输出以字符c 开头的子字符串

//char *strrchr( const char *string, int c );

char* pr = strrchr(StrSource,'e');

if(pr != NULL)

cout << "pr = " << pr << endl;

//在字符串中查找子串:char *strstr( const char *string, const char *strCharSet );

char* pStr = strstr(StrSource,"here");

if(pStr != NULL)

cout << "pStr = " << pStr << endl;

//cout << strstr(StrSource,"here") << endl;

//下面sizeof(Str) 取得是指针Str在内存中的存储空间的大小---4个字节

//char *Str;

//cout << "指针在内存中存储空间的大小:" << sizeof(Str) << endl;

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: