1、VC+学习总结一、 MessageBox、:MessageBox和AfxMessageBox的区别和联系。来源:或者:二、C、c+中字符串函数的不同在C语言下,字符串的定义只能是char*或者char100数组的类型。处理这些字符串的简便方法是用C语言库自带的字符串处理函数,这些函数有:strlen求字符串长度strcmp比较2个字符串是否一样strcat字符串连接操作strcpy字符串拷贝操作strncat字符串连接操作(前n个字符)strncpy字符串拷贝操作(前n个字符)strchr查询子串strstr查询字串strcpy函数名:strcpy功 能: 拷贝一个字符串到另一个用 法: ch
2、ar *strcpy(char *destin, char *source);程序例:12345678910111213141516171819#include#includeintmain(void)charstring10;char*str1=abcdefghi;strcpy(string,str1);printf(%sn,string);return0;strncpy函数名:strncpy原型:char *strncpy(char *dest, char *src,size_tn);功能:将字符串src中最多n个字符复制到字符数组dest中(它并不像strcpy一样遇到NULL才停止复制
3、,而是等凑够n个字符才开始复制),返回指向dest的指针。strcat函数名:strcat功 能: 字符串拼接函数用 法: char *strcat(char *destin, char *source);程序例:1234567891011#include#includevoidmain()chardestination25;char*blank=,*c=C+,*Borland=Borland;strcpy(destination,Borland);strcat(destination,blank);strcat(destination,c);printf(%sn,destination);s
4、trchr函数名:strchr功 能: 在一个串中查找给定字符的第一个匹配之处用 法: char *strchr(char *str, char c);程序例:1234567891011121314#include#includeintmain(void)charstring15;char*ptr,c=r;strcpy(string,Thisisastring);ptr=strchr(string,c);if(ptr)printf(Thecharacter%cisatposition:%dn,c,ptr-string);elseprintf(Thecharacterwasnotfoundn);
5、return0;strcmp函数名:strcmp功 能: 串比较用 法: intstrcmp(char *str1, char *str2);看Asic码,str1str2,返回值 0;两串相等,返回0程序例:123456789101112131415161718#include#includeintmain(void)char*buf1=aaa,*buf2=bbb,*buf3=ccc;intptr;ptr=strcmp(buf2,buf1);if(ptr0)printf(buffer2isgreaterthanbuffer1n);elseprintf(buffer2islessthanbuf
6、fer1n);ptr=strcmp(buf2,buf3);if(ptr0)printf(buffer2isgreaterthanbuffer3n);elseprintf(buffer2islessthanbuffer3n);return0;strnicmp函数名:strnicmp功 能: 将一个串中的一部分与另一个串比较, 不管大小写用 法: intstrnicmp(char *str1, char *str2, unsigned maxlen);程序例:123456789101112131415#include#includeintmain(void)char*buf1=BBB,*buf2=
7、bbb;intptr;ptr=strnicmp(buf2,buf1);if(ptr0)printf(buffer2isgreaterthanbuffer1n);if(ptr0)printf(buffer2islessthanbuffer1n);if(ptr=0)printf(buffer2equalsbuffer1n);return0;strlen函数名:strlen功能: strlen函数求的是字符串的长度,它求得方法是从字符串的首地址开始到遇到第一个0停止计数,如果你只定义没有给它赋初值,这个结果是不定的,它会从字符串首地址一直记下去,直到遇到0才会停止。原型:size_tstrlen(c
8、onst char *s);12345678910#include#includeintmain()inti=0;char*he=Hello,world;i=strlen(he);printf(字符串长度为%dn,i);return0;运行结果:字符串长度为11strcspn函数名:strcspn功 能: 在串中查找第一个给定字符集内容的段用 法: intstrcspn(char *str1, char *str2);程序例:1234567891011#include#includeintmain(void)char*string1=1234567890;char*string2=747DC8
9、;intlength;length=strcspn(string1,string2);printf(Characterwherestringsintersectisatposition%dn,length);return0;strdup函数名:strdup功 能: 将串拷贝到新建的位置处用 法: char *strdup(char *str);程序例:123456789101112131415161718192021#include#include#includeintmain(void)char*dup_str,*string=abcde;dup_str=strdup(string);pri
10、ntf(%sn,dup_str);free(dup_str);return0;stricmp函数名:stricmp功 能: 以大小写不敏感方式比较两个串用 法: intstricmp(char *str1, char *str2);程序例:123456789101112131415#include#includeintmain(void)char*buf1=BBB,*buf2=bbb;intptr;ptr=stricmp(buf2,buf1);if(ptr0)printf(buffer2isgreaterthanbuffer1n);if(ptr0)printf(buffer2islesstha
11、nbuffer1n);if(ptr=0)printf(buffer2equalsbuffer1n);return0;strerror函数名:strerror功 能: 返回指向错误信息字符串的指针用 法: char *strerror(int errnum);程序例:123456789#include#includeintmain(void)char*buffer;buffer=strerror(errno);printf(Error:%sn,buffer);return0;strcmpi函数名:strcmpi功 能: 将一个串与另一个比较, 不管大小写用 法: intstrcmpi(char
12、*str1, char *str2);程序例:123456789101112131415#include#includeintmain(void)char*buf1=BBB,*buf2=bbb;intptr;ptr=strcmpi(buf2,buf1);if(ptr0)printf(buffer2isgreaterthanbuffer1n);if(ptr0)printf(buffer2islessthanbuffer1n);if(ptr=0)printf(buffer2equalsbuffer1n);return0;strncmp函数名:strncmp功 能: 串比较用 法: intstrnc
13、mp(char *str1, char *str2, int maxlen);程序例:12345678910111213141516171819202122#include#includeintmain(void)char*buf1=aaabbb,*buf2=bbbccc,*buf3=ccc;intptr;ptr=strncmp(buf2,buf1,3);if(ptr0)printf(buffer2isgreaterthanbuffer1n);elseprintf(buffer2islessthanbuffer1n);ptr=strncmp(buf2,buf3,3);if(ptr0)print
14、f(buffer2isgreaterthanbuffer3n);elseprintf(buffer2islessthanbuffer3n);return0;strncpy函数名:strncpy功 能: 串拷贝用 法: char *strncpy(char *destin, char *source, int maxlen);程序例:1234567891011#include#includeintmain(void)charstring10;char*str1=abcdefghi;strncpy(string,str1,3);string3=0;printf(%sn,string);return
15、0;strnicmp函数名:strnicmp功 能: 不注重大小写地比较两个串用 法: int strnicmp(char *str1, char *str2, unsigned maxlen);程序例:123456789101112131415#include#includeintmain(void)char*buf1=BBBccc,*buf2=bbbccc;intptr;ptr=strnicmp(buf2,buf1,3);if(ptr0)printf(buffer2isgreaterthanbuffer1n);if(ptr0)printf(buffer2islessthanbuffer1n
16、);if(ptr=0)printf(buffer2equalsbuffer1n);return0;strnset函数名:strnset功 能: 将一个字符串前n个字符都设为指定字符用 法: char *strnset(char *str, char ch, unsigned n);程序例:1234567891011#include#includeintmain(void)char*string=abcdefghijklmnopqrstuvwxyz;charletter=x;printf(stringbeforestrnset:%sn,string);strnset(string,letter,
17、13);printf(stringafterstrnset:%sn,string);return0;strpbrk函数名:strpbrk功 能: 在串中查找给定字符集中的字符用 法: char *strpbrk(char *str1, char *str2);程序例:1234567891011121314#include#includeintmain(void)char*string1=abcdefghijklmnopqrstuvwxyz;char*string2=onm;char*ptr;ptr=strpbrk(string1,string2);if(ptr)printf(strpbrkfo
18、undfirstcharacter:%cn,*ptr);elseprintf(strpbrkdidntfindcharacterinsetn);return0;strrchr函数名:strrchr功 能: 在串中查找指定字符的最后一个出现用 法: char *strrchr(char *str, char c);程序例:1234567891011121314#include#includeintmain(void)charstring15;char*ptr,c=r;strcpy(string,Thisisastring);ptr=strrchr(string,c);if(ptr)printf(
19、Thecharacter%cisatposition:%dn,c,ptr-string);elseprintf(Thecharacterwasnotfoundn);return0;strrev函数名:strrev功 能: 串倒转用 法: char *strrev(char *str);程序例:12345678910#include#includeintmain(void)char*forward=string;printf(Beforestrrev():%sn,forward);strrev(forward);printf(Afterstrrev():%sn,forward);return0;
20、strspn函数名:strspn功 能: 在串中查找指定字符集的子集的第一次出现用 法: intstrspn(char *str1, char *str2);程序例:123456789101112#include#include#includeintmain(void)char*string1=1234567890;char*string2=123DC8;intlength;length=strspn(string1,string2);printf(Characterwherestringsdifferisatposition%dn,length);return0;strstr函数名:strs
21、tr功 能: 在串中查找指定字符串的第一次出现用 法: char *strstr(char *str1, char *str2);程序例:123456789#include#includeintmain(void)char*str1=BorlandInternational,*str2=nation,*ptr;ptr=strstr(str1,str2);printf(Thesubstringis:%sn,ptr);return0;strtod函数名: strtod功 能: 将字符串转换为double型值用 法: double strtod(char *str, char *endptr);程序
22、例:123456789101112#include#includeintmain(void)charinput80,*endptr;doublevalue;printf(Enterafloatingpointnumber:);gets(input);value=strtod(input,&endptr);printf(Thestringis%sthenumberis%lfn,input,value);return0;strtok函数名:strtok功 能: 查找由在第二个串中指定的分界符分隔开的单词用 法: char *strtok(char *str1, char *str2);程序例:12
23、3456789101112131415161718192021#include#includeintmain(void)charinput16=abc,d;char*p;/*strtokplacesaNULLterminatorinfrontofthetoken,iffound*/p=strtok(input,);if(p)printf(%sn,p);/*AsecondcalltostrtokusingaNULLasthefirstparameterreturnsapointertothecharacterfollowingthetoken*/p=strtok(NULL,);if(p)prin
24、tf(%sn,p);return0;strtol函数名:strtol功 能: 将串转换为长整数用 法: longstrtol(char *str, char *endptr, int base);程序例:12345678910111213#include#includeintmain(void)char*string=87654321,*endptr;longlnumber;/*strtolconvertsstringtolonginteger*/lnumber=strtol(string,&endptr,10);printf(string=%slong=%ldn,string,lnumber);return0;strupr函数名:strupr功 能: 将串中的小写字母转换为大写字母用 法: char *strupr(char *str);程序例:123456789101112#include#includeintmain(void)charstring=abcdefghijklmnopqrstu
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1