C语言实现手写字符串处理工具的示例代码
头文件
#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] != '\0') {
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] != '\0') {
if (str[i] != ' ') {
result[j] = str[i];
j++;
}
i++;
}
result[j] = '\0';
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] != '\0') {
if (str[i] != ' ') {
break;
}
i++;
}
while (str[i] != '\0') {
result[j] = str[i];
i++;
j++;
}
result[j] = '\0';
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] != '\0') {
if (str[i] != ' ') {
result[j] = str[i];
j++;
}
i++;
}
result[j] = '\0';
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] != '\0') {
if (str[i] >= 'a' && str[i] <= 'z') {
result[i] = str[i] - 32;
} else {
result[i] = str[i];
}
i++;
}
result[i] = '\0';
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] != '\0') {
if (str[i] >= 'A' && str[i] <= 'Z') {
result[i] = str[i] + 32;
} else {
result[i] = str[i];
}
i++;
}
result[i] = '\0';
return result;
}
//字符串比较 1相等 0不相等
BOOL str_compare(char *str1, char *str2) {
int i = 0;
while (str1[i] != '\0' && str2[i] != '\0') {
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] != '\0' && str2[i] != '\0') {
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] != '\0') {
result[i] = str[i];
i++;
}
result[i] = '\0';
return result;
}
//字符串拷贝指定长度
char *str_copy_n(char *str, int n) {
int i = 0;
char *result = (char *) malloc(sizeof(char) * n);
while (str[i] != '\0' && i < n) {
result[i] = str[i];
i++;
}
result[i] = '\0';
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] != '\0') {
result[j] = str1[k];
j++;
k++;
}
}
result[j] = '\0';
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] != '\0') {
result[i] = str1[i];
i++;
}
while (str2[j] != '\0' && j < n) {
result[i] = str2[j];
i++;
j++;
}
result[i] = '\0';
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 != '\0') {
if (*p == *q || *p == *q + 32 || *p == *q - 32) {
char *p1 = p;
char *q1 = q;
while (*p1 != '\0' && *q1 != '\0') {
if (*p1 != *q1 && *p1 != *q1 + 32 && *p1 != *q1 - 32) {
break;
}
p1++;
q1++;
}
if (*q1 == '\0') {
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 != '\0' && i < end) {
if (*p == *q || *p == *q + 32 || *p == *q - 32) {
char *p1 = p;
char *q1 = q;
while (*p1 != '\0' && *q1 != '\0') {
if (*p1 != *q1 && *p1 != *q1 + 32 && *p1 != *q1 - 32) {
break;
}
p1++;
q1++;
}
if (*q1 == '\0') {
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] = '\0';
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] = '\0';
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 != '\0') {
if (*p == *q) {
char *p1 = p;
char *q1 = q;
while (*p1 != '\0' && *q1 != '\0') {
if (*p1 != *q1) {
break;
}
p1++;
q1++;
}
if (*q1 == '\0') {
i++;
}
}
p++;
}
char **str = (char **) malloc(sizeof(char *) * (i + 1));
p = str1;
while (*p != '\0') {
if (*p == *q) {
char *p1 = p;
char *q1 = q;
while (*p1 != '\0' && *q1 != '\0') {
if (*p1 != *q1) {
break;
}
p1++;
q1++;
}
if (*q1 == '\0') {
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] = '\0';
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] = '\0';
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] != '\0') {
if (str1[i] == str2[j]) {
j++;
} else {
j = 0;
}
if (str2[j] == '\0') {
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 != '\0') {
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] = '\0';
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("%s\n", str);
}
//字符串反转
char *str_reverse(char *str) {
char *p = str;
int i = 0;
int j = 0;
while (*p != '\0') {
p++;
i++;
}
char *str1 = (char *) malloc(sizeof(char) * (i + 1));
p = str;
while (*p != '\0') {
str1[i - j - 1] = *p;
p++;
j++;
}
str1[i] = '\0';
return str1;
}
//判断字符串是否为空
int str_is_empty(const char *str) {
if (str == NULL || str[0] == '\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 != '\0' && *q != '\0') {
if (*p != *q) {
return 0;
}
p++;
q++;
}
if (*q == '\0') {
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 != '\0') {
p++;
i++;
}
while (*q != '\0') {
q++;
j++;
}
if (i < j) {
return FALSE;
}
p = str1;
q = str2;
while (*p != '\0') {
p++;
}
while (*q != '\0') {
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 != '\0') {
if (*p == *q) {
char *p1 = p;
char *q1 = q;
while (*p1 != '\0' && *q1 != '\0') {
if (*p1 != *q1) {
break;
}
p1++;
q1++;
}
if (*q1 == '\0') {
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 != '\0') {
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 != '\0') {
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 != '\0') {
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 != '\0') {
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 != '\0') {
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 != '\0') {
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 != '\0') {
p++;
i++;
}
while (*q != '\0') {
q++;
j++;
}
if (i < j) {
return 0;
}
p = str;
q = str;
while (*p != '\0') {
p++;
}
while (*q != '\0') {
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 != '\0') {
p++;
i++;
}
while (*q != '\0') {
q++;
j++;
}
p = str1;
q = str2;
while (*p != '\0') {
while (*q != '\0') {
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语言字符串处理工具的资料请关注其它相关文章!
相关文章