首頁 > 軟體

C語言實現手寫字串處理工具的範例程式碼

2022-09-16 22:04:06

標頭檔案

#ifndef STUDY_STR_UTIL_H
#define STUDY_STR_UTIL_H

#include "../structure/charhashmap.h"
#include "../structure/charlist.h"
#include "../structure/json.h"
#include <malloc.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <stdarg.h>

typedef int BOOL;//定義一個布林型別
#define TRUE 1
#define FALSE 0

int str_to_int(char *str);

char *int_to_str(int num);

long str_to_long(char *str);

long long str_to_long_long(char *str);

char *long_to_str(long num);

char *float_to_str(float num);

double str_to_double(char *str);

char *double_to_str(double num);

char *str_trim(char *str);

char *str_ltrim(char *str);

char *str_rtrim(char *str);

char *str_to_upper(char *str);

char *str_to_lower(char *str);

BOOL str_compare(char *str1, char *str2);

BOOL str_equals(char *str1, char *str2);

BOOL str_compare_ignore_case(char *str1, char *str2);

char *str_copy(char *str);

char *str_copy_n(char *str, int n);

char *str_concat(int count, ...);

char *str_concat_n(char *str1, char *str2, int n);

int str_find(char *str, char *search);

int str_find_n(char *str, char *search, int start, int end);

int str_find_n_times(char *str, char *search, int n);

int str_find_n_times_reverse(char *str, char *search, int n);

int str_find_ignore_case(char *str1, char *str2);

int str_find_n_ignore_case(char *str1, char *str2, int n);

int str_find_reverse(char *str1, char *str2);

int str_find_reverse_n(const char *str1, char *str2, int start, int end);

char *str_replace(char *str1, char *str2, char *str3);

char *str_replace_all(char *str1, char *str2, char *str3);

CharList *str_split(char *str1, char *str2);

int str_array_length(char **str);

int str_length(const char *str);

char *str_substring(char *str, int start, int end);

void str_array_print(char **str);

void str_print(char *str);

char *str_reverse(char *str);

int str_is_empty(const char *str);

int str_start_with(char *str1, char *str2);

int str_end_with(char *str1, char *str2);

unsigned int hashCode(char *str);

int str_contains(char *str1, char *str2);

int str_is_number(char *str);

int str_char_code(const char *str);

char str_char_at(char *str, int index);

int str_is_char(char *str);

int str_is_double(char *str);

int str_is_special_char(char *str);

int str_is_char_number(char *str);

int str_is_palindrome(char *str);

int str_is_what(char *str);

int str_similarity(char *str1, char *str2);

int str_is_boolean(char *str);

int str_is_numeric(char *str);

char *str_calculate_one(char *a, char *symbol);

char *str_calculate_two(char *a, char *b, char *symbol);

char *str_template(char *str, char *template);

CharList *get_range(char *start, char *end);

char *str_create(char *str);

char *str_append(char *str1, char *str2);

char *str_template(char *str, char *template);

char *str_template_map(char *str, CharHashMap *pMap);

char *formatStr(char *format, ...);

int str_contain_count(char *str1, char *str2);

#endif //STUDY_STR_UTIL_H

實現檔案

#include "str_util.h"


//字串轉換為整數
int str_to_int(char *str) {
    return atoi(str);
}


//整數轉換為字串
char *int_to_str(int num) {
    char *str = (char *) malloc(sizeof(char) * 10);
    sprintf(str, "%d", num);
    return str;
}

//字串轉換為長整數
long str_to_long(char *str) {
    return atol(str);
}

//字串轉換為長整數的長整數
long long str_to_long_long(char *str) {
    return atoll(str);
}

//長整數轉換為字串
char *long_to_str(long num) {
    char *str = (char *) malloc(sizeof(char) * 20);
    sprintf(str, "%ld", num);
    return str;
}

//浮點數轉換為字串
char *float_to_str(float num) {
    char *str = (char *) malloc(sizeof(char) * 20);
    // 把雙精度浮點數dd轉換為字串,存放在strdd中。
    sprintf(str, "%.2f", num);
    return str;
}

//字串轉換為浮點數
double str_to_double(char *str) {
    return atof(str);
}

//雙精度浮點數轉換為字串
char *double_to_str(double num) {
    char *str = (char *) malloc(sizeof(char) * 20);
    // 把雙精度浮點數dd轉換為字串,存放在strdd中。
    sprintf(str, "%.2lf", num);
    return str;
}

//獲取字串的長度  從1開始的
int str_length(const char *str) {
    int i = 0;
    while (str[i] != '') {
        i++;
    }
    return i;
}

