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;
}
*字符串常量(即字符串字面量)的一些问题
*处理字符串的一些函数
*/
#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;
}
相关文章推荐
- C风格字符串和C++的标准库类型string的比较
- c++基本类型&&&c++string与c风格字符串的相互转化
- 【C++】【日志贴】浅谈标准库类型string、vector及C风格字符串在全局和局部作用域中默认初始值情况
- C++ 中string类型和 C风格 char类型字符串比较
- 【C++】学习笔记三十三——函数和C风格字符串
- 【C++学习笔记】数组和指针再C-风格字符串的演示
- C++字符串与数值——安全的类型转换扩展
- C++中字符串与数字类型之间的转换
- C++ leveldb Slice的数据类型 不一定 必须是字符串。也可以是二进制
- More Effective C++----(1)指针与引用的区别 & (2)尽量使用C++风格的类型转换 & (3)不要对数组使用多态
- C++任意类型转化成字符串
- C++临时变量的另类应用:基于iostream的类型安全的log接口设计
- C风格字符串和string类型的相互转换
- C/C++中int/long/float/double数值类型与字符串互相转换
- C++学习笔记(第六章 指针的应用 字符串 引用 之二)
- c风格字符串和C++的string在文件名方面的使用注意
- C++中的字符串类型及相互转化
- 输入带空格的string类型字符串 c++
- Android应用资源---字符串资源类型(String)
- C/C++字符串处理(3):String ADT - 字符串只是抽象数据类型