《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的更多相关文章

  1. 《C和指针》章节后编程练习解答参考——6.3

    <C和指针>——6.3 题目: 编写一个函数,把参数字符串中的字符反向排列. 函数原型: void reverse_string(char *string); 要求: 使用指针而不是数组下 ...

  2. 《C和指针》章节后编程练习解答参考——第5章

    5.1 题目: 略 解答代码: #include <stdio.h> int main(void) { char ch; while (((ch = getchar()) != EOF) ...

  3. 《C和指针》章节后编程练习解答参考——6.6

    <C和指针>——6.6 题目: 在指定的下限.上限之间使用数组方法查找质数,并将质数提取出来. 要求: 略 解答代码: #include <stdio.h> #define U ...

  4. 《C和指针》章节后编程练习解答参考——6.4

    <C和指针>——6.4 题目: 质数是只能被1和本身整除的整数. 在1到1000之间的质数,在数组中剔除不是质数的数. 解答代码: #include <stdio.h> #de ...

  5. 《C和指针》章节后编程练习解答参考——6.1

    <C和指针>——6.1 6.1 题目: 编写一个函数,在一个字符串中进行搜索,查找另一子字符串中出现的字符. 函数原型如下: char *find_char(char const *sou ...

  6. 《C和指针》章节后编程练习解答参考——第10章

    10.1 #include <stdio.h> typedef struct { unsigned ]; unsigned ]; unsigned ]; }TelphoneNumber; ...

  7. 《C和指针》章节后编程练习解答参考——第9章

    9.1 #include <stdio.h> #include <ctype.h> #include <string.h> #define N 100 int ma ...

  8. 《C和指针》章节后编程练习解答参考——第8章

    8.1 #include <stdio.h> int main (void) { int a, b, c, d; // 不使用嵌套花括号初始化 unsigned ][][][] = { , ...

  9. DSAPI多功能组件编程应用-参考-Win32API常数

    DSAPI多功能组件编程应用-参考-Win32API常数 在编程过程中,常常需要使用Win32API来实现一些特定功能,而Win32API又往往需要使用一些API常数,百度搜索常数值,查手册,也就成了 ...

随机推荐

  1. [置顶] IOS 基础入门教程

    IOS 基础入门教程 教程列表: IOS 简介 IOS环境搭建 Objective C 基础知识 创建第一款iPhone应用程序 IOS操作(action)和输出口(Outlet) iOS - 委托( ...

  2. 【Android - 框架】之ButterKnife的使用

    ButterKnife可以省去控件findViewById的步骤,提高开发效率. 用法: 1.添加依赖: compile 'com.jakewharton:butterknife:5.1.1' 2.在 ...

  3. [置顶] 获取激活码,激活myeclipse

    myeclipse10.0 正式版下载地址: http://downloads.myeclipseide.com/downloads/products/eworkbench/indigo/instal ...

  4. Android之打开闪光灯关键代码

    在AndroidManifest中注册相应的权限: <uses-permission android:name="android.permission.FLASHLIGHT" ...

  5. PHP问题Parse error: syntax error, unexpected end of file in

    检查一下你的php文件中是否存在这样的语法错误:<<php{>或者<?{?>以上两种写法都是有错误的,修改为下面的就可以了: <?php}?>

  6. JSON 入门

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族 ...

  7. hadoop处理Excel通话记录

    前面我们所写mr程序的输入都是文本文件,但真正工作中我们难免会碰到需要处理其它格式的情况,下面以处理excel数据为例 1.项目需求 有刘超与家庭成员之间的通话记录一份,存储在Excel文件中,如下面 ...

  8. PhalGo-介绍

    PhalGo-介绍 phalgo是一个Go语言的一体化开发框架,主要用于API开发应为使用ECHO框架作为http服务web程序一样可以使用,牛顿曾经说过"如果我比别人看得远,那是因为我站在 ...

  9. Java NIO 学习笔记

    为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/SJQ. http://www.cnblogs.com/shijiaqi1066/p/3344148.html ...

  10. spring 定时任务的 执行时间设置规则

    单纯针对时间的设置规则org.springframework.scheduling.quartz.CronTriggerBean允许你更精确地控制任务的运行时间,只需要设置其cronExpressio ...