//獲取字串陣列的長度 注意從1開始的   陣列結尾必須加一個NULL才能獲取陣列的長度 str[i+1]=NULL
int str_array_length(char **str_array) {
    int i = 0;
    while (str_array[i] != NULL) {
        i++;
    }
    return i;
}

//字串去空格 (徹底去掉所有的空格)
char *str_trim(char *str) {
    int i = 0;
    int j = 0;
    int len = str_length(str);
    char *result = (char *) malloc(sizeof(char) * len);
    while (str[i] != '') {
        if (str[i] != ' ') {
            result[j] = str[i];
            j++;
        }
        i++;
    }
    result[j] = '';
    return result;
}

//字串去除左邊空格
char *str_ltrim(char *str) {
    int i = 0;
    int j = 0;
    int len = str_length(str);
    char *result = (char *) malloc(sizeof(char) * len);
    while (str[i] != '') {
        if (str[i] != ' ') {
            break;
        }
        i++;
    }
    while (str[i] != '') {
        result[j] = str[i];
        i++;
        j++;
    }
    result[j] = '';
    return result;
}

//字串去除右邊空格
char *str_rtrim(char *str) {
    int i = 0;
    int j = 0;
    int len = str_length(str);
    char *result = (char *) malloc(sizeof(char) * len);
    while (str[i] != '') {
        if (str[i] != ' ') {
            result[j] = str[i];
            j++;
        }
        i++;
    }
    result[j] = '';
    return result;
}


//字串轉換為大寫
char *str_to_upper(char *str) {
    int i = 0;
    int len = str_length(str);
    char *result = (char *) malloc(sizeof(char) * len);
    while (str[i] != '') {
        if (str[i] >= 'a' && str[i] <= 'z') {
            result[i] = str[i] - 32;
        } else {
            result[i] = str[i];
        }
        i++;
    }
    result[i] = '';
    return result;
}

//字串轉換為小寫
char *str_to_lower(char *str) {
    int i = 0;
    int len = str_length(str);
    char *result = (char *) malloc(sizeof(char) * len);
    while (str[i] != '') {
        if (str[i] >= 'A' && str[i] <= 'Z') {
            result[i] = str[i] + 32;
        } else {
            result[i] = str[i];
        }
        i++;
    }
    result[i] = '';
    return result;
}

//字串比較 1相等 0不相等
BOOL str_compare(char *str1, char *str2) {
    int i = 0;
    while (str1[i] != '' && str2[i] != '') {
        if (str1[i] != str2[i]) {
            return FALSE;
        }
        i++;
    }
    if (str1[i] != str2[i]) {
        return FALSE;
    }
    return TRUE;
}
BOOL str_equals(char *str1, char *str2) {
    return str_compare(str1, str2);
}

//字串比較忽略大小寫 1相等 0不相等
BOOL str_compare_ignore_case(char *str1, char *str2) {
    int i = 0;
    while (str1[i] != '' && str2[i] != '') {
        if (str1[i] != str2[i]) {
            if (str1[i] >= 'A' && str1[i] <= 'Z') {
                if (str1[i] + 32 != str2[i]) {
                    return FALSE;
                }
            } else if (str1[i] >= 'a' && str1[i] <= 'z') {
                if (str1[i] - 32 != str2[i]) {
                    return FALSE;
                }
            } else {
                return FALSE;
            }
        }
        i++;
    }
    if (str1[i] != str2[i]) {
        return FALSE;
    }
    return TRUE;
}


//字串拷貝
char *str_copy(char *str) {
    int i = 0;
    int len = str_length(str);
    char *result = (char *) malloc(sizeof(char) * len);
    while (str[i] != '') {
        result[i] = str[i];
        i++;
    }
    result[i] = '';
    return result;
}

//字串拷貝指定長度
char *str_copy_n(char *str, int n) {
    int i = 0;
    char *result = (char *) malloc(sizeof(char) * n);
    while (str[i] != '' && i < n) {
        result[i] = str[i];
        i++;
    }
    result[i] = '';
    return result;
}

//字串拼接可變引數 引數1為拼接的字串個數 ,引數2開始為字串.....
char *str_concat(int count, ...) {
    va_list args;
    va_start(args, count);
    int i = 0;
    //保留引數
    char **str = (char **) malloc(sizeof(char *) * count);
    int len = 0; //總長度(位元組)
    for (i = 0; i < count; i++) {
        str[i] = va_arg(args, char *);
        len += str_length(str[i]);
    }
    char *result = (char *) malloc(sizeof(char) * len);
    int j = 0;
    for (i = 0; i < count; i++) {
        char *str1 = str[i];
        int k = 0;
        while (str1[k] != '') {
            result[j] = str1[k];
            j++;
            k++;
        }
    }
    result[j] = '';
    va_end(args);
    free(str);
    return result;
}

