
今天遇到平时比较少用的这三个字符串函数c语言strchr函数,查资料并测试了一下,备忘。。。
头文件:#include <string.h>
1、定义函数:char * strtok(char *s, const char *delim);
函数说明:strtok()用来将字符串分割成一个个片段. 参数s 指向欲分割的字符串, 参数delim 则为分割字符串,当strtok()在参数s 的字符串中发现到参数delim 的分割字符时则会将该字符改为\0 字符. 在第一次调用时,strtok()必需给予参数s 字符串, 往后的调用则将参数s 设置成NULL. 每次调用成功则返回下一个分割后的字符串指针.
返回值:返回下一个分割后的字符串指针, 如果已无从分割则返回NULL.
定义函数 int sscanf (const char *str,const char * format,........)。定义函数 file * fopen(const char * path,const char * mode)。函数原型:extern char *strstr(const char *str1, const char *str2)。
函数说明:strstr()会从字符串haystack 中搜寻字符串needle, 并将第一次出现的地址返回.
返回值:返回指定字符串第一次出现的地址, 否则返回0.
3、 定义函数:char * strchr (const char *s, int c);
字符串结构是:[命令][设备别名][命令参数].第二个参数:返回信息的缓冲区,为一指定了大小的字符串变量.第三个参数:缓冲区的大小,就是字符变量的长度.第四个参数:回调方式,一般设为零返回值:函数执行成功返回零c语言strchr函数,否则返回错误代码首先在代码中引入这个api函数codecode highlighting produced by actipro codehighlighter (freeware)>[dllimport("winmm.dll",entrypoint="mcisendstring",charset=charset.auto)]publicstaticexternintmcisendstring(stringlpstrcommand,stringlpstrreturnstring,intureturnlength,inthwndcallback)。函数说明:execl()用来执行参数path字符串所代表的文件路径, 接下来的参数代表执行该文件时传递的argv[0],argv[1].....是后一个参数必须用空指针null作结束返回值:成功则不返回值, 失败返回-1, 失败原因存于errno中错误代码:参execve()范例:/*执行 /bin/...。查找任何一个不包含在strcharset串中的字符 (字符串结束符null除外) 在string串中首次出现的位置序号. 返回一个整数值, 指定在string中全部由characters中的字符组成的子串的长度. 如果string以一个不包含在strcharset中的字符开头, 函数将返回0值.。
返回值:如果找到指定的字符则返回该字符所在地址, 否则返回0.

其实2、3这两个函数差不多,只不过一个是子串,一个是字符。
[cpp]view plain
//一些不常用的字符串函数备忘
//str_test.c
#include<stdio.h>
#include<string.h>
voidstrtok_test()//字符串分割
{
chars[]="ab-cd:123fez-o:hello,world!";
char*delim="-:,f";
char*p;

inti=1;
fprintf(fid,'hello world。fprintf(2,'hello world。fprintf(1,'hello world。
while((p=strtok(NULL,delim)))
printf("%d%s",++i,p);
printf("\n");
}
voidstrstr_test()//字符串查找
{
char*s="123abc";
char*p;
p=strstr(s,"3a");

printf("strstr_testprint:%s\n",p);
}
voidstrchr_test()//找出字符在字符串第一次出现的位置
{
char*addr="192.168.1.1";
char*p;
p=addr;
p=strchr(p,'.');
printf("strchr_testprint:%s\n",p);
}
intmain()

{
strtok_test();
strstr_test();
strchr_test();
return0;
结果:
[huangbin@localhost test]$ gcc str_test.c
[huangbin@localhost test]$ ./a.out
s =ab-cd:123fez-o:hello,world! delim=-:,f
strtok_test print:1 ab 2 cd 3 123 4 ez 5 o 6 hello 7 world!
strstr_test print: 3abc
strchr_test print: .168.1.1
[huangbin@localhost test]$
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-109136-1.html
正确
我若动手