//这个程序用来统计一个包含有英文和中文字符串中的字符的个数,以及得到串中的某个字符
#include <iostream> #include <string> using namespace std;
/*the following program stastic the total number of the string*/ int main () { string str = "我是一个中国人,我爱我的祖国和人民,helloword!"; int count = 0; for (int i = 0; i < str.size(); i++) { if (str[i] < 0) { count++; // 中文字的最高位为1,则小于0,那么向前连续两位
i++; } else count++; } cout << "the total number of characters is: " << count << endl; cout << "the total length of the string is: " << str.length() << endl; /*找出字符串中第i个字符*/ int index = 25; int temp = 0; int j; int step = 0; /*每次递增的步长*/ for (j = 0; j <= str.size() && temp < index; j++) { if (str[j] < 0) { temp++; j++; step = 2; } else { temp++; step = 1; } } cout << "j = " << j << endl; string subStr = str.substr(j -step,step); cout << "the " << index << "character is: " << subStr << endl; return 0; }
|
阅读(524) | 评论(0) | 转发(0) |