H面试程序(10): 字符串包含问题
题目描述:判断第二个字符串中的元素是否都能在第一个字符串中找到:
注意:和字符串的字串的问题有所区别,如第一个字符串为 abcdefg,第二个字符串为 aaabc,第二个字串还是包含于第一个字符串
方法1:将str2中的每一个字符取出,逐一和字符串str1中的字符进行比较
#include <stdio.h> int WhetherInclude(char* str1, char * str2)
{
int ia;
int ib = 0; while(str2[ib] !='\0') //遍历str2
{
ia = 0;
while(str1[ia]!='\0') //遍历str1
{
if(str1[ia] == str2[ib])
break;
else
ia++; } if(str1[ia] == '\0') //如果经过前面的一次遍历在str1中还是没有找到相同的字符就返回false
return 0; ib++; //增量 来控制外循环 if(str2[ib] == '\0') //如果查到str2的最后一个字符都没问题的话
return 1; }
}
int main()
{
char str1[] ="abcdefg";
char str2[] ="adgc"; int a = WhetherInclude(str1, str2); //1代表str1包含str2;
printf("%d\n",a);
return 0; }
方法2,使用hash数组
#include <stdio.h>
#include<malloc.h>
#include<memory.h>
#include<assert.h> int WhetherInclude(char* str1, char * str2) //只能判断str2的字母是不是都在str1中,和子串的问题不一样
{
char * phash = (char *)malloc(256*sizeof(char)); //进行一些初始化的工作
assert(phash);
memset(phash, 0, 256); int i = 0;
while(str1[i-1] != '\0') //将str1在phash数组中映射一遍
{
phash[str1[i-1]] = 1;
i++;
} int j = 0;
while(str2[j-1] != '\0') //将str2和str映射后的表再映射一遍,发现为0的位即不包含
{
if(phash[str2[j-1]] == 0)
return 0;
j++;
}
return 1; }
int main()
{
char str1[] ="abcdefg";
char str2[] ="aaagc"; int a = WhetherInclude(str1, str2); //1代表str1包含str2;
if(a = 0)
printf("不是所有字符串str2中的字符在str1中都能找到!!!\n");
else
printf("所有字符串str2中的字符在str1中都能找到\n"); return 0; }
H面试程序(10): 字符串包含问题的更多相关文章
- H面试程序(11): 判断字符串是否包含子串问题
题目描述: 如字符串str1为''abcdef''' 字符串str2为'' bc''; 则字符串str1中含有 ...
- H面试程序(12): 输出字符串中第一个只出现一次的字母
题目描述: 若字符串str为'' sbdddsbfc'',则输出 f; 若字符串str为''aabbccdd'',则输出:字符串str中的字符都出现两次以上 #include <stdio.h& ...
- H面试程序(28):字符串处理转换
//2 字符串处理转换 //问题描述: //在给定字符串中找出单词( “单词”由大写字母和小写字母字符构成, //其他非字母字符视为单词的间隔,如空格.问号.数字等等:另外单个字母不算单词): //找 ...
- H面试程序(27):字串转换
//1 字串转换 //问题描述: //将输入的字符串(字符串仅包含小写字母‘a’到‘z’),按照如下规则,循环转换后输出:a->b,b->c,…,y->z,z->a: //若输 ...
- H面试程序(16): 简单选择排序
#include<stdio.h> #include<assert.h> void display(int * a, int n) { assert(a); for(int i ...
- H面试程序(4):翻转句子中单词的顺序 .
题目:输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变. 句子中单词以空格符隔开.为简单起见,标点符号和普通字母一样处理. 例如输入“I am a student.”,则输出“stude ...
- H面试程序(29):求最大递增数
要求:求最大递增数 如:1231123451 输出12345 #include<stdio.h> #include<assert.h> void find(char *s) { ...
- H面试程序(15): 冒泡排序法
#include<stdio.h> #include<assert.h> void display(int * a, int n) { for(int i = 0; i < ...
- H面试程序(1)编写一个函数,要求输入年月日时分秒,输出该年月日时分秒的 下一秒
编写一个函数,要求输入年月日时分秒,输出该年月日时分秒的下一秒. 如输入 2004 年 12 月 31 日 23 时 59 分 59 秒,则输出 2005年 1 月 1 日 0 时 0 分 0 秒. ...
随机推荐
- 纯 CSS 创建各种不同的图形形状
使用代码 矩形 .rectangle { width: 250px; height: 150px; background-color: #6DC75F; } <div></div&g ...
- Lesson 5: Typography in Product Design
Lesson 5: Typography in Product Design Article 1: Interactive Guide to Blog Typography 布局(Layout) 用空 ...
- 关于char与varchar,varchar2的区别
http://zhidao.baidu.com/question/220360696.html?qbl=relate_question_0&word=char%BA%CDvarchar2%B5 ...
- E10后,导致VS2010调试时报错“未能将脚本调试器附加到计算机..."
以管理员身份打开CMD,运行:regsvr32.exe "%ProgramFiles(x86)%\Common Files\Microsoft Shared\VS7Debug\msdbg2. ...
- 将图片转换为Base64
string Imagefilename 硬盘路径 protected string ImgToBase64String(string Imagefilename) { try { Bitmap ...
- CentOS 7 之安装X Window System
网上说的直接 yum groupinstall "Desktop"经实验,在我的机器上是无效的.我使用的是: yum groupinstall 'GNOME Desktop' 才o ...
- 基于.NET MVC的高性能IOC插件化架构(二)之插件加载原理
上一篇博文简单介绍了下插件化的代码组成部分:http://www.cnblogs.com/gengzhe/p/4390932.html,源码地址:https://github.com/luohuazh ...
- ASP.NET Web.Config 读资料 (学习笔记)
refer : http://www.cnblogs.com/fish-li/archive/2011/12/18/2292037.html 上面这篇写很好了. 在做项目时,我们经常会遇到一些资料,我 ...
- hdu 4535 吉哥系列故事——礼尚往来
http://acm.hdu.edu.cn/showproblem.php?pid=4535 错排公式:a[i]=(i-1)*(a[i-2]+a[i-1]): #include <cstdio& ...
- @Valid springMVC bean校验不起作用及如何统一处理校验
SpringMVC 使用JSR-303进行校验 @Valid 使用注解 一.准备校验时使用的JAR validation-api-1.0.0.GA.jar:JDK的接口: hibernate-vali ...