H面试程序(11): 判断字符串是否包含子串问题
题目描述:
如字符串str1为''abcdef'''
字符串str2为'' bc'';
则字符串str1中含有字串str2;
#include <stdio.h>
#include<malloc.h>
#include<memory.h>
#include<assert.h> int Whether_Is_Substring(char* str1, char * str2) //这个函数只是找到了str1中第一个和字串相同字串
{ //还可以设立一个计数器统计str1中一共包含有几个相同字串
assert(str1);
assert(str2);
int i = 0; int j = 0; while(str1[i] != '\0')
{
while(str1[i] ==str2[j]) //因为是字串问题,必须连续地比
{
i++;
j++;
}
if(str2[j] =='\0') //当字串到达'\0'时,就说明字符串str1中含有字符串str2
return 1;
else //如果没有到达'\0',str2就要重新从第1个字符比起,
j = 0;
i++;
}
return 0; }
int main()
{
char str1[] ="afdasfg";
char str2[] ="fd"; int a = Whether_Is_Substring(str1, str2); //1代表str1包含str2;
if(0 == a)
printf("字符串str1中不包含字符串str2\n");
else
printf("字符串str1中包含字符串str2\n"); return 0; }
H面试程序(11): 判断字符串是否包含子串问题的更多相关文章
- Jquery 选择器 详解 js 判断字符串是否包含另外一个字符串
Jquery 选择器 详解 在线文档地址:http://tool.oschina.net/apidocs/apidoc?api=jquery 各种在线工具地址:http://www.ostools ...
- PHP判断字符串的包含
PHP语言是一个功能强大的嵌入式HTML脚本语言,它的易用性让许多程序员选择使用.PHP判断字符串的包含,可以使用PHP的内置函数strstr,strpos,stristr直接进行判断.也可以通过ex ...
- 判断字符串是否包含字母‘k’或者‘K’
判断字符串是否包含字母‘k’或者‘K’ public bool IsIncludeK(string temp) { temp = temp.ToLower(); if (temp.Contains(' ...
- js判断字符串是否包含指定的字符
判断字符串是否包含指定字符是很常用的功能,比如说,注册时用户名限制不能输入"管理员",或者需要js判断url跳转链接是否包含某个关键词等-- <!DOCTYPE html&g ...
- SQL中判断字符串中包含字符的方法
通过2个函数CHARINDEX和PATINDEX以及通配符的灵活使用 函数:CHARINDEX和PATINDEX CHARINDEX:查某字符(串)是否包含在其他字符串中,返回字符串中指定表达式的起始 ...
- 【功能代码】---3 JS判断字符串是否包含某个字符串
JS判断字符串是否包含某个字符串 var str ="abc"; if(str.indexOf("bc")>-1){ alert('str中包含bc字符串 ...
- Java:判断字符串中包含某字符的个数
Java:判断字符串中包含某字符的个数 JAVA中查询一个词在内容中出现的次数: public int getCount(String str,String key){ if(str == null ...
- Struts标签<s:if>判断字符串是否包含一个固定的值
Struts标签<s:if>判断字符串是否包含一个固定的值:1.如果比较对象是字符串: <s:if test="str.contains('判断是否包含的字符串')&quo ...
- C/C++判断字符串是否包含某个字符串
C风格 #include <iostream> #include <string> #include <cstring> using namespace std; ...
随机推荐
- javax.Swing 使用GridBagLayout的程序栗子
摘自https://zhidao.baidu.com/question/110748776.html javax.Swing 使用GridBagLayout的程序栗子 总共两个文件,第一个是启动文件, ...
- Android专项面试训练题(一)
1.下面不可以退出Activity的是?(D) A.finish() B.抛异常强制退出 C.System.exit(0) D.onStop() 解析: A, finish() 方法就是退出activ ...
- Python常用模块(time, datetime, random, os, sys, hashlib)
time模块 在Python中,通常有这几种方式来表示时间: 时间戳(timestamp) : 通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.我们运 ...
- java打印菱形的简单方法
代码: public class Diamond { /** * 测试main方法 */ public static void main(String[] args) { printDiamond(1 ...
- Trie树|字典树(字符串排序)
有时,我们会碰到对字符串的排序,若采用一些经典的排序算法,则时间复杂度一般为O(n*lgn),但若采用Trie树,则时间复杂度仅为O(n). Trie树又名字典树,从字面意思即可理解,这种树的结构像英 ...
- 【DataStructure】Some useful methods about linkedList(二)
Method 1: Add one list into the other list. For example, if list1is {22, 33, 44, 55} and list2 is { ...
- SVN中tag branch trunk用法详解
SVN中tag branch trunk用法详解 2010-05-24 18:32 佚名 字号:T | T 本文向大家简单介绍一下SVN中tag branch trunk用法,SVN中tag bran ...
- 【并查集专题】【HDU】
PS:做到第四题才发现 2,3题的路径压缩等于没写 How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- 64bit ubuntu14.04编译PlatinumKit出现的arm-linux-androideabi-g++: not found错误解决方法
编译命令:scons target=arm-android-linux build_config=Release 出现错误: scons: Reading SConscript files ...** ...
- MongoDB安装说明
1.去官网(https://www.mongodb.org/dr/fastdl.mongodb.org/win32/mongodb-win32-x86_64-3.2.3-signed.msi/down ...