《C和指针》——6.1

6.1

题目:

  编写一个函数,在一个字符串中进行搜索,查找另一子字符串中出现的字符。

函数原型如下:

  char *find_char(char const *source, char const *chars);

要求:

  a.不适用任何用于操纵字符串的库函数(如:strcpy strcmp等)

  b.函数中不能使用下标引用

解答代码:

#include <stdio.h>

char *find_char(char const *source, char const *chars)
{
int i, j;
if ((*source != NULL) && (*chars != NULL))
{
for (j=; *(source+j) != '\0'; j++)
{
for (i=; *(chars+i) != '\0'; i++)
{
if (*(source+j) == *(chars+i))
{
printf("i = %d\n", j);
return ((char *)(source+j)); //注:source类型为(char const *),函数类型为(char *),返回时需要进行强制类型转换
}
}
}
return NULL;
}
else
return NULL;
} int main()
{
char source[] = "ABCDEB";
char chars[] = "EF"; char *p = find_char(source, chars); if (p != NULL)
{
printf("Find the substr at %d\n", p-source);
}
else
printf("Substr no found!"); getchar();
return ;
}

注:

1、函数中使用双层循环查询对比,外层用于source数据循环,内层用于子字符串数据循环,逐一进行对比。

2、函数定义参数类型为(char const *)和返回值类型(char *)不一致,不能直接返回,必须进行类型强制转换((char *)(source+j))。

《C和指针》章节后编程练习解答参考——6.1的更多相关文章

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

    <C和指针>——6.2 题目: 编写一个函数,删除源字符串中含有的子字符串部分. 函数原型: int del_substr(char *str, char const *substr); ...

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

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

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

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

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

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

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

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

  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. lib_mysqludf_sys的安装过程

    看了太多资料,累坏了,先杂乱的放在这里吧,回头有时间再排版 一.window系统 1.从sqlmap中找到32为的 lib_mysqludf_sys.dll (64位的我没有测试成功) 选择数据库 U ...

  2. UVA 10985 - Rings'n'Ropes(floyd)

    Problem D Rings'n'Ropes Time Limit: 3 seconds "Well, that seems to be the situation. But, I don ...

  3. ImageView类简介

    4.8  图片控件 本节将要介绍的是图片控件ImageView,首先对ImageView类进行简单介绍,然后通过一个案例来说明ImageView的用法. 4.8.1  ImageView类简介 Ima ...

  4. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(4)-构建项目解决方案 创建EF DataBase Frist模式

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(4)-构建项目解决方案 创建EF DataBase Frist模式 进行本次文章之前,我们可能需要补充一些 ...

  5. TREEVIEW节点拖拽

    http://files.cnblogs.com/xe2011/TreeView_Drag_and_Drop.rar       假设把A节点往B节点上拖拽 那么  A 为Node1,B为Node2 ...

  6. golang 学习笔记

    golan 声明的变量必须要用到? 语法 a,b:=2323; b为 bool 类型 结构体的赋值 需要用到逗号分隔字段 并且最后一个字段后也必须加上逗号 这和 JavaScript 的对象不一样哦 ...

  7. android 47 service绑定

    如果一个service已经启动了,activity和service绑定了在解除邦定,则这个service不会销毁,因为这个service不是这个Activity创建的. service生命周期: Ac ...

  8. Enable Access Logs in JBoss 7 and tomcat--转

    JBoss 7 is slightly different than earlier version JBoss 5 or 6. The procedure to enable access logs ...

  9. shell入门之expr的使用 分类: 学习笔记 linux ubuntu 2015-07-10 14:59 76人阅读 评论(1) 收藏

    在expr中加减乘除的使用,脚本如下: #!/bin/sh #a test about expr v1=`expr 5 + 6` echo "$v1" echo `expr 3 + ...

  10. Linux写配置HDF5的python包h5py

    闲言碎语不讲,直接进入正题.Python在科学计算的应用越来越丰度,而hdf(5)数据的应用也非常广泛.python提供了h5py包供开发者处理数据(http://www.h5py.org/).在wi ...