//字串拼接指定長度 從str2的第n個字元開始拼接
char *str_concat_n(char *str1, char *str2, int n) {
    int i = 0;
    int j = 0;
    int len1 = str_length(str1);
    int len2 = str_length(str2);
    char *result = (char *) malloc(sizeof(char) * (len1 + len2));
    while (str1[i] != '') {
        result[i] = str1[i];
        i++;
    }
    while (str2[j] != '' && j < n) {
        result[i] = str2[j];
        i++;
        j++;
    }
    result[i] = '';
    return result;
}

//字串查詢 從左到右查詢 返回第一次出現的位置 不存在返回-1 從0開始
int str_find(char *str, char *search) {
    int i = 0;
    int j = 0;
    int k = 0;
    int len = str_length(str);
    int search_len = str_length(search);
    while (i < len) {
        if (str[i] == search[j]) {
            k = i;
            while (str[k] == search[j] && j < search_len) {
                k++;
                j++;
            }
            if (j == search_len) {
                return i;
            }
            j = 0;
        }
        i++;
    }
    return -1;
}

//字串查詢指定長度 從指定位置開始到什麼位置結束 ,start從0開始 ,end從1開始
int str_find_n(char *str, char *search, int start, int end) {
    int i = start;
    int j = 0;
    int k = 0;
    int len = str_length(str);
    int search_len = str_length(search);
    while (i < len && i < end) {
        if (str[i] == search[j]) {
            k = i;
            while (str[k] == search[j] && j < search_len) {
                k++;
                j++;
            }
            if (j == search_len) {
                return i;
            }
            j = 0;
        }
        i++;
    }
    return -1;
}

//字串查詢 從左到右查詢 返回第n次出現的位置
int str_find_n_times(char *str, char *search, int n) {
    int i = 0;
    int j = 0;
    int k = 0;
    int len = str_length(str);
    int search_len = str_length(search);
    while (i < len) {
        if (str[i] == search[j]) {
            k = i;
            while (str[k] == search[j] && j < search_len) {
                k++;
                j++;
            }
            if (j == search_len) {
                n--;
                if (n == 0) {
                    return i;
                }
            }
            j = 0;
        }
        i++;
    }
    return -1;
}

//字串查詢,從右到左查詢,返回第n次出現的位置
int str_find_n_times_reverse(char *str, char *search, int n) {
    int i = str_length(str) - 1;
    int j = str_length(search) - 1;
    int k = 0;
    int len = str_length(str);
    int search_len = str_length(search);
    while (i >= 0) {
        if (str[i] == search[j]) {
            k = i;
            while (str[k] == search[j] && j >= 0) {
                k--;
                j--;
            }
            if (j == -1) {
                n--;
                if (n == 0) {
                    return i - search_len + 1;
                }
            }
            j = search_len - 1;
        }
        i--;
    }
    return -1;
}


//字串查詢忽略大小寫  返回位置
int str_find_ignore_case(char *str1, char *str2) {
    char *p = str1;
    char *q = str2;
    int i = 0;
    while (*p != '') {
        if (*p == *q || *p == *q + 32 || *p == *q - 32) {
            char *p1 = p;
            char *q1 = q;
            while (*p1 != '' && *q1 != '') {
                if (*p1 != *q1 && *p1 != *q1 + 32 && *p1 != *q1 - 32) {
                    break;
                }
                p1++;
                q1++;
            }
            if (*q1 == '') {
                return i;
            }
        }
        p++;
        i++;
    }
    return -1;
}

//字串查詢指定長度忽略大小寫 從指定位置開始到什麼位置結束,start從0開始 ,end從1開始
int str_find_ignore_case_n(char *str1, char *str2, int start, int end) {
    char *p = str1 + start;
    char *q = str2;
    int i = start;
    while (*p != '' && i < end) {
        if (*p == *q || *p == *q + 32 || *p == *q - 32) {
            char *p1 = p;
            char *q1 = q;
            while (*p1 != '' && *q1 != '') {
                if (*p1 != *q1 && *p1 != *q1 + 32 && *p1 != *q1 - 32) {
                    break;
                }
                p1++;
                q1++;
            }
            if (*q1 == '') {
                return i;
            }
        }
        p++;
        i++;
    }
    return -1;
}


