C++中汉字字符串的操作问题,请大神讲解一下
程序代码:
#include<iostream> int replace_(char* str,char c1,char c2);////功能:将C风格的str字符串的中c1字符全换成c2字符,并返回替换次数 ///字符串为英文时是没问题,中文就有问题了,请问是编译软件不识别中文的问题还是说汉字在字符串中的存储机制导致的呢? int main() { using namespace std; char str[]="李明是个大笨蛋."; cout<<"the str[] is: "<<str<<endl; cout<<"it's had been changed "<<replace_(str,'李','张') <<" times and after replace function, the str[] is:\n" <<str<<endl; } int replace_(char* str,char c1,char c2) { int count_=0; while(*str!='\0') { if(*str==c1) { *str=c2; count_++; } str++; } return count_; }
[此贴子已经被作者于2016-1-13 15:50编辑过]