博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c语言程序设计案例教程(第2版)笔记(六)—字符串处理实例
阅读量:4610 次
发布时间:2019-06-09

本文共 3210 字,大约阅读时间需要 10 分钟。

字符串处理

  • 功能描述:从键盘输入一个文本行后,为用户提供菜单选择,实现字符串一些操作——显示文本行、查找并替换指定子串、删除指定子串、统计指定子串数目
  • 实现代码:
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 }

 

转载于:https://www.cnblogs.com/sunshine-blog/p/8377745.html

你可能感兴趣的文章
linux 装mysql的方法和步骤
查看>>
poj3667(线段树区间合并&区间查询)
查看>>
51nod1241(连续上升子序列)
查看>>
SqlSerch 查找不到数据
查看>>
集合相关概念
查看>>
Memcache 统计分析!
查看>>
(Python第四天)字符串
查看>>
个人介绍
查看>>
使用python动态特性时,让pycharm自动补全
查看>>
MySQL数据库免安装版配置
查看>>
你必知必会的SQL面试题
查看>>
html5 Canvas绘制时钟以及绘制运动的圆
查看>>
Unity3D热更新之LuaFramework篇[05]--Lua脚本调用c#以及如何在Lua中使用Dotween
查看>>
JavaScript空判断
查看>>
洛谷 P1439 【模板】最长公共子序列(DP,LIS?)
查看>>
python timeit
查看>>
Wireless Network 并查集
查看>>
51nod 1019 逆序数
查看>>
20145202马超《JAVA》预备作业1
查看>>
云推送注意(MSDN链接)
查看>>