//從結尾開始查詢 str1字串 str2子串
int str_find_reverse(char *str1, char *str2) {
    int i = str_length(str1) - 1;
    int j = str_length(str2) - 1;
    while (i >= 0) {
        if (str1[i] == str2[j]) {
            int k = i;
            int l = j;
            while (str1[k] == str2[l] && l >= 0) {
                k--;
                l--;
            }
            if (l == -1) {
                return k + 1;
            }
        }
        i--;
    }
    return -1;
}


//從結尾開始查詢到什麼位置結束,start從0開始 ,end從1開始
int str_find_reverse_n(const char *str1, char *str2, int start, int end) {
    int i = start;
    int j = str_length(str2) - 1;
    while (i >= end) {
        if (str1[i] == str2[j]) {
            int k = i;
            int l = j;
            while (str1[k] == str2[l] && l >= 0) {
                k--;
                l--;
            }
            if (l == -1) {
                return k + 1;
            }
        }
        i--;
    }
    return -1;
}


//字串替換(只替換第一個)  str1:源字串  str2:要替換的字串  str3:替換成的字串
char *str_replace(char *str1, char *str2, char *str3) {
    int i = str_find(str1, str2);
    if (i == -1) {
        return str1;
    }
    int len1 = str_length(str1);
    int len2 = str_length(str2);
    int len3 = str_length(str3);
    char *str = (char *) malloc(sizeof(char) * (len1 - len2 + len3 + 1));
    int j = 0;
    int k = 0;
    while (j < i) {
        str[j] = str1[j];
        j++;
    }
    while (k < len3) {
        str[j] = str3[k];
        j++;
        k++;
    }
    k = i + len2;
    while (k < len1) {
        str[j] = str1[k];
        j++;
        k++;
    }
    str[j] = '';
    return str;
}


//字串替換(全部替換)  str1:源字串  str2:要替換的字串  str3:替換成的字串
char *str_replace_all(char *str1, char *str2, char *str3) {
    int i = str_find(str1, str2);
    if (i == -1) {
        return str1;
    }
    int len1 = str_length(str1);
    int len2 = str_length(str2);
    int len3 = str_length(str3);
    char *str = (char *) malloc(sizeof(char) * (len1 - len2 + len3 + 1));
    int j = 0;
    int k = 0;
    while (j < i) {
        str[j] = str1[j];
        j++;
    }
    while (k < len3) {
        str[j] = str3[k];
        j++;
        k++;
    }
    k = i + len2;
    while (k < len1) {
        str[j] = str1[k];
        j++;
        k++;
    }
    str[j] = '';
    return str_replace_all(str, str2, str3);
}


//字串分割  str1:源字串  str2:分割符   返回值:分割後的字串陣列
CharList *str_split(char *str1, char *str2) {
    char *p = str1;
    char *q = str2;
    int i = 0;
    int j = 0;
    int k = 0;
    int m = 0;
    int n = 0;
    while (*p != '') {
        if (*p == *q) {
            char *p1 = p;
            char *q1 = q;
            while (*p1 != '' && *q1 != '') {
                if (*p1 != *q1) {
                    break;
                }
                p1++;
                q1++;
            }
            if (*q1 == '') {
                i++;
            }
        }
        p++;
    }
    char **str = (char **) malloc(sizeof(char *) * (i + 1));
    p = str1;
    while (*p != '') {
        if (*p == *q) {
            char *p1 = p;
            char *q1 = q;
            while (*p1 != '' && *q1 != '') {
                if (*p1 != *q1) {
                    break;
                }
                p1++;
                q1++;
            }
            if (*q1 == '') {
                str[j] = (char *) malloc(sizeof(char) * (p - str1 - m + 1));
                for (k = 0; k < p - str1 - m; k++) {
                    str[j][k] = str1[m + k];
                }
                str[j][k] = '';
                j++;
                m = p - str1 + 1;
            }
        }
        p++;
    }
    str[j] = (char *) malloc(sizeof(char) * (p - str1 - m + 1));
    for (k = 0; k < p - str1 - m; k++) {
        str[j][k] = str1[m + k];
    }
    str[j][k] = '';
    str[j + 1] = NULL; //陣列最後一個元素為NULL 用於判斷是否到達陣列末尾
    //轉換為charlist集合
    CharList *pCharlist = createCharList(str_array_length(str));
    int length = str_array_length(str);
    for (i = 0; i < length; ++i) {
        addCharList(pCharlist, str[i]);
    }
    return pCharlist;
}

