《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. B和B+树学习笔记

    二叉树 如果数据都在内存中,我们就用平衡二叉查找树即可,这样效率最高. 在前面的文章中我使用过红黑树(大致平衡的二叉查找树),500万节点时,搜索的深度可以达到50,也就是需要50次指针操作才能获取到 ...

  2. 11个让你吃惊的Linux终端命令

  3. bzoj2822: [AHOI2012]树屋阶梯

    咦,这里有好多东西https://en.wikipedia.org/wiki/Catalan_number 每个矩形最多贡献一个拐角 枚举左上角的点和那个拐角是一个矩形 #include<cst ...

  4. 一个可视化的retrospective网站

    IdeaBoardz - Brainstorm, Retrospect, Collaborate是一个可视化的retrospective,brainstorm的网站,比较简单易用,可以导出pdf和ex ...

  5. poj 2926 Requirements

    点击打开poj 2926 思路: n维空间计算最远的曼哈顿距离 分析: 1 题目给定n个5维的点,要求最远的曼哈顿距离 2 求最远曼哈顿距离,对于一个n维的空间,其中两点的曼哈顿距离为:|x1-x2| ...

  6. shell 验证ip

    #!/bin/bash function isIp(){ IP=$ ];then echo "Wrong IP!" exit else a=`echo $IP | awk -F . ...

  7. linux 内核分析+使用SystemTap调试新增内核模块

    http://blog.chinaunix.net/uid/14528823/list/1.html?cid=189394

  8. Android热门网络框架Volley详解

    .Volley简介 volley的英文意思为‘群发’.‘迸发’.Volley是2013年谷歌官方发布的一款Android平台上的网络通信库.Volley非常适合一些数据量不大,但需要频繁通信的网络操作 ...

  9. 解决error:could not open ...jvm.cfg

    出现error:could not open '...jvm.cfg'大多是以前安装jdk或者jre的时候在注册表里注册过,现在安装的文件夹不在或者换了名字,有很多解决方法,最简单的一招是删除java ...

  10. Java基础知识强化07:打印出空心菱形

    1.如图打印出空心菱形: 2.下面是逻辑实现代码: package himi.hebao04; import java.util.Scanner; public class TestDemo08 { ...