http解析中的一些东西,strpbrk,strcasecmp,strncasecmp,strspn,strcspn

来源:互联网 发布:ubuntu恢复出厂 命令 编辑:程序博客网 时间:2024/06/15 22:17
http解析中的一些东西:
1:
/t:空格
/r:回车
/r/n:一行结束的标志
2:
strpbrk():
函数原型:extern char *strpbrk(char *str1, char *str2)
参数说明:str1待比较的字符串,str2为指定被搜索的字符串。
        
所在库名:#include <string.h>
  
函数功能:比较字符串str1和str2中是否有相同的字符,如果有,则返回该字符在str1中的位置的指针。
  
返回说明:返回指针,搜索到的字符在str1中的索引位置的指针。
其它说明:暂时无。
实例:
#include<string.h>
#include<stdio.h>
int main()
{
    char *str1="please try again,sky2098!";
    char *str2="Hello,I am sky2098,I like writing!";
    char *strtemp;
    strtemp=strpbrk(str1,str2);  //搜索进行匹配
    printf("Result is:  %s ",strtemp);
    return 0;
}
返回了str2中字符“l”在str1中位置的指针,打印出字符串“lease try again,sky2098!”。
如果str2中所有字符都没有在str1中出现过,则返回null。
strcasetmp():
忽略大小写比较两个字符串是否相等,相等返回0。
strncasetmp():
忽略大小写比较两个字符串前n个字符是否相等,相等返回0。
strspn() :
函数用来计算字符串 str 中连续有几个字符都属于字符串 accept,其原型为:
size_t strspn(const char *str, const char * accept);


【函数说明】strspn() 从参数 str 字符串的开头计算连续的字符,而这些字符都完全是 accept 所指字符串中的字符。简单的说,若 strspn() 返回的数值为n,则代表字符串 str 开头连续有 n 个字符都是属于字符串 accept 内的字符。


【返回值】返回字符串 str 开头连续包含字符串 accept 内的字符数目。所以,如果 str 所包含的字符都属于 accept,那么返回 str 的长度;如果 str 的第一个字符不属于 accept,那么返回 0。


注意:检索的字符是区分大小写的。


提示:提示:函数 strcspn() 的含义与 strspn() 相反,可以对比学习。


范例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
    int i;
    char str[] = "129th";
    char accept[] = "1234567890";
    i = strspn(str, accept);
    printf("str 前 %d 个字符都属于 accept\n",i);
    system("pause");
    return 0;
}
执行结果:
str 前 3 个字符都属于 accept
strcspn():
用来计算字符串 str 中连续有几个字符都不属于字符串 accept,其原型为:
    int strcspn(char *str, char *accept);


【参数说明】str、accept为要进行查找的两个字符串。


strcspn() 从字符串 str 的开头计算连续的字符,而这些字符都完全不在字符串 accept 中。简单地说,若 strcspn() 返回的数值为 n,则代表字符串 str 开头连续有n 个字符都不含字符串 accept 中的字符。


【返回值】返回字符串 str 开头连续不含字符串 accept 内的字符数目。


注意:如果 str 中的字符都没有在 accept 中出现,那么将返回 atr 的长度;检索的字符是区分大小写的。


提示:函数 strspn() 的含义与 strcspn() 相反,可以对比学习。


【示例】返回s1、s2包含的相同字符串的位置。
#include<stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
    char* s1 = "http://c.biancheng.net/cpp/u/biaozhunku/";
    char* s2 = "c is good";
    int n = strcspn(s1,s2);
    printf("The first char both in s1 and s2 is :%c\n",s1[n]);  
    printf("The position in s1 is: %d\n",n);
    system("pause");
    return 0;
}
运行结果:
The first char both in s1 and s2 is :c
The position in s1 is: 7


再看一个例子,判断两个字符串的字符是否有重复的。
#include<stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
    char* s1 = "http://c.biancheng.net/cpp/xitong/";
    char* s2 = "z -+*";
    if(strlen(s1) == strcspn(s1,s2)){
        printf("s1 is diffrent from s2!\n");
    }else{
        printf("There is at least one same character in s1 and s2!\n");
    }
    system("pause");
    return 0;
}
运行结果:
s1 is diffrent from s2!
0 0