處理字符串的一些小的實用的函數

字符串比較函數不區分大小寫比如 “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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章