c语言字符编码转换库函数 c语言编程字符转换( 二 )


2.strtok函数以后的调用时的需用NULL来替换s.
3.形参s(要分割的字符串)对应的变量应用char s[]=”….”形式,而不能用char *s=”….”形式 。
举例:
[cpp] view plain copy
voidmain()
{
char buf[]=”Golden Global View”;
char* token = strtok( buf, ” “);
while( token != NULL )
{
printf( ”%s “, token );
token = strtok( NULL, ” “);
}
return 0;
}
/*其结果为:
Golden
Global
View
*/
函数名:strncpy
功能:把src所指由NULL结束的字符串的前n个字节复制到dest所指的数组中
用法:char *strncpy(char *dest, char *src, int n);
说明:
如果src的前n个字节不含NULL字符,则结果不会以NULL字符结束 。
如果src的长度小于n个字节,则以NULL填充dest直到复制完n个字节 。
src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串 。
返回指向dest的指针 。
举例:
[c-sharp] view plain copy
#include syslib.h
#include string.h
main()
{
char buf[4];
char *s="abcdefg";
strncpy(buf,s,4);
printf("%s/n",buf);
return 0;
}
/*运行结果:
abcd
*/
函数名: stpcpy
功能: 拷贝一个字符串到另一个
用法: char *stpcpy(char *destin, char *source);
举例:
[cpp] view plain copy
#include stdio.h
#include string.h
int main(void)
{
char string[10];
char *str1 = "abcdefghi";
stpcpy(string, str1);
printf("%s/n", string);
return 0;
}
/*运行结果
abcdefghi
*/
函数名: strcat
功能: 字符串拼接函数
用法: char *strcat(char *destin, char *source);
举例:
[cpp] view plain copy
#include string.h
#include stdio.h
int main(void)
{
char destination[25];
char *blank = " ", *c = "C++", *Borland = "Borland";
strcpy(destination, Borland);
strcat(destination, blank);
strcat(destination, c);
printf("%s/n", destination);
return 0;
}
/*运行结果:
Borland C++
*/
函数名: strcmp
功能: 串比较
用法: int strcmp(char *str1, char *str2);
看Asic码 , str1str2,返回值0;两串相等,返回0
举例:
[cpp] view plain copy
#include string.h
#include stdio.h
int main(void)
{
char *buf1 = "aaa", *buf2 = "bbb";
int ptr;
ptr = strcmp(buf2, buf1);
if (ptr0)
printf("buffer 2 is greater than buffer 1/n");
else if(ptr0)
printf("buffer 2 is less than buffer 1/n");
else
printf("buffer 2 is equal with buffer 1/n");
return 0;
}
/*运行结果:
buffer 2 is greater than buffer 1
*/
函数名: strncmpi
功能: 将一个串中的一部分与另一个串比较, 不管大小写
用法: int strncmpi(char *str1, char *str2, unsigned maxlen);
举例:
[cpp] view plain copy
#include string.h
#include stdio.h
int main(void)
{
char *buf1 = "BBB", *buf2 = "bbb";
int ptr;
ptr = strcmpi(buf2, buf1);
if (ptr0)
printf("buffer 2 is greater than buffer 1/n");
if (ptr0)
printf("buffer 2 is less than buffer 1/n");
if (ptr == 0)
printf("buffer 2 equals buffer 1/n");
return 0;
}
函数名: strcspn
功能: 在串中查找第一个给定字符集内容的段
用法: int strcspn(char *str1, char *str2);
举例:
[cpp] view plain copy
#include stdio.h
#include string.h
#include alloc.h
int main(void)
{
char *string1 = "1234567890";
char *string2 = "747DC8";

推荐阅读