#include <stdio.h>
#include <string.h>
int main()
{
char szText[] = "我是CIW.";
char szChinese[3] = {0};
int i = 0, nLen = strlen(szText);
for(; i < nLen; i++)
{
if( szText[i] >= 0 && szText[i] <= 127 ) //不是全角字符?
{
printf("%c\n", szText[i]);
}
else //是全角字符
{
szChinese[0] = szText[i];
szChinese[1] = szText[i + 1];
printf("%s\n", szChinese);
i++; //中文是2个字节,所以i++
}
}
return 0;
}
计算文件大小
#include <sys/types.h>
#include <sys/stat.h>
#include <iostream.h>
#include <string>
using namespace std;
int getFileSize(string sFileName)
{
struct stat buf;
int iRet = stat(sFileName.c_str(), &buf);
if (iRet == -1)
return NULL;
return buf.st_size;
}
void main()
{
int i = getFileSize("E:\\反恐精英csV1.6中文版.exe");
cout<<i<<endl;
}
去除字符串空格 反转字符串
#include<iostream>
#include<string>
using namespace std;
void strallcut(char *str)
{
int i,j=0;
char sp[512];
for (i = 0; *(str + i) != '\0'; i++) {
if (*(str + i) == ' ' )
continue;
sp[j++]=*(str + i);
}
sp[j] = 0;
strcpy(str, sp);
}
void reverse(char s[])
{
for(int i=0,j=strlen(s)-1;i<j;++i,--j)
{
int c = s[i];
s[i] = s[j];
s[j] = c;
}
}
void main()
{
char temp[100] = " hello zhangsan word ";
cout<<temp<<endl;
strallcut(temp);
cout<<temp<<endl;
reverse(temp);
cout<<temp<<endl;
}
统计文档中单词出现的次数
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <map>
using namespace std;
void main()
{
ifstream read;
typedef map<string,int> instrmap;
instrmap coll;
map<string,int>::iterator it1;
ofstream display("word.txt");
read.open("words.txt");
coll.clear();
while(!read.eof())
{
string word;
read>>word;
coll[word]++;
}
for(it1 = coll.begin();it1 != coll.end();it1++)
cout<<it1->first<<" "<<it1->second<<endl;
if (!display)
{
cout<<"不能打开文件 word.txt"<<endl;
}
for(it1 = coll.begin();it1 != coll.end();it1++)
{
if (it1->first!="the"&&it1->first!="an"&&it1->first!="a"&&it1->first!="but"&&it1->first!="and"&&it1->first!=" ")
{
display<<"单词"<<it1->first<<" "<<"出现了"<<it1->second<<"次"<<endl;
}
}
read.close();
display.close();
}