//獲取字串包含指定字元的個數 str1是字串  str2是指定字元
int str_contain_count(char *str1, char *str2) {
    int i = 0;
    int j = 0;
    while (str1[i] != '') {
        if (str1[i] == str2[j]) {
            j++;
        } else {
            j = 0;
        }
        if (str2[j] == '') {
            j = 0;
            i++;
        }
        i++;
    }
    return i;
}

//擷取字串  str:源字串  start:開始位置  end:結束位置  start和end都是從0開始的
char *str_substring(char *str, int start, int end) {
    char *p = str;
    int i = 0;
    int j = 0;
    while (*p != '') {
        p++;
        i++;
    }
    if (start < 0 || start > i) {
        return NULL;
    }
    if (end < 0 || end > i) {
        return NULL;
    }
    if (start > end) {
        return NULL;
    }
    char *str1 = (char *) malloc(sizeof(char) * (end - start + 1));
    p = str;
    while (j < start) {
        p++;
        j++;
    }
    int k = 0;
    while (j <= end) {
        str1[k] = *p;
        p++;
        j++;
        k++;
    }
    str1[k] = '';
    return str1;
}

//列印字串陣列
void str_array_print(char **str) {
    int i = str_array_length(str);
    //列印字串陣列
    for (int j = 0; j < i; j++) {
        printf("%s", str[j]);
        //如果不是最後一個元素,列印逗號
        if (j != i - 1) {
            printf(",");
        }
    }
}

//列印字串
void str_print(char *str) {
    printf("%sn", str);
}

//字串反轉
char *str_reverse(char *str) {
    char *p = str;
    int i = 0;
    int j = 0;
    while (*p != '') {
        p++;
        i++;
    }
    char *str1 = (char *) malloc(sizeof(char) * (i + 1));
    p = str;
    while (*p != '') {
        str1[i - j - 1] = *p;
        p++;
        j++;
    }
    str1[i] = '';
    return str1;
}

//判斷字串是否為空
int str_is_empty(const char *str) {
    if (str == NULL || str[0] == '') {
        return 1;
    }
    return 0;
}

//判斷開頭是否包含指定字串 str1:源字串  str2:指定字串 返回值:1包含  0不包含
BOOL str_start_with(char *str1, char *str2) {
    char *p = str1;
    char *q = str2;
    while (*p != '' && *q != '') {
        if (*p != *q) {
            return 0;
        }
        p++;
        q++;
    }
    if (*q == '') {
        return TRUE;
    }
    return FALSE;
}

//判斷結尾是否包含指定字串 str1:源字串  str2:指定字串 返回值:1包含  0不包含
BOOL str_end_with(char *str1, char *str2) {
    char *p = str1;
    char *q = str2;
    int i = 0;
    int j = 0;
    while (*p != '') {
        p++;
        i++;
    }
    while (*q != '') {
        q++;
        j++;
    }
    if (i < j) {
        return FALSE;
    }
    p = str1;
    q = str2;
    while (*p != '') {
        p++;
    }
    while (*q != '') {
        q++;
    }
    while (j > 0) {
        if (*p != *q) {
            return FALSE;
        }
        p--;
        q--;
        j--;
    }
    return TRUE;
}


//最好的char型別的hash演演算法,衝突較少,效率較高
unsigned int hashCode(char *str) {
    unsigned int seed = 131;
    unsigned int hash = 0;

    while (*str) {
        hash = hash * seed + (*str++);
    }
    return (hash & 0x7FFFFFFF);
}


//字串包含 str1:源字串是否包含str2  包含返回1  不包含返回0
int str_contains(char *str1, char *str2) {
    char *p = str1;
    char *q = str2;
    while (*p != '') {
        if (*p == *q) {
            char *p1 = p;
            char *q1 = q;
            while (*p1 != '' && *q1 != '') {
                if (*p1 != *q1) {
                    break;
                }
                p1++;
                q1++;
            }
            if (*q1 == '') {
                return 1;
            }
        }
        p++;
    }
    return 0;
}

//獲取單個字元的ascii碼
int str_char_code(const char *str) {
    return (int) *str;
}

//獲取字串的的單個字元 如果沒有找到那麼返回0,找到返回對應的字元
char str_char_at(char *str, int index) {
    char *p = str;
    int i = 0;
    while (*p != '') {
        if (i == index) {
            char str1 = *p;
            return str1;
        }
        p++;
        i++;
    }
    return 0;
}


