《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. my.cnf已经存在,影响安装--mysql

    Found existing config file ./my.cnf on the system. Because this file might be in use, it was not rep ...

  2. [转] 三步将你的 React Native 项目运行在 Web 浏览器上面

    React Native 的出现,让前端工程师拥有了使用 JavaScript 编写原生 APP 的能力.相比之前的 Web app 来说,对于性能和用户体验提升了非常多. 但是 React Nati ...

  3. Fragment之我的解决方案:Fragmentation

    Fragment系列文章:1.Fragment全解析系列(一):那些年踩过的坑2.Fragment全解析系列(二):正确的使用姿势3.Fragment之我的解决方案:Fragmentation 如果你 ...

  4. html、body、document、window的区别

    html是一门超文本标记语言: document对象代表整个html文档,可用来访问页面中的所有元素: body代表document的主体子对象,除浏览器头部,页面中能够看到的内容都属于body中的内 ...

  5. RSA 加解密 签名 示例

    import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.FileOutputStrea ...

  6. js的MVC结构设计

    基于jquery的Deferred,设计出如下MVC架构. 模型model interface.js interface: function(userid){ var dtd = $.Deferred ...

  7. java 引用资源-ClassLoader.getResource()方法

    如图,eclipse中我的包结构为:,我在 spt.app.MainFrame 中可以通过一下代码段使用资源: public static Object obj = ImageIconProxy.cl ...

  8. jQuery AJAX实现调用页面后台方法

    1.新建demo.aspx页面.2.首先在该页面的后台文件demos.aspx.cs中添加引用. using System.Web.Services; 3.无参数的方法调用. 大家注意了,这个版本不能 ...

  9. Zend Server安装后首次运行就出现Internal Server Error的解决

    无论是使用哪个版本的Zend Server来搭建PHP服务器,首次运行都会出现Internal Server Error的错误,对很多新手而言,每当看到这种错误时,那一刻内心绝对都是崩溃的.然而,这个 ...

  10. 分布式发布订阅消息系统 Kafka 架构设计[转]

    分布式发布订阅消息系统 Kafka 架构设计 转自:http://www.oschina.net/translate/kafka-design 我们为什么要搭建该系统 Kafka是一个消息系统,原本开 ...