《C和指针》章节后编程练习解答参考——6.2
《C和指针》——6.2
题目:
编写一个函数,删除源字符串中含有的子字符串部分。
函数原型:
int del_substr(char *str, char const *substr);
解答代码:
#include <stdio.h> int del_substr(char *str, char const *substr)
{
if ((*str != NULL) && (*substr != NULL))
{
for(; *str != '\0'; str++)
{
if (*str == *substr)
{
int i=;
while ((*(str+i) != '\0') && (*(substr+i) != '\0'))
{
if (*(str+i) == *(substr+i))
{
i++; //保持两个字符串指针同时加1
}
else
break;
} if ((*(substr+i) == '\0')) //子字符串检测结束
{
for(; *(str+i) != '\0'; str++)
{
*str = *(str+i);
}
*str = '\0'; //将源字符串后边的字符舍弃
return ;
}
else
{
return ;
}
}
}
}
return ;
} int main()
{
char source[] = "ABCDEFGHIJKLMN";
char subsource[] = "DEF"; printf("%s\n", source);
printf("%s\n", subsource); if (del_substr(source, subsource) == )
{
printf("Delete the substr:\n");
printf("%s", source);
}
else
printf("Have not found the substr!"); getchar();
return ;
}
注:
1、应先判断源字符串中是否包含子字符串,检测时要保持两个指针增量同步。
2、子字符串对比完后判断是否包含于源字符串。
《C和指针》章节后编程练习解答参考——6.2的更多相关文章
- 《C和指针》章节后编程练习解答参考——6.3
<C和指针>——6.3 题目: 编写一个函数,把参数字符串中的字符反向排列. 函数原型: void reverse_string(char *string); 要求: 使用指针而不是数组下 ...
- 《C和指针》章节后编程练习解答参考——第5章
5.1 题目: 略 解答代码: #include <stdio.h> int main(void) { char ch; while (((ch = getchar()) != EOF) ...
- 《C和指针》章节后编程练习解答参考——6.6
<C和指针>——6.6 题目: 在指定的下限.上限之间使用数组方法查找质数,并将质数提取出来. 要求: 略 解答代码: #include <stdio.h> #define U ...
- 《C和指针》章节后编程练习解答参考——6.4
<C和指针>——6.4 题目: 质数是只能被1和本身整除的整数. 在1到1000之间的质数,在数组中剔除不是质数的数. 解答代码: #include <stdio.h> #de ...
- 《C和指针》章节后编程练习解答参考——6.1
<C和指针>——6.1 6.1 题目: 编写一个函数,在一个字符串中进行搜索,查找另一子字符串中出现的字符. 函数原型如下: char *find_char(char const *sou ...
- 《C和指针》章节后编程练习解答参考——第10章
10.1 #include <stdio.h> typedef struct { unsigned ]; unsigned ]; unsigned ]; }TelphoneNumber; ...
- 《C和指针》章节后编程练习解答参考——第9章
9.1 #include <stdio.h> #include <ctype.h> #include <string.h> #define N 100 int ma ...
- 《C和指针》章节后编程练习解答参考——第8章
8.1 #include <stdio.h> int main (void) { int a, b, c, d; // 不使用嵌套花括号初始化 unsigned ][][][] = { , ...
- DSAPI多功能组件编程应用-参考-Win32API常数
DSAPI多功能组件编程应用-参考-Win32API常数 在编程过程中,常常需要使用Win32API来实现一些特定功能,而Win32API又往往需要使用一些API常数,百度搜索常数值,查手册,也就成了 ...
随机推荐
- zendstudio xdebug 配置
1. 下载XDebug: http://www.xdebug.org/download.php 通过phpinfo()查看你的php版本,现在相对应的dll文件
- Apple Pay强势来袭,开发者应做的事情
"iOS8.1就已经有这个功能了,只是木有现在这么的火,现在的趋势是要火的节奏,因此很多电商平台B2B,P2P,C2C,X2X都有可能需要这个屌丝的付款功能了,在此简单的研究一下." ...
- bootbox.js [v4.2.0]设置确认框 按钮语言为中文
Bootbox.js (http://bootboxjs.com/)是一个小型的 JavaScript 库用来创建简单的可编程对话框,基于 Twitter 的 Bootstrap 开发. 弹出确认框方 ...
- 洛谷 P1040 加分二叉树
题目描述 设一个n个节点的二叉树tree的中序遍历为(1,2,3,…,n),其中数字1,2,3,…,n为节点编号.每个节点都有一个分数(均为正整数),记第i个节点的分数为di,tree及它的每个子树都 ...
- pomelo 服务器之间的通信
master服务器在启动的时候会启动mater服务,生成一个MasterAgent,作为中心服务器. 然后所有服务器,包括mater服务器,都会启动monitor服务,生成一个MonitorAgent ...
- MapObject shape数据操作
利用MO 的GeoDataset .DataConnection Recordset 进行数据操作 在MO中.使用GeoDataset对象和DataConnection 对象来连接和读取地理数据. ...
- 设置U盘为第一启动顺序
本文转载:http://u.diannaodian.com/Article/1004.html 盘安装系统的首要步骤就是设置U盘为第一启动顺序.下面电脑店官网就来说说到底如何来设置U盘启动顺序吧. ...
- poj2406--Power Strings(KMP求最小循环节)
Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 33178 Accepted: 13792 D ...
- JavaCodeTra 猴子选猴王 约瑟夫循环
之前用的是循环链表,java刚学,不知道怎么用链表.用个小算法吧 代码: import java.util.Scanner; /** * */ /** * @author john * @约瑟夫循环/ ...
- mysqldump 定时任务 执行后备份的文件为空
#!/bin/bash mysql_host="127.0.0.1" mysql_user="root" mysql_passwd="******** ...