《C和指针》章节后编程练习解答参考——6.1
《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的更多相关文章
- 《C和指针》章节后编程练习解答参考——6.2
<C和指针>——6.2 题目: 编写一个函数,删除源字符串中含有的子字符串部分. 函数原型: int del_substr(char *str, char const *substr); ...
- 《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和指针》章节后编程练习解答参考——第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常数,百度搜索常数值,查手册,也就成了 ...
随机推荐
- winform 加密 解密 分类: WinForm 2014-05-16 15:05 400人阅读 评论(0) 收藏
界面显示: 加密: 解密: 代码实现: public string EncryptString(string str) { #region 加密程序 ...
- codechef Turbo Sort 题解
Input t – the number of numbers in list, then t lines follow [t <= 10^6]. Each line contains one ...
- Delphi Format中的换行符号是什么
Delphi Format中的换行符号是什么 #,s1]); s3#'%s',[s,s1]); ShowMessage(s2); ShowMessage(s3); end; #13#10两边 ...
- Android自己定义控件而且使其能够在xml中自己定义属性
为什么要自己定义View android开发中自己定义View的优点是显而易见的.比方说以下的这个顶部导航,它被设计出如今应用的每一个界面,但每次的内容却不尽同样.我们不能在每一个layout资源中都 ...
- 构造Scala开发环境并创建ApiDemos演示样例项目
从2011年開始写Android ApiDemos 以来.Android的版本号也更新了非常多,眼下的版本号已经是4.04. ApiDemos中的样例也添加了不少,有必要更新Android ApiDe ...
- Java NIO——Selector机制源码分析---转
一直不明白pipe是如何唤醒selector的,所以又去看了jdk的源码(openjdk下载),整理了如下: 以Java nio自带demo : OperationServer.java Oper ...
- PHP中的循环while、do...while、for、foreach四种循环。
php中的while循环,循环执行代码块制定的次数,或者当指定的条件为真时循环执行代码块. 在我们编写代码是时候,我们经常需要一块代码块重复执行多次.我们就可以使用while循环语句来完成这个任务. ...
- ASP.NET MVC 中 ActionResult 和 ViewResult 在使用上的区别
如果确认你返回的是一个视图(view),你可以直接返回类型为ViewResult. 如果你并不是很清楚,或者你根本不想去理解这些东西,你可以直接返回ActionResult
- 16、SQL Server 复制及常见错误处理
SQL Server 复制 复制是一组技术的组合,可以用此组合对数据和数据库对象进行复制由一个数据库移动到另一个数据库. 复制的英文是Replication,重复的意思,而不是Copy.复制的核心功能 ...
- PTHREAD_MUTEX_INITIALIZER问题
PTHREAD_MUTEX_INITIALIZER 与 expected expression before ‘{’ token 在进行 Posix thread 编程时,出现以下编译错误:err ...