[c/c++] programming之路(25)、字符串(六)——memset,Unicode及宽字符,strset

时间:2023-03-09 08:32:53
[c/c++] programming之路(25)、字符串(六)——memset,Unicode及宽字符,strset

一、memset

#include<stdio.h>
#include<stdlib.h>
#include<memory.h> void *mymemset(void *p, int num, int len) {
char *px = (char *)p;
if (p == NULL)
return NULL;
while (len>)
{
*px = (char)num;
px++;
len--;
}
return p;//因为上述代码是对px进行操作,p的地址没有变化,所以最后返回p
} void main() {
char str[]= {"china is great"};
int num[] = { ,,,, };
float f[] = { 1.0,2.0,3.0,4.0,5.0 };
//memset(str, '\0', 40);
mymemset(str, '\0', );
printf("%s\n",str);
mymemset(num,,);
mymemset(f,,);
for (int i = ; i < ; i++)
printf("%d,%f\n",num[i],f[i]); system("pause");
}

二、Unicode及宽字符

#include<stdio.h>
#include<stdlib.h>
#include<locale.h>//设置本地化 void main0() {
//字符串可以显示汉字,字符不可以,字符连在一起%c%c可以输出一个汉字
char str[] = "你好中国";
char ch = '我';//char只能是字母,数字,字符
printf("%s\n", str);
printf("%c%c\n",ch);//ch无法正常输出‘我’,ASCII表里面没有‘我’
printf("%c%c\n",str[],str[]);//输出一个汉字,一个汉字占两个字节
system("pause");
} void main() {
wchar_t ch = L'我';//L宽字符或者宽字符串
printf("%d\n",sizeof(ch));//2个字节
setlocale(LC_ALL,"chs");//简体中文
wprintf(L"%wc\n", ch);//汉字当做一个字符 wchar_t str[] = L"我是一个好人ABC";
wprintf(L"%s\n", str);
system("pause");
}

[c/c++] programming之路(25)、字符串(六)——memset,Unicode及宽字符,strset

[c/c++] programming之路(25)、字符串(六)——memset,Unicode及宽字符,strset

#include<stdio.h>
#include<stdlib.h>
#include<Windows.h> void main() {
//MessageBox(0, "滴,不准用快播飙车!", "360安全卫士", 0);//使用多字节字符集时正常运行
//MessageBox(0,L"滴,不准用快播飙车!",L"360安全卫士", 0);//使用Unicode字符集时,需要加‘L’
MessageBoxA(, "滴,不准用快播飙车!", "MessageBoxA", );//无论是多字节还是Unicode,始终以多字节运行
MessageBoxW(, L"滴,不准用快播飙车!", L"MessageBoxW", );//无论是多字节还是Unicode,始终以Unicode运行
MessageBox(,TEXT("滴,不准用快播飙车!"),TEXT("TEXT"), );//自动适应多字节或者Unicode }

[c/c++] programming之路(25)、字符串(六)——memset,Unicode及宽字符,strset[c/c++] programming之路(25)、字符串(六)——memset,Unicode及宽字符,strset[c/c++] programming之路(25)、字符串(六)——memset,Unicode及宽字符,strset

三、strset

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string> void mystrset(char *p, char ch) {
while (*p)
{
*p = ch;
p++;
}
} void main() {
char str[] = "yincheng8848";
printf("before:%s\n", str);
//_strset(str, '8');//_strset标准C语言
mystrset(str, '');//_strset标准C语言
//_strset(str, '\0');//清空字符串
printf("after:%s\n", str);
system("pause");
}

[c/c++] programming之路(25)、字符串(六)——memset,Unicode及宽字符,strset