64,654
社区成员
发帖
与我相关
我的任务
分享
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int gbk_strlen(const char* str);
main()
{
string str[4] = {"周玥座位","中国人123","上海","北漂"};
for (int i = 0; i < sizeof(str); i++) {
char p[20];
strcpy(p,str[i].data());
int strLength=gbk_strlen(p);
cout<<str[i]+" length长度为: ";
cout<<strLength<<endl;
}
}
int gbk_strlen(const char* str)
{
const char* p = str; //p用于后面遍历
while(*p) //若是结束符0,则结束循环
{
if(*p < 0 && (*(p+1)<0 || *(p+1) < 63)) //中文汉字情况
{
str++; //str移动一位,p移动移动2位,因此长度加1
p += 2;
}
else
{
p++; //str不动,p移动一位,长度加1
}
}
return p-str; //返回地址之差
}
//凡是?。!后面跟1~1000后面跟半角.的,在?。!后面加回车换行。
//in.txt:
//1.测试。2.测试2?3.测试3!4.测试
//四。5.测试。6.测试6?7.测试3!8.测试
//运行该程序将输出重定向到比如out.txt即可将输出保存到文件out.txt中
#include <iostream>
#include <fstream>
#include <string>
#include <regex>
using namespace std;
int main() {
wifstream wifs("in.txt");
wifs.imbue(locale("chs"));
wstring wstr(L""),wln;
while (wifs) {
getline(wifs,wln);
wstr+=wln;
}
wifs.close();
wcout.imbue(locale("chs"));
wcout << wstr << endl;
wstring rs = L"([?。!])(\\d{1,3}\\.)";
wregex expression(rs);
wstr = regex_replace(wstr, expression, wstring(L"$1\r\n$2"));
wcout << wstr << endl;
return 0;
}
//1.测试。2.测试2?3.测试3!4.测试四。5.测试。6.测试6?7.测试3!8.测试
//1.测试。
//2.测试2?
//3.测试3!
//4.测试四。
//5.测试。
//6.测试6?
//7.测试3!
//8.测试
//