#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void fc(char* d,int n)
{
int len=strlen(d);
char c[100]="";
char* e=NULL;
int i=0;
if(n>=len)
printf("%s",d);
else
{
while(i<n)
{
//汉字由8bit组成且最高为1,通过右移7位&1判断是否为汉字,汉字截取2字节
if ( d[i]>>7&1 && d[i+1]>>7&1 )
i=i+2;
//字母截取1字节
else
i++;
}
strncpy(c,d,i);
e=c;
printf("从%s字符串中截取%d位字符串结果是:\n%s\n\n",d,n,e);
}
}
int main(int argc, char* argv[])
{
fc("china中国verygood天朝nice",19);
system("pause");
return 0;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void fc(char* d,int n)
{
int len=strlen(d);
char c[100]="";
char* e=NULL;
int i=0;
if(n>=len)
printf("%s",d);
else
{
while(i<n)
{
//汉字由8bit组成且最高为1,通过右移7位&1判断是否为汉字,汉字截取2字节
if ( d[i]>>7&1 && d[i+1]>>7&1 )
i=i+2;
//字母截取1字节
else
i++;
}
strncpy(c,d,i);
e=c;
printf("从%s字符串中截取%d位字符串结果是:\n%s\n\n",d,n,e);
}
}
int main(int argc, char* argv[])
{
fc("china中国verygood天朝nice",19);
system("pause");
return 0;
}