c语言程序设计案例教程(第2版)笔记(六)—字符串处理实例
字符串处理
功能描述:从键盘输入一个文本行后,为用户提供菜单选择,实现字符串一些操作——显示文本行、查找并替换指定子串、删除指定子串、统计指定子串数目。实现代码:
1 #include  2 #include  3 #include  4 #pragma warning(disable:4996)  5   6 #define NUM 256  7   8 void displayMenu();  9 int choiceItem(); 10 void searchReplace(char *buf, char *s, char *t); 11 void deleteString(char *buf, char *s); 12 int totle(char *buf, char *s); 13  14 main() 15 { 16     char buf[NUM]; 17     char s[80], t[80]; 18     int choice; 19  20     system("cls"); 21     printf("Enter a text line : ");   /*输入待操作文本行*/ 22     gets(buf); 23     do{ 24         choice = choiceItem();    /*选择菜单项*/ 25         switch (choice){ 26         case 1: 27             printf("Search:"); 28             gets(s); 29             printf("\nReplace:"); 30             gets(t); 31             searchReplace(buf, s, t); 32             printf("\nThe result is %s\n", buf); 33             break; 34         case 2: 35             printf("\nDelete:"); 36             gets(s); 37             deleteString(buf, s); 38             printf("\nThe result is %s\n", buf); 39             break; 40         case 3: 41             printf("\nSearch:"); 42             gets(s); 43             printf("\nThe counts of %s is %d\n", s, totle(buf, s)); 44             break; 45         case 4: 46             printf("\nThe string is %s\n", buf); 47             break; 48         } 49     } while (choice != 0); 50     printf("\n\nBey!"); 51 } 52  53 void displayMenu()   /*显示菜单*/ 54 { 55     printf("\n==========MENU===========\n"); 56     printf("1............Search/Replace"); 57     printf("\n2............Delete"); 58     printf("\n3............Totle"); 59     printf("\n4............Display"); 60     printf("\n0............Exit\n"); 61     printf("\nChoice:\n"); 62 } 63  64 int choiceItem()   /*菜单选择*/ 65 { 66     int choice; 67     char line[80]; 68  69     do{ 70         displayMenu(); 71         gets(line); 72         choice = atoi(line);   /*将字符串转化为整型*/ 73     } while (choice<0 || choice>4); 74     return choice; 75 } 76  77 void searchReplace(char *buf, char *s, char *t)   /*查找替换子串*/ 78 { 79     char m[256];   /*内部缓冲区*/ 80     char *searchPtr = NULL; 81     do{ 82         searchPtr = strstr(buf, s);   /*查找子串*/ 83         if (searchPtr != NULL){       84             strcpy(m, searchPtr + strlen(s));   /*将子串后面的字符串备份到m中*/ 85             strcpy(searchPtr, t); 86             strcpy(searchPtr + strlen(t), m); 87         } 88     } while (searchPtr != NULL); 89 } 90  91 void deleteString(char *buf, char *s)   /*删除子串*/ 92 { 93     char *searchPtr = NULL; 94     do{ 95         searchPtr = strstr(buf, s);   /*查找子串*/ 96         if (searchPtr != NULL){ 97             strcpy(searchPtr, searchPtr + strlen(s)); 98         } 99     } while (searchPtr != NULL);100 }101 102 int totle(char *buf, char *s)   /*统计子串出现的次数*/103 {104     int n = 0;105     char *searchPtr = NULL;106     do{107         searchPtr = strstr(buf, s);   /*查找子串*/108         if (searchPtr != NULL){109             n++;110             buf = searchPtr + strlen(s);   /*改变查找的初始位置*/111         }112     } while (searchPtr != NULL);113     return n;114
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。