国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

C實(shí)例---關(guān)鍵詞查找、替換算法

2019-11-10 17:27:02
字體:
供稿:網(wǎng)友

運(yùn)行環(huán)境:macOS shell 代碼:

#include <stdio.h>#include <string.h>#include <stdlib.h>#define ALL 1#define SINGLE 0int str2num(char *str); //字符串轉(zhuǎn)宏void Find(const char *src, char *argv, int Mode, int *position); //查找函數(shù)char *Replace(char *src, char *argv, char *replace, int Mode, int *position); //替換函數(shù)void atoA(char *src); //小寫轉(zhuǎn)大寫void PRintInfo(void); //打印提示信息void ArrayReverse(int *array); //數(shù)組倒置---冒泡實(shí)現(xiàn)int main (){ char string[1024] = "This year is Chinese new year---the year of the rooster, and happy new year to you!"; char keyWord[15] = {0}; char mode[10] = {0}, M[10] = {0}, replace[15] = {0}; int pos[10] = {0}; int i,count = 0; int len; int argvlen; PrintInfo(); printf("/n/nTest String :/n"); printf("%s/n/n", string); printf("Please input your find or repalce keyword : "); scanf("%[^/n]",keyword); getchar(); printf("Please chose the mode --- /"ALL or SINGLE/": "); scanf("%[^/n]",mode); atoA(mode); /* while input error, resume load up to correct. */ while ( (strcmp(mode, "ALL") != 0) && (strcmp(mode, "SINGLE") != 0) ) { printf("Line %d : Input Mode Error! Sample : all or ALL or single or SINGLE!/n", __LINE__); printf("Resume Mode : "); getchar(); scanf("%[^/n]",mode); atoA(mode); } printf("Debug %d : mode --- %s/n",__LINE__, mode); getchar(); printf("Please chose the mode --- /"FIND or REPLACE/" : "); scanf("%[^/n]",M); atoA(M);// printf("Debug %d : mode --- %s/n",__LINE__, mode); /* while input error, resume load up to correct. */ while ( (strcmp(M, "FIND") != 0) && (strcmp(M, "REPLACE") != 0) ) { printf("Line %d : Input Mode Error! Sample : find or FIND or replace or REPLACE!/n", __LINE__); printf("Resume Mode : "); getchar(); scanf("%[^/n]",M); atoA(M); }// printf("Debug %d : M --- %s/n",__LINE__, M);// printf("Debug %d : mode --- %s/n",__LINE__, mode); if (strcmp(M, "REPLACE") == 0) { printf("Please input your replace keyword : "); getchar(); scanf("%[^/n]",replace); }// printf("Debug %d : mode --- %s/n",__LINE__, mode);// printf("Debug %d : replace --- %s/n",__LINE__, replace); len = (int)strlen(string); argvlen = (int)strlen(keyword); Find(string, keyword, str2num(mode), pos); if (strcmp(M, "FIND") == 0) { for (i = 0; i < len; i ++) { if (pos[count] != -1 && pos[count] != -2 && i == pos[count]) { printf("/e[1;30;47m%s/e[m",keyword); i += argvlen - 1; count ++; } else { printf("%c",string[i]); } } printf("/n"); } else if (strcmp(M, "REPLACE") == 0) { count = 0; Replace(string, keyword, replace, str2num(mode), pos); len = (int)strlen(string); argvlen = (int)strlen(replace); for (i = 0; i < len; i ++) { if (pos[count] != -1 && pos[count] != -2 && i == pos[count]) { printf("/e[1;30;47m%s/e[m",replace); i += argvlen - 1; count ++; } else { printf("%c",string[i]); } } printf("/n"); } return 0;}void PrintInfo(void){ printf("******************************************/n"); printf("* Find and Repalce Test */n"); printf("* Sample : <KeyWord> <Mode> <M> */n"); printf("* Mode : ALL or SINGLE */n"); printf("* M : FIND or REPALCE */n"); printf("******************************************/n");}int str2num(char *str){ int num = 2; if (strcmp(str, "ALL") == 0) num = 1; else if (strcmp(str, "SINGLE") == 0) num = 0; return num;}void Find(const char *src, char *argv, int Mode, int *position){ int i,j,len,argvlen; int count = 0; len = (int)strlen(src); argvlen = (int)strlen(argv); switch(Mode) { case SINGLE: if (strstr(src, argv) != NULL) *position++ = (int)(strstr(src, argv) - src); else *position++ = -1; break; case ALL: for (i = 0; i < len; i ++) { if (src[i] == argv[0]) { for (j = 1; j < argvlen; j ++) { if (src[i+j] == argv[j]) { count ++; } } if (count == argvlen - 1) { *position++ = i; i += argvlen - 1; count = 0; } } } break; default: printf("Line %d : Mode Error! Sample : /"ALL or SINGLE!/"/n",__LINE__); exit(1); break; } *position ++ = -2;}char *Replace(char *src, char *argv, char *replace, int Mode, int *position){ char *p = src; int *posi = position; int len, argvlen,repalcelen,i,j,posm; len = (int)strlen(src); argvlen = (int)strlen(argv); repalcelen = (int)strlen(replace); switch(Mode) { case SINGLE: if (*position != -1 && *position != -2) { if (argvlen == repalcelen) { for (i = 0; i < argvlen; i ++) { src[(*position) + i] = replace[i]; } } else if (argvlen > repalcelen) { posm = argvlen - repalcelen - 1; for (i = 0; i < repalcelen; i ++) { src[(*position) + i] = replace[i]; for (j = *position + repalcelen; src[j]; j ++) { src[j] = src[j + posm]; } } } else { posm = repalcelen - argvlen; len = (int)strlen(src); for (i = len; i >= *position; i --) src[i + posm] = src[i]; for (i = 0; i < repalcelen; i ++) src[(*position) + i] = replace[i]; } } else { printf("Line %d : No keyword to repalce!!/n",__LINE__); } break; case ALL: if (*position != -1 && *position != -2) { if (argvlen == repalcelen) { while(*position != -2) { for (i = 0; i < argvlen; i ++) { src[(*position) + i] = replace[i]; } position ++; } } else if (argvlen > repalcelen) { ArrayReverse(position); for (i = 0; i < 6; i ++) printf("%d ", position[i]); printf("/n"); posm = argvlen - repalcelen - 1; while (*position != -2) { for (i = 0; i < repalcelen; i ++) { src[(*position) + i] = replace[i]; for (j = *position + repalcelen; src[j]; j ++) { src[j] = src[j + posm]; } } position ++; } } else { posm = repalcelen - argvlen; ArrayReverse(position); while (*position != -2) { len = (int)strlen(src); for (i = len; i >= *position; i --) src[i + posm] = src[i]; for (i = 0; i < repalcelen; i ++) src[(*position) + i] = replace[i]; position ++; } } } else { printf("Line %d : No keyword to repalce!!/n",__LINE__); } break; default: printf("Line %d : Mode Error! Sample : /"ALL or SINGLE!/"/n", __LINE__); exit(1); break; } Find(src, replace, Mode, posi); return p;}/*小寫字符轉(zhuǎn)大寫字符*/void atoA(char *src){ while (*src != '/0') { if (*src >= 'a' && *src <= 'z') *src -= 32; src ++; }}void ArrayReverse(int *array){ int count = 0,i,j,tmp; while (array[count] != -2) count ++; for (i = count; i >= 0; i --) { for (j = 0; j < i-1; j ++) { tmp = array[j]; array[j]= array[j+1]; array[j+1] = tmp; } }}

運(yùn)行結(jié)果: 這里寫圖片描述 這里寫圖片描述 這里寫圖片描述 這里寫圖片描述 這里寫圖片描述


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 麻城市| 通榆县| 那坡县| 襄城县| 莱西市| 大竹县| 乐清市| 怀远县| 甘谷县| 涿州市| 建宁县| 上蔡县| 大埔县| 胶州市| 句容市| 渝中区| 康马县| 枣阳市| 绥江县| 阜南县| 和平县| 墨竹工卡县| 广汉市| 渑池县| 贵南县| 上杭县| 高安市| 上栗县| 丰县| 牙克石市| 禹州市| 金乡县| 诸暨市| 太原市| 商南县| 含山县| 金溪县| 密云县| 新干县| 漯河市| 泰来县|