//判斷是否是數位包括負數  是返回1  不是返回0
int str_is_number(char *str) {
    char *p = str;
    int i = 0;
    while (*p != '') {
        if (*p == '-') {
            if (i != 0) {
                return 0;
            }
        } else if (*p < '0' || *p > '9') {
            return 0;
        }
        p++;
        i++;
    }
    return 1;
}

//判斷是否都是字元  是返回1  不是返回0
int str_is_char(char *str) {
    char *p = str;
    while (*p != '') {
        if (*p < 'a' || *p > 'z') {
            if (*p < 'A' || *p > 'Z') {
                return 0;
            }
        }
        p++;
    }
    return 1;
}

//判斷是否是小數包括負數  是返回1  不是返回0
int str_is_double(char *str) {
    char *p = str;
    int i = 0;
    int j = 0;
    while (*p != '') {
        if (*p == '-') {
            if (i != 0) {
                return 0;
            }
        } else if (*p == '.') {
            j++;
            if (j > 1) {
                return 0;
            }
        } else if (*p < '0' || *p > '9') {
            return 0;
        }
        p++;
        i++;
    }
    return 1;
}


//判斷是否是特殊字元(除了字母和數位之外的符號都是特殊字元)  是返回1  不是返回0
int str_is_special_char(char *str) {
    char *p = str;
    while (*p != '') {
        if (*p >= 'a' && *p <= 'z') {
            p++;
            continue;
        }
        if (*p >= 'A' && *p <= 'Z') {
            p++;
            continue;
        }
        if (*p >= '0' && *p <= '9') {
            p++;
            continue;
        }
        return 1;
    }
    return 0;
}

//判斷是否是字母和數位組成的字串  是返回1  不是返回0
int str_is_char_number(char *str) {
    char *p = str;
    while (*p != '') {
        if (*p >= 'a' && *p <= 'z') {
            p++;
            continue;
        }
        if (*p >= 'A' && *p <= 'Z') {
            p++;
            continue;
        }
        if (*p >= '0' && *p <= '9') {
            p++;
            continue;
        }
        return 0;
    }
    return 1;
}

//判斷字串是否是布林值  是返回1  不是返回0
int str_is_boolean(char *str) {
    if (str_compare(str, "true") || str_compare(str, "false")) {
        return 1;
    }
    return 0;
}

//判斷字串是否是迴文字串  是返回1  不是返回0   (迴文字串:正讀和反讀都一樣的字串)
int str_is_palindrome(char *str) {
    char *p = str;
    char *q = str;
    int i = 0;
    int j = 0;
    while (*p != '') {
        p++;
        i++;
    }
    while (*q != '') {
        q++;
        j++;
    }
    if (i < j) {
        return 0;
    }
    p = str;
    q = str;
    while (*p != '') {
        p++;
    }
    while (*q != '') {
        q++;
    }
    while (i > 0 && j > 0) {
        if (*p != *q) {
            return 0;
        }
        p--;
        q--;
        i--;
        j--;
    }
    if (j == 0) {
        return 1;
    }
    return 0;
}

// 判斷字串是,數位,字母,小數,數位返回1,字母返回2,小數返回3,其他返回0
int str_is_what(char *str) {
    if (str_is_number(str)) {
        return 1;
    }
    if (str_is_char(str)) {
        return 2;
    }
    if (str_is_double(str)) {
        return 3;
    }
    return 0;
}

//判斷是否是數值  是返回1  不是返回0
int str_is_numeric(char *str) {
    if (str_is_number(str)) {
        return 1;
    }
    if (str_is_double(str)) {
        return 1;
    }
    return 0;
}


//計算兩個字串的相似度,返回百分比 0-100
int str_similarity(char *str1, char *str2) {
    char *p = str1;
    char *q = str2;
    int i = 0;
    int j = 0;
    int k = 0;
    while (*p != '') {
        p++;
        i++;
    }
    while (*q != '') {
        q++;
        j++;
    }
    p = str1;
    q = str2;
    while (*p != '') {
        while (*q != '') {
            if (*p == *q) {
                k++;
            }
            q++;
        }
        p++;
        q = str2;
    }
    if (i > j) {
        return (int) (k * 100 / i);
    }
    return (int) (k * 100 / j);
}


//字串轉布林值
BOOL str_to_bool(char *str) {
    if (str_compare(str, "true")) {
        return TRUE;
    } else {
        return FALSE;
    }
}

//布林值轉字串
char *bool_to_str(BOOL bool) {
    if (bool) {
        return "true";
    } else {
        return "false";
    }
}


