- #include<stdio.h>
- #include<malloc.h>
- /************************************************************************/
- /* 函数声明 */
- /************************************************************************/
- /*去掉首尾的不可显示字符,即去掉ASCII码值为0-31的字符*/
- void lxh_strTrim(char *str);
- /*求字符串长度*/
- unsigned lxh_strLen(char *str);
- /*字符串比较。大于返回整数,小于返回负数,等于,返回0*/
- int lxh_strComp(char *str1, char *str2);
- /*查找指定字符。返回首次出现的位置下标*/
- /*参数说明:str原串,ch待查找字符,pos查找起始位置*/
- int lxh_strIndexOfChar(char *str, char ch, unsigned pos);
- /*查找指定字符串。返回首次出现的位置下标*/
- /*参数说明:S原串,T待查找模式串,pos查找起始位置,isKMP是否使用KMP算法*/
- int lxh_strIndexOfStr(char *S, char *T, unsigned pos, unsigned isKMP);
- /*字符串拷贝。将src串从begin到end拷贝到dest串*/
- /*参数说明:src原串,begin原串起始下标,end原串结束下标,dest目的串*/
- void lxh_strCopy(char *src, unsigned begin, unsigned end, char *dest);
- /*是否以指定字符串开始*/
- /*参数说明:S原串,T模式串*/
- unsigned lxh_beginWith(char *S, char *T);
- /*是否以指定字符串结尾*/
- /*参数说明:S原串,T模式串*/
- unsigned lxh_endWith(char *S, char *T);
- /*查找指定字符串--具体实现,不用KMP*/
- int _lxh_strIndexOfStr(char *S, char *T, unsigned pos);
- /*查找指定字符串--具体实现,使用KMP*/
- int _lxh_strIndexOfStr_KMP(char *S, char *T, unsigned pos);
- /*查找指定字符串--具体实现,KMP的辅助*/
- void _lxh_strGetNextval(char *T, unsigned lenT, int *next);
- /************************************************************************/
- /* 函数实现 */
- /************************************************************************/
- /*去掉首尾的不可显示字符,即去掉ASCII码值为0-31的字符*/
- void lxh_strTrim(char *str){
- unsigned i, j, endIndex;
- if(NULL==str || '\\0'==str[0]) return;
- for(i=0; str[i]!=0&&str[i]<=32; i++);
- endIndex = 0;
- for(j=0; '\\0'!=str[i];){
- if(str[i]>32) endIndex = j;
- str[j++] = str[i++];
- }
- if(str[endIndex]>32) str[endIndex+1] = '\\0';
- else str[endIndex] = '\\0';
- }
- /*求字符串长度*/
- unsigned lxh_strLen(char *str){
- unsigned i;
- if(NULL==str || '\\0'==str[0]) return 0;
- for(i=0; str[i]!='\\0'; i++);
- return i;
- }
- /*字符串比较。大于返回整数,小于返回负数,等于,返回0*/
- int lxh_strComp(char *str1, char *str2){
- unsigned i;
- int result;
- if(NULL==str1 && NULL==str2) return 0;
- else if(NULL==str1 && NULL!=str2) return -1;
- else if(NULL!=str1 && NULL==str2) return 1;
- for(i=0; str1[i]!=0 || str2[i]!=0; i++){
- result = str1[i] - str2[i];
- if(result != 0) break;
- }
- return result;
- }
- /*查找指定字符。返回首次出现的位置下标*/
- int lxh_strIndexOfChar(char *str, char ch, unsigned pos){
- unsigned i;
- if(NULL==str || pos<0 || pos>=lxh_strLen(str)) return -1;
- for(i=pos; str[i]!='\\0'; i++){
- if(str[i] == ch) return i;
- }
- return -1;
- }
- /*查找指定字符串。返回首次出现的位置下标*/
- int lxh_strIndexOfStr(char *S, char *T, unsigned pos, unsigned isKMP){
- if(NULL==S || NULL==T || pos<0 || pos>=lxh_strLen(S)) return -1;
- if(isKMP == 0) return _lxh_strIndexOfStr(S, T, pos);
- else return _lxh_strIndexOfStr_KMP(S, T, pos);
- }
- /*查找指定字符串--具体实现,不用KMP*/
- int _lxh_strIndexOfStr(char *S, char *T, unsigned pos){
- unsigned i, j, lenS, lenT;
- i = pos; j=0;
- lenS = lxh_strLen(S); lenT = lxh_strLen(T);
- while(i < lenS && j < lenT){
- if(S[i] == T[j]){i++; j++;}
- else {i = i-j+1; j = 0;}
- }
- if(j==lenT) return i-lenT;
- else return -1;
- }
- /*查找指定字符串--具体实现,使用KMP*/
- int _lxh_strIndexOfStr_KMP(char *S, char *T, unsigned pos){
- unsigned i, j, lenS, lenT;
- int *next;
- i = pos; j = 0;
- lenS = lxh_strLen(S); lenT = lxh_strLen(T);
- next = (int*)malloc(sizeof(int)*lenT);
- _lxh_strGetNextval(T, lenT, next);
- while(i < lenS && j < lenT){
- if(S[i] == T[j]){i++; j++;}
- else if(j == 0) i++;
- else j = next[j];
- }
- free(next);
- next = NULL;
- if(j==lenT) return i-lenT;
- else return -1;
- }
- /*查找指定字符串--具体实现,KMP的辅助*/
- void _lxh_strGetNextval(char *T, unsigned lenT, int *next){
- unsigned i, j;
- i = 1; j = 0;
- next[0] = -1; next[1] = 0;
- while(i < lenT-1){
- if(T[i] == T[j]) {
- i++; j++;
- if (T[i]!=T[j]) next[i] = j;
- else next[i] = next[j];
- }
- else if(j ==0){i++; next[i] = 0;}
- else j = next[j];
- }
- }
- /*字符串拷贝。将src串从begin到end拷贝到dest串*/
- void lxh_strCopy(char *src, unsigned begin, unsigned end, char *dest){
- unsigned i, j;
- if(NULL==src || begin<0 || end<begin || end>lxh_strLen(src) || NULL==dest) return;
- for(i=begin,j=0; i<end; i++,j++) dest[j] = src[i];
- dest[j] = '\\0';
- }
- /*是否以指定字符串开始*/
- unsigned lxh_beginWith(char *S, char *T){
- unsigned i;
- if(NULL==S || NULL==T) return 0;
- for(i=0; S[i]!=0 && T[i]!=0; i++);
- if(0 == T[i]) return 1;
- else return 0;
- }
- /*是否以指定字符串结尾*/
- unsigned lxh_endWith(char *S, char *T){
- unsigned lenS, lenT;
- if(NULL==S || NULL==T) return 0;
- lenS = lxh_strLen(S); lenT = lxh_strLen(T);
- if(0 == lxh_strComp(S+lenS-lenT, T)) return 1;
- else return 0;
- }
- //该片段来自于http://www.codesnippet.cn/detail/0311201410866.html
来源: http://www.codesnippet.cn/detail/0311201410866.html