《c程序设计语言》读书笔记-4.1-判断字符串在另一个字符串中的位置
来源:互联网 发布:js 判断元素隐藏 编辑:程序博客网 时间:2024/06/05 17:48
#include <io.h>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <math.h>#include <time.h>#define Num 20int strindex(char s[],char t[]){ int i,j,k; int position = -1; for(i = 0;s[i] != '\0';i++) { for(j = i,k = 0;t[k] != '\0' && s[j] == t[k];j++,k++) ; if(k > 0 && t[k] == '\0') position = i; } return position;}int main(){ char str1[20],str2[20]; char c; int i = 0,j = 0; int position; printf("please input string1\n"); while((c = getchar()) != '\n' && i < Num) { str1[i++] = c; } str1[i] = '\0'; printf("please input string2\n"); while((c = getchar()) != '\n' && j < Num) { str2[j++] = c; } str2[j] = '\0'; position = strindex(str1,str2); printf("%d\n",position);return 0;}
上面的程序是正确的,可以正常运行得出结果,不过,我又编了下面的函数:
#include <io.h>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <math.h>#include <time.h>int strindex(char s[],char t[]){ int i,j,k; int position = -1;printf("%d\n",strlen(s)); for(i = 0;i < strlen(s);i++) { printf("%d\n",i); for(j = i,k = 0;k < strlen(t) && s[j] == t[k];j++,k++) ; if(k > 0 && t[k] == '\0') position = i; } return position;}int main(){ char *str1,*str2; int i = 0,j = 0; int position; printf("please input string1\n"); gets(str1); printf("please input string2\n"); gets(str2); position = strindex(str1,str2); printf("%d\n",position);return 0;}
为了不定义数组的大小就用的指针,可是程序错了。。gets函数只能读入8个字符,这里面有问题,可是我不知道哪里错了,待看完指针那章看能不能解决!
0 0
- 《c程序设计语言》读书笔记-4.1-判断字符串在另一个字符串中的位置
- 《c程序设计语言》读书笔记-第二个字符串任意一个在第一个字符串出现的位置,未出先返回-1
- 《c程序设计语言》读书笔记-3.4-数字转字符串
- SQL判断字符在字符串中的位置
- 判断字符串是否在另一个字符串中
- SysUtils.LastDelimiter - 判断一个字符串在另一个字符串中最后出现的位置
- C:strstr 查找指定字符串在源字符串中的位置
- 【PHP函数】strstr() 函数搜索一个字符串在另一个字符串中的第一次出现的位置
- 《c程序设计语言》读书笔记-删除字符串中匹配的字符
- 《c程序设计语言》读书笔记-3.5-按要求进制位数字转字符串
- 《c程序设计语言》读书笔记-3.6-数字转字符串最小宽度限制
- 《c程序设计语言》读书笔记-4.12-递归整数转字符串
- 查找一个字符串在另一个字符串的位置原理分析
- 在字符串中的特定位置插入字符串
- c语言中判断一个字符串是否包含另一个字符串
- c语言中判断一个字符串是否包含另一个字符串
- 判断一个字符串在另一个字符串中出现的次数
- 判断一个字符串是否在另一个字符串中
- Eclipse 配置maven web项目
- android应用怎么获取系统权限
- Struts2学习笔记(一)搭建struts2
- Some ussful links to an engineer
- c++的子对象,继承和多态
- 《c程序设计语言》读书笔记-4.1-判断字符串在另一个字符串中的位置
- 64位Win7系统在管理员权限下使用chkdsk命令时直接蓝屏死机然后重启的一种解决办法(不一定通用)
- 不断的选择决定命运——见过的比较好的职业规划的文章
- C++ - 扩展欧几里德算法非递归实现
- 字符串为*号和26个字母的任意组合把*号都移动到最右侧,把字母移到最右侧并保持相对顺序不变
- 马士兵struts2视频教程第五十二集
- IA-32
- 学习OpenCV——车牌检测(定位)
- c++ boost 中的 各种 转换