字符串比较函数不区分大小写比如 “DMX” and “dmx”
/*
字符串比较函数可以用int strcmp(const char *s1,const char *s2);但是其原型比较的ascii码的值的大小,但是对于大小写是不能相比较的。所以就有下边的字符串函数的比较函数,不区分大小写。
extern int strcmp(const char *s1,const char *s2);
规则
当s1<s2时,返回为负数;
当s1=s2时,返回值= 0;
当s1>s2时,返回正数。
*/
/*
*其参数
*const char *str1[in] 输入字符指针,也可为数组名
*const char *str2[in] 相比较的字符指针,也可为数组名
*int count [in] 比较的长度
*返回值
* 0表示相等,-1表示不相等
* 其实就是用到了一个toupper这样的函数,要用此函数必须引用#include <ctype.h>这个库
*/
#include <ctype.h>
// 字符串比较, 不区分大小写, 例如: str == STR
int strncmp_ul(const char *str1, const char *str2, int count)
{
int i = 0;
//比较两个字符串的长度
if ((strlen(str1) < count) || (strlen(str2) < count))
{
return -1;
}
//遍历每一个字符串里的字符利用toupper这个函数比较大小
while (i < count)
{
if (toupper(*str1) != toupper(*str2))
return -1;
i++;
++str1;
++str2;
}
return 0;
}
/*一下代码是总结实用的小程序*/
//注意用一下函数必须要引用一下库函数
#include <ctype.h>
#include "..\\inc\\ctype_plus.h"
// 检查参数c是否为空格字符, 也就是判断是否为空格(' ')
//int isspace(int c)
//{
// if(c == ' ') return 1;
// else return 0;
//}
// isblank() 用来判断一个字符是否为TAB或者空格, 其原型为:
// int isblank(int c);
// 【参数】c 为需要检测的字符.
// 【返回值】若 c 为TAB或空格, 返回非 0 值, 否则返回 0.
// 说明:这里的TAB是指控制字符 '\t', 空格指我们能看得见的用来分割单词的 ' '.
// 注意:isblank() 属于 C99 标准, 并没有在 ASNI C(C89)中定义.
int isblank(int c)
{
if((c == ' ') || (c == '\t')) return 1;
else return 0;
}
// isbreak() 用来判断一个字符是否为'\r'或者'\n', 其原型为:
// int isbreak(int c);
// 【参数】c 为需要检测的字符.
// 【返回值】若 c 为'\r'或者'\n', 返回非 0 值, 否则返回 0.
int isbreak(int c)
{
if((c == '\r') || (c == '\n')) return 1;
else return 0;
}
// isblank_c() 用来判断一个字符是否为TAB或者空格, 其原型为:
// int isblank_c(int c);
// 【参数】c 为需要检测的字符.
// 【返回值】若 c 为TAB或空格, 返回非 0 值, 否则返回 0.
// 说明:这里的TAB是指控制字符 '\t', 空格指我们能看得见的用来分割单词的 ' '.
// 备注: 本函数除了TAB和空格外, 多了','的判断
int isblank_c(int c)
{
if((c == ' ') || (c == '\t') || (c == ',')) return 1;
else return 0;
}
// isblank_e() 用来判断一个字符是否为TAB或者空格, 其原型为:
// int isblank_e(int c);
// 【参数】c 为需要检测的字符.
// 【返回值】若 c 为TAB或空格, 返回非 0 值, 否则返回 0.
// 说明:这里的TAB是指控制字符 '\t', 空格指我们能看得见的用来分割单词的 ' '.
// 备注: 本函数除了TAB和空格外, 多了'='的判断
int isblank_e(int c)
{
if((c == ' ') || (c == '\t') || (c == '=')) return 1;
else return 0;
}
// isequal() 用来判断一个字符是否为'=', 其原型为:
// int isequal(int c);
// 【参数】c 为需要检测的字符.
// 【返回值】若 c 为'=', 返回非 0 值, 否则返回 0.
int isequal(int c)
{
if(c == '=') return 1;
else return 0;
}
// iscomma() 用来判断一个字符是否为',', 其原型为:
// int iscomma(int c);
// 【参数】c 为需要检测的字符.
// 【返回值】若 c 为',', 返回非 0 值, 否则返回 0.
int iscomma(int c)
{
if(c == ',') return 1;
else return 0;
}
// isdigits() 用来判断一个字符串是否为int, 其原型为:
// int isdigits(const char *str);
// 【参数】str 为需要检测的字符串.
// 【返回值】若 str 为int, 返回非 0 值, 否则返回 0.
int isdigits(const char *str)
{
while (isdigit(*str))
++str;
if (*str == '\0')
return 1;
return 0;
}
// isxdigits() 用来判断一个字符串是否为16进制数, 其原型为:
// int isxdigits(const char *str);
// 【参数】str 为需要检测的字符串.
// 【返回值】若 str 为int, 返回非 0 值, 否则返回 0.
int isxdigits(const char *str)
{
while (isxdigit(*str))
++str;
if (*str == '\0')
return 1;
return 0;
}
// isalnums() 用来判断一个字符串是否为字母或数字, 其原型为:
// int isalnums(const char *str);
// 【参数】str 为需要检测的字符串.
// 【返回值】若 str 为int, 返回非 0 值, 否则返回 0.
int isalnums(const char *str)
{
while (isalnum(*str))
++str;
if (*str == '\0')
return 1;
return 0;
}
#include <string.h>
#include <ctype.h>
#include "..\\inc\\ctype_plus.h"
#include "..\\inc\\trim.h"
// 去除字符串左侧的空格
char *trim_l(char* dStr, const char* pStr)
{
if (pStr == NULL)
return NULL;
while (*pStr == ' ')
pStr++;
while (*pStr != '\0')
*dStr++ = *pStr++;
return dStr;
}
// 去除字符串左侧的空格或TAB'\t'
char *trimt_l(char* dStr, const char* pStr)
{
if (pStr == NULL)
return NULL;
while (*pStr == ' ' || *pStr == '\t')
pStr++;
while (*pStr != '\0')
*dStr++ = *pStr++;
return dStr;
}
// 去除字符串左右的空格
char *trim_a( char *s )
{
int i = 0;
int j = strlen ( s ) - 1;
int k = 0;
while ( isspace ( s[i] ) && s[i] != '\0' )
i++;
while ( isspace ( s[j] ) && j >= 0 )
j--;
while ( i <= j )
s[k++] = s[i++];
s[k] = '\0';
return s;
}
// 去除字符串左右的空格或TAB'\t'
char *trimt_a( char *s )
{
int i = 0;
int j = strlen ( s ) - 1;
int k = 0;
while ( isblank ( s[i] ) && s[i] != '\0' )
i++;
while ( isblank ( s[j] ) && j >= 0 )
j--;
while ( i <= j )
s[k++] = s[i++];
s[k] = '\0';
return s;
}
// 去除字符串左右的'\r'或'\n'
char *trimb_a( char *s )
{
int i = 0;
int j = strlen ( s ) - 1;
int k = 0;
while ( isbreak ( s[i] ) && s[i] != '\0' )
i++;
while ( isbreak ( s[j] ) && j >= 0 )
j--;
while ( i <= j )
s[k++] = s[i++];
s[k] = '\0';
return s;
}