字串反轉
#include "stdafx.h"
#includeusing namespace std;
char*reverse_str(char*str);
int _tmain(int argc, _tchar* argv)
char*reverse_str(char*str)
return dst;
}
atoi
// my_atoi.cpp : 定義控制台應用程式的入口點。
//#include "stdafx.h"
#includeusing namespace std;
int my_atoi(char*in);
int _tmain(int argc, _tchar* argv)
return sum;
}
itoa
// my_itoa.cpp : 定義控制台應用程式的入口點。
//#include "stdafx.h"
#includeusing namespace std;
char*my_itoa(int value, char *string, int radix)
string[k] = '\0';
while (k > 0)
return string;
}int _tmain(int argc, _tchar* argv)
編寫乙個程式實現功能:將兩個字串合併為乙個字串並且輸出,用指標實現。
#include "stdafx.h"#includeusing namespace std;
char*my_strcat(char*src, char*dst)
*p = '\0';
return dst;
}int _tmain(int argc, _tchar* argv)
子字串的替換(c語言)
描述:編寫乙個字串替換函式,如函式名為 strreplace(char* strsrc, char* strfind, char* strreplace),strsrc為原字串,strfind是待替換的字串,strreplace為替換字串。
舉個直觀的例子吧,如:「abcdefghijklmnopqrstuvwxyz」這個字串,把其中的「rst」替換為「gggg」這個字串,結果就變成了: abcdefghijklmnopqgggguvwxyz
// strreplace.cpp : 定義控制台應用程式的入口點。
//#include "stdafx.h"
#includeusing namespace std;
char*strreplace(char* strsrc, char* strfind, char* strreplace)
}} else
p1++;
if (flag)
break;
} if (flag)
while (*p3 != '\0')
*p = '\0';
} return strsrc;
}int _tmain(int argc, _tchar* argv)
字串分割
int split(const std::string& str, std::vector& ret_, std::string sep = ",")
std::string tmp;
std::string::size_type pos_begin = str.find_first_not_of(sep);
std::string::size_type comma_pos = 0;
while (pos_begin != std::string::npos)
else
if (!tmp.empty())
} return 0;
}
sprintf
字串格式化命令,主要功能是把格式化的資料寫入某個字串中
#include/*某個stdio.h*/
int main()/*主函式「整數」型別*/
輸出結果:
5 plus 3 is 8
下面這個函式處理文字很有用
fscanf
#include
file *stream;
int main(void)
return 0;}
1.使用sizeof獲取字串長度
sizeof的含義很明確,它用以獲取字元陣列的位元組數(當然包括結束符\0)。對於ansi字串和unicode字串,形式如下:
sizeof(cs)/sizeof(char)
sizeof(ws)/sizeof(wchar_t)
可以採用類似的方式,獲取到其字元的數目。如果遇到mbcs,如"中文abc",很顯然,這種辦法就無法奏效了,因為sizeof()並不知道哪個char是半個字元。
2.使用strlen()獲取字串長度
strlen()及wcslen()是標準c++定義的函式,它們分別獲取ascii字串及寬字串的長度,如:
size_t strlen( const char *string );
size_t wcslen( const wchar_t *string );
strlen()與wcslen()採取\0作為字串的結束符,並返回不包括\0在內的字元數目。
3.使用cstring::getlength()獲取字串長度
cstringt繼承於c******stringt類,該類具有函式:
int getlength( ) const throw( );
getlength()返回字元而非位元組的數目。比如:cstringw中,"中文abc"的getlength()會返回5,而非10。那麼對於mbcs呢?同樣,它也只能將乙個位元組當做乙個字元,cstringa表示的"中文abc"的getlength()則會返回7。
4.使用std::string::size()獲取字串長度
basic_string同樣具有獲取大小的函式:
size_type length( ) const;
size_type size( ) const;
length()和size()的功能完全一樣,它們僅僅返回字元而非位元組的個數。如果遇到mcbs,它的表現和cstringa::getlength()一樣。
5.使用_bstr_t::length()獲取字串長度
_bstr_t類的length()方法也許是獲取字元數目的最佳方案,嚴格意義來講,_bstr_t還稱不上乙個完善的字串類,它主要提供了對bstr型別的封裝,基本上沒幾個字串操作的函式。不過,_bstr_t 提供了length()函式:
unsigned int length ( ) const throw( );
該函式返回字元的數目。值得稱道的是,對於mbcs字串,它會返回真正的字元數目。
現在動手
編寫如下程式,體驗獲取字串長度的各種方法。
【程式 4-8】各種獲取字串長度的方法
01 #include "stdafx.h"
02 #include "string"
03 #include "comutil.h"
04 #pragma comment( lib, "comsuppw.lib" )
05
06 using namespace std;
07
08 int main()
09
輸出結果:
sizeof s1: 8
sizeof s2: 12
strlen(s1): 7
wcslen(s2): 5
sa.getlength(): 7
sw.getlength(): 5
ss1.size(): 7
ss2.size(): 5
bs1.length(): 5
bs2.length(): 5
字串操作 靠字串分割字串
字串分解函式。注意strtok比較複雜。要妥善運用!也可以不用strtok函式,但要實現字串靠字串分割比較困難!注意str指向的空間必須是可讀可寫的 如陣列或動態分配的空間 不能為字串常量的指標,因為strtok改變了其中的內容。include include 功能 將str中的字串按照elemon...
字串操作
字串操作 要了解字串操作首先要了解什麼是字串。前面已經提過,字串是乙個由零個或者多個字元組成的有限序列,既然是有限的那麼也就意味著字串存在乙個起始位置和乙個結束位置。我們以指定起始位置的方式來通知程式從該位置起向後的一段記憶體空間的內容應該解釋為字串。那麼這個字串在什麼地方結束呢?規定當遇到字元 0...
字串操作
include using namespace std int strlength char str char strcopy char str1,char str2 char strlink char str1,char str2 int main char strcopy char str1,c...