//單個字串的操作(自增(++),自減(--),取反(~),布林取反(!),取絕對值(+),取負(-),向上取整(ceil),向下取整(floor),四捨五入(round)
char *str_calculate_one(char *a, char *symbol) {
    //判斷是否是數值
    if (str_is_numeric(a)) {
        if (str_compare(symbol, "++")) { //自增
            return int_to_str(str_to_int(a) + 1);
        }
        if (str_compare(symbol, "--")) { //自減
            return int_to_str(str_to_int(a) - 1);
        }
        if (str_compare(symbol, "~")) { //按位元取反
            return int_to_str(~str_to_int(a));
        }
        if (str_compare(symbol, "-")) { //取負
            if (*a != '-') { //如果不是負數
                return int_to_str(-str_to_int(a));
            } else {
                return a;
            }
        }
        if (str_compare(symbol, "+")) { //取絕對值
            return int_to_str(abs(str_to_int(a)));
        }
        if (str_is_double(a)) { //如果是小數
            if (str_compare(symbol, "ceil")) { //向上取整
                return double_to_str(ceil(str_to_double(a)));
            }
            if (str_compare(symbol, "floor")) { //向下取整
                return double_to_str(floor(str_to_double(a)));
            }
            if (str_compare(symbol, "round")) { //四捨五入
                return double_to_str(round(str_to_double(a)));
            }
        } else {//如果是整數
            return a;
        }
    }
    //判斷是否是布林值
    if (str_is_boolean(a)) {
        if (str_compare(symbol, "!")) { //取反
            if (str_compare(a, "true")) {
                return bool_to_str(0);
            }
            return bool_to_str(1);
        }
    }
    return NULL;
}


//計算兩個字串的各種操作
// 數值運算: 加(+) 減(-) 乘(*) 除(/) 取餘(%) 自增(++) 自減(--)
// 比較運算: 大於(>)、小於(<)、等於(==)、 大於等於(>=)、小於等於(<=)和不等於(!=)
// 邏輯運算: 包括與(&&)、或(||)、非(!)
// 位元運算: 按位元與(&)、按位元或(|)、按位元互斥或(^)、按位元取反(~)
char *str_calculate_two(char *a, char *b, char *symbol) {
    //判斷是否是整數
    if (str_is_numeric(a) && str_is_numeric(b)) {
        if (str_compare(symbol, "+")) { //加
            if (str_is_number(a) && str_is_number(b)) {
                return int_to_str(str_to_int(a) + str_to_int(b));
            }
            return double_to_str(str_to_double(a) + str_to_double(b));

        }
        if (str_compare(symbol, "-")) { //減
            if (str_is_number(a) && str_is_number(b)) {
                return int_to_str(str_to_int(a) - str_to_int(b));
            }
            return double_to_str(str_to_double(a) - str_to_double(b));

        }
        if (str_compare(symbol, "*")) { //乘
            if (str_is_number(a) && str_is_number(b)) {
                return int_to_str(str_to_int(a) * str_to_int(b));
            }
            return double_to_str(str_to_double(a) * str_to_double(b));

        }
        if (str_compare(symbol, "/")) { //除
            if (str_is_number(a) && str_is_number(b)) {
                return int_to_str(str_to_int(a) / str_to_int(b));
            }
            return double_to_str(str_to_double(a) / str_to_double(b));

        }
        if (str_compare(symbol, "%")) { //取餘
            return int_to_str(str_to_int(a) % str_to_int(b));

        }
        if (str_compare(symbol, ">")) { //大於
            if (str_is_number(a) && str_is_number(b)) {
                return bool_to_str(str_to_int(a) > str_to_int(b));
            }
            return bool_to_str(str_to_double(a) > str_to_double(b));
        }
        if (str_compare(symbol, "<")) { //小於
            if (str_is_number(a) && str_is_number(b)) {
                return bool_to_str(str_to_int(a) < str_to_int(b));
            }
            return bool_to_str(str_to_double(a) < str_to_double(b));
        }
        if (str_compare(symbol, "==")) { //等於
            if (str_is_number(a) && str_is_number(b)) {
                return bool_to_str(str_to_int(a) == str_to_int(b));
            }
            return bool_to_str(str_to_double(a) == str_to_double(b));
        }
        if (str_compare(symbol, ">=")) { //大於等於
            if (str_is_number(a) && str_is_number(b)) {
                return bool_to_str(str_to_int(a) >= str_to_int(b));
            }
            return bool_to_str(str_to_double(a) >= str_to_double(b));
        }
        if (str_compare(symbol, "<=")) { //小於等於
            if (str_is_number(a) && str_is_number(b)) {
                return bool_to_str(str_to_int(a) <= str_to_int(b));
            }
            return bool_to_str(str_to_double(a) <= str_to_double(b));
        }
        if (str_compare(symbol, "!=")) { //不等於
            if (str_is_number(a) && str_is_number(b)) {
                return bool_to_str(str_to_int(a) != str_to_int(b));
            }
            return bool_to_str(str_to_double(a) != str_to_double(b));
        }
        if (str_is_number(a) && str_is_number(b)) {
            if (str_compare(symbol, "&")) { //按位元與
                return int_to_str(str_to_int(a) & str_to_int(b));
            }
            if (str_compare(symbol, "|")) { //按位元或
                return int_to_str(str_to_int(a) | str_to_int(b));
            }
            if (str_compare(symbol, "^")) { //按位元互斥或
                return int_to_str(str_to_int(a) ^ str_to_int(b));
            }
            if (str_compare(symbol, "<<")) { //左移
                return int_to_str(str_to_int(a) << str_to_int(b));
            }
            if (str_compare(symbol, ">>")) { //右移
                return int_to_str(str_to_int(a) >> str_to_int(b));
            }
        }
    }

    if (str_is_boolean(a) && str_is_boolean(b)) {
        if (str_compare(symbol, "&&")) { //與
            return bool_to_str(str_to_bool(a) && str_to_bool(b));
        }
        if (str_compare(symbol, "||")) { //或
            return bool_to_str(str_to_bool(a) || str_to_bool(b));
        }
    }

    return NULL;
}



