1. 程式人生 > 其它 >c語言判斷迴文字串函式_C語言實現String字串及其函式

c語言判斷迴文字串函式_C語言實現String字串及其函式

技術標籤:c語言判斷迴文字串函式

2b1dbb09a7ee4cc1c952b547dc720f6c.png

供參考學習~

stringUtil.h

#ifndef _STRINGUTIL_H#define _STRINGUTIL_H #define true  1#define false 0typedef char* String;typedef char** Array_t;typedef unsigned char Bool; typedef struct{  char* (*addExtra)(char*, char*);char* (*add)(char*, char*);char* (*newString)(int, ...);void  (*delString)(char*);int   (*split)(char*, char*, Array_t*);int   (*splitExtra)(char*, char*, Array_t*);void  (*delArray)(Array_t, int);char* (*toUpper)(char*);char* (*toLower)(char*);Bool  (*startWith)(char*, char*);Bool  (*endWith)(char*, char*);char* (*join)(Array_t, int);char* (*strip)(char*, char*);}STRINGUTIL;extern STRINGUTIL StringUtil;  void stringUtilTest(void);  #endif

stringUtil.c

#include "stdio.h"#include "stdlib.h"#include "string.h"#include "ctype.h"#include "stdarg.h"#include "stringUtil.h" static char* addExtra(char* p1, char* p2);static char* add(char* p1, char* p2);static char* newString(int num, ...);static void  delString(char* p);static int   split(char* buff, char* separator, Array_t* result);static int   splitExtra(char* buff, char* separator, Array_t* result);static void  delArray(Array_t p, int n);static char* toUpper(char* ptr);static char* toLower(char* ptr);static Bool  startWith(char* src, char* str);static Bool  endWith(char* src, char* str);static char* join(Array_t ptr, int n);static char* strip(char* ptr, char* separator);  STRINGUTIL StringUtil = {.add=add,             .addExtra=addExtra, .newString=newString, .delString=delString, .split=split, .splitExtra=splitExtra, .delArray=delArray, .toUpper=toUpper, .toLower=toLower, .startWith=startWith, .endWith=endWith, .join=join, .strip=strip};  /** * @description: 字串合併 * @param {p1} 字串1 * @param {p2} 字串2 * @return {*} 合併後的字串指標p1 * @attention 會釋放p1,所以呼叫時等號左邊要有,不能省略,否則用的是已經釋放的舊地址。如:str = stringUtil.add(p1,p2) */static char* addExtra(char* p1, char* p2){char* ptr = NULL;ptr = (char*)realloc(p1, strlen(p1)+strlen(p2)+1);if (ptr != NULL) {strcat(ptr, p2);}return ptr;} /** * @description: 字串合併 * @param {p1} 字串1 * @param {p2} 字串2 * @return {*} 合併後的字串指標p1 * @attention 會建立一個新的字串返回 */static char* add(char* p1, char* p2){char* ptr = NULL;ptr = (char*)calloc(strlen(p1)+strlen(p2)+1, 1);if (ptr != NULL) {strcat(ptr, p1);strcat(ptr, p2);}return ptr;} /** * @description: 建立字串 * @param {num} 字串陣列的個數 * @param {...} 多個字串陣列 * @return {*} 建立完的字串指標 * @attention 需要呼叫delString()手動釋放ptr */static char* newString(int num, ...){char *arg = NULL;va_list ap;int length = 0;va_start(ap, num);for (int i = 0; i < num; i++) {arg = va_arg(ap, char*);length += strlen(arg);}// va_end(ap);char* ptr = (char*)calloc(length+1, sizeof(char));if (ptr != NULL) {va_start(ap, num);for (int i = 0; i < num; i++) {arg = va_arg(ap, char*);strcat(ptr, arg);}va_end(ap);}else {printf("malloc failed");}return ptr;}  /** * @description: 釋放一維指標的記憶體 * @param {p} 一維指標 * @return {*} 無 */static void delString(char* p){free(p);p = NULL;} /** * @description: 釋放二維指標的記憶體 * @param {p} 二維指標 * @param {n} 第一維的數量 * @return {*} 無 * @attention 使用本函式可釋放呼叫split()函式後的二維指標的記憶體 */static void delArray(Array_t p, int n){for(int i=0; i