C風格類型的字符串在C++中的應用

/*
 *字符串常量(即字符串字面量)的一些問題
 *處理字符串的一些函數
 */

#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;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章