给定两个字符串str1和str2,查找str2在str1中出现的位置
如:str1为"ABCDLANCEXYZ",str2为"LANCE",则返回位置5。如果没有找到,返回-1。(起始位置从1开始)
// findSubStrPosition.c
#include <stdio.h>int findSubStrPosition(const char *pStr1, const char *pStr2)
{int len1 = strlen(pStr1);int len2 = strlen(pStr2);if (len1 < len2)return -1;int i = 0;for ( ; i < len1; i++){int j = 0;if (pStr1[i] != pStr2[j])continue;while(j < len2 & i+j < len1){j++;if (pStr1[i+j] != pStr2[j])break;}if (j == len2)return i+1 ; // start 1}return -1;
}int main()
{const char *str1 = "ABCDLANCEXYZ";const char *str2 = "LANCE";int position = 0;position = findSubStrPosition(str1, str2);printf("the substring position is %d\n", position);return 0;
}
// findSubStrPosition.cpp
#include <iostream>
#include <string>bool isSubstr(std::string str1, std::string str2)
{if (str1.length() < str2.length())return false;int index = 0;while(index < str2.length()){if(str2[index] == str1[index])index++;elsereturn false;}return true;
}int findSubStrPosition(std::string str1, std::string str2)
{int index = 0;while(index < (str1.length() - str2.length())){if (isSubstr(str1.substr(index), str2))return index + 1; // start 1elseindex++;}return -1;
}int main()
{std::string str1 = "ABCDLANCEXYZ";std::string str2 = "LANCE";int position = 0;position = findSubStrPosition(str1, str2);std::cout<< "the substring position is "<< position << std::endl;//std::cout<< "the substring position is "<< str1.find(str2) << std::endl;return 0;
}
给定两个字符串str1和str2,查找str2在str1中出现的位置相关推荐
- 2021-11-25:给定两个字符串s1和s2,返回在s1中有多少个子串等于s2。来自美团。
2021-11-25:给定两个字符串s1和s2,返回在s1中有多少个子串等于s2.来自美团. 答案2021-11-25: 改写kmp算法. next数组多求一位. 比如:str2 = aaaa, 那么 ...
- 给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和。
给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和. 不能直接将输入的字符串转换为整数形式. 思路:将字符串从最小位进行相加,最后形成的字符串倒置 class Solution {pu ...
- 给定两个字符串,确定其中一个字符串的字符重新排列后,能否变成另一个字符串...
1 /* 2 * 给定两个字符串,确定其中一个字符串的字符重新排列后,能否变成另一个字符串,其实也就是变位词问题 3 * 比如说 a='abc' b='acb'是可以通过a变成b的 4 * 思路; 5 ...
- 随笔-给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和
题目:给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和. 注意: num1 和num2 的长度都小于 5100. num1 和num2 都只包含数字 0-9. num1 和num2 ...
- LeetCode||有效的字母异位词(排列)--给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词
原题链接:https://leetcode-cn.com/problems/valid-anagram 题目描述: 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词. 示 ...
- 算法019:字符串轮转。给定两个字符串s1和s2,请编写代码检查s2是否为s1旋转而成(比如,waterbottle是erbottlewat旋转后的字符串)。
题目:字符串轮转.给定两个字符串s1和s2,请编写代码检查s2是否为s1旋转而成(比如,waterbottle是erbottlewat旋转后的字符串).示例1:输入:s1 = "waterb ...
- 给定两个字符串 s 和 t,它们只包含小写字母。 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。 请找出在 t 中被添加的字母。...
给定两个字符串 s 和 t,它们只包含小写字母.字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母.请找出在 t 中被添加的字母. 示例: 输入: s = "abcd" ...
- 找不同(给定两个字符串 s 和 t,它们只包含小写字母。 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。 请找出在 t 中被添加的字母。)
给定两个字符串 s 和 t,它们只包含小写字母. 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母. 请找出在 t 中被添加的字母. 示例: 输入: s = "abcd&quo ...
- JAVA算法:给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合(JAVA)
JAVA算法:给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合(JAVA) 给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合.. 当 n = 4 ...
最新文章
- 为什么在重庆比北京更容易迷路?Nature子刊:大脑GPS系统呈蜂窝状,弯路多了就“变形”...
- phpnow mysql字符集_使用PHPnow搭建本地PHP环境+创建MySQL数据库
- 【今日CV 视觉论文速览】26 Nov 2018
- 课程设计-三层架构ASP.NET作品分享网站(sql server数据库)
- 好玩的python代码聊天客户端_python socket实现客户端与服务器端对话
- bzoj 2821:作诗 分块
- Python redis 批量模糊删除keys
- 空指针、NULL指针、零指针
- db2v9/9.5高级应用开发_11月26日 l FloMASTER元器件开发及高级应用公开课
- 简述静态全局变量的概念 C++
- 2018注册测绘师各地报名时间和注意事项!莫错过哦!
- vmware服务器虚拟机重新安装系统教程,在VMware虚拟机装系统教程_vmware装系统_U盘工具_装系统教程_课课家...
- 微信小程序如何测试?
- sap 双计量单位_SAP系统里批次双计量单位的实现
- 假装写在了开头qwq
- 旁观OpenGL里的透视投影矩阵
- 【编程实践】什么是计算机编程?定义软件开发。What is Computer Programming? Defining Software Development.
- 服务号模板消息群发二代服务器,服务号模板消息群发
- 纯C语言写计算器界面
- autojs自定义下拉框