//建立一個字串
char *str_create(char *str) {
    return str_copy(str);
}

//追加字串
char *str_append(char *str1, char *str2) {
    return str_concat(2, str1, str2);
}


//模板字串(字串中可以包含變數) 例如: "hello${name},age${age}"  傳輸: {name:world,age=123}  返回: hello world,age 123
char *str_template(char *str, char *template) {
    if (str == NULL || str_length(str) == 0) {
        return NULL;
    }
    CharHashMap *pMap = str_to_json_map(template);
    if (pMap == NULL || pMap->size == 0) {
        return str;
    }
    char *result = str;
    CharHashMapIterator *pIterator = createCharHashMapIterator(pMap);
    while (hasNextCharHashMapIterator(pIterator)) {
        CharKvLinkedNode *pNode = nextCharHashMapIterator(pIterator);
        char *key = pNode->key;
        //拼接key,例如: ${name}
        char *key_str = str_concat(3, "${", key, "}");
        //替換 ${name} 為 value
        result = str_replace(result, key_str, pNode->value);
    }
    charHashMapClear(pMap);//清空map,防止記憶體漏失
    return result;
}

//模板字串,map版本
char *str_template_map(char *str, CharHashMap *pMap) {
    if (str == NULL || str_length(str) == 0) {
        return NULL;
    }
    if (pMap == NULL || pMap->size == 0) {
        return str;
    }
    char *result = str;
    CharHashMapIterator *pIterator = createCharHashMapIterator(pMap);
    while (hasNextCharHashMapIterator(pIterator)) {
        CharKvLinkedNode *pNode = nextCharHashMapIterator(pIterator);
        char *key = pNode->key;
        //拼接key,例如: ${name}
        char *key_str = str_concat(3, "${", key, "}");
        //替換 ${name} 為 value
        result = str_replace(result, key_str, pNode->value);
    }
    return result;
}

// 字串模板  formatStr("%s is %d year old.", "frank", 25);  //frank is 25 year old.
char *formatStr(char *format, ...) {
    char *pszBuff = malloc(1024);
    va_list vl;
    va_start(vl, format);
    vsprintf(pszBuff, format, vl);
    return pszBuff;
}


//獲取n~n之間的所有數值
CharList *get_range(char *start, char *end) {
    if (start == NULL || end == NULL) {
        return NULL;
    }
    if (!str_is_number(start) || !str_is_number(end)) {
        return NULL;
    }
    int start_int = str_to_int(start);
    int end_int = str_to_int(end);
    CharList *pList = createCharList(end_int - start_int + 1);
    for (int i = start_int; i <= end_int; i++) {
        char *str = int_to_str(i);
        addCharList(pList, str);
    }
    return pList;
}

以上就是C語言實現手寫字串處理工具的範例程式碼的詳細內容,更多關於C語言字串處理工具的資料請關注it145.com其它相關文章!


IT145.com E-mail:sddin#qq.com