在父字符串中查找子字符串

在父字符串中查找子字符串(指针控制,也可选择标控制)

#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
char* StrStr(char* source, char* dest)
{
	assert(source&&dest);
	if (strlen(source) < strlen(dest))
		return NULL;
	char* newSrc = NULL;
	char* newDest = dest;
	while (*source)
	{
		newSrc = source;
		while (*source&&*dest&&*source == *dest)
		{
			source++;
			dest++;
		}
		if (*dest == ‘\0‘)
		{
			return newSrc;
		}
		dest = newDest;
		source = newSrc + 1;
	}
}
void Test1()
{
	char* src = "abcbcdef";
	char* dest = "bcd";
	cout << StrStr(src, dest) << endl;
}

指针追踪截图

时间: 2024-08-02 11:02:27

在父字符串中查找子字符串的相关文章

40 python 正则表达式 match方法匹配字符串 使用search函数在一个字符串中查找子字

第一课: 使用match方法匹配字符串 # 正则表达式:使用match方法匹配字符串 ''' 正则表达式:是用来处理文本的,将一组类似的字符串进行抽象,形成的文本模式字符串 windows dir *.txt file1.txt file2.txt abc.txt test.doc a-file1.txt-b linux/mac ls 主要是学会正则表达式的5方面的方法 1. match:检测字符串是否匹配正则表达式 2. search:在一个长的字符串中搜索匹配正则表达式的子字符串 3. fi

模拟实现在一个字符串中查找一个字符串

在标准库中有一个函数strstr()用于在一个字符串中查找一个规定的字符串,这个函数可以模拟实现一下,代码如下: #include <stdio.h> #include <assert.h> char *my_strstr(const char str[],const char strstr[]) {  int i = 0,j = 0,k = 0;  assert(str != NULL);  assert(strstr != NULL);  for(i = 0;str[i] !=

在一个字符串中提取子字符串

一.提取字符串函数int substr(char dst[], char src[], int start, int len): 分析: 源字符串src[]用来提供字符串: 目的字符串dst[]用来接收子字符串: 从src[]的起始位置向后偏移,从start的位置开始最多复制len个字符,当src[]中字符数可以从start个字符开始复制len个字符,那么可以复制len个字符:当从start开始到字符串结束,即遇到'\0'时,小于len个字符,那么只需复制从start开始到字符串结束的字符串.

从字符串中提取子字符串

#include <assert.h> int substr(char dst[], char src[], int start, int len) {  int srcLen = strlen(src);  int left = 0;  assert(dst);  assert(src);  if (srcLen < start)  {   return -1;  }  while (start--)    {   src++;             //指针向后偏移start  }

KMP查找子字符串算法

举例说明: S:  ababcababa P:  ababa KMP算法之所以叫做KMP算法是因为这个算法是由三个人共同提出来的,就取三个人名字的首字母作为该算法的名字.其实KMP算法与BF算法的区别就在于KMP算法巧妙的消除了指针i的回溯问题,只需确定下次匹配j的位置即可,使得问题的复杂度由O(mn)下降到O(m+n).  在KMP算法中,为了确定在匹配不成功时,下次匹配时j的位置,引入了next[]数组,next[j]的值表示P[0...j-1]中最长后缀的长度等于相同字符序列的前缀.  对

*字符串-01. 在字符串中查找指定字符

1 /* 2 * Main.c 3 * D1-字符串-01. 在字符串中查找指定字符 4 * Created on: 2014年8月18日 5 * Author: Boomkeeper 6 *****部分通过****** 7 */ 8 9 #include <stdio.h> 10 11 int mysearch(char ch, const char str[], int length) { 12 13 int j, ret = -1; 14 15 for (j = 0; j < le

字符串中查找子串

使用C语言编写程序: 1.在字符串中查找一个指定的字符第一次出现的位置,并返回字符所在的位置,如果不存在则返回NULL 具体实现如下: char* strchr(char const *str, int ch) { char* st = (char*)str; while (st) { if (*st == ch) return st; st++; } return NULL; } 2.在字符串中查找一个指定的字符串第一次出现的位置,并返回字符所在的位置,如果不存在则返回NULL 具体实现如下:

在字符串中查找指定字符

输入一个字符串S,再输入一个字符c,要求在字符串S中查找字符c.如果找不到则输出“Not found”:若找到则输出字符串S中从c开始的所有字符. 输入格式: 输入在第1行中给出一个不超过80个字符长度的.以回车结束的非空字符串:在第2行中给出一个字符. 输出格式: 在一行中按照题目要求输出结果. 输入样例1: It is a black box b 输出样例1: black box 输入样例2: It is a black box B 输出样例2: Not found #include<std

【C语言】模拟实现strchr函数.即在一个字符串中查找一个字符第一次出现的位置并返回

//模拟实现strchr函数.即在一个字符串中查找一个字符第一次出现的位置并返回 #include <stdio.h> //#include <string.h> #include <assert.h> char* my_strchr(char *dst, char src) { assert(dst); while (*dst != '\0') { if (*dst == src) return dst; dst++; } return 0; } int main()