Table of Contents
例如
下面的程序中使用的方法如下
示例
输出
Home Backend Development C++ Recursive function for substring search in C++

Recursive function for substring search in C++

Aug 31, 2023 am 10:29 AM
function recursion substring search

Recursive function for substring search in C++

给定两个字符串 Str 和 subStr 作为输入。目标是确定 subStr 中存在的文本是否作为子字符串存在于 Str 中。如果整个 X 在 Y 中至少出现一次,则字符串 X 称为 Y 的子串。 我们将使用递归方法来执行此操作。

例如

输入− Str = “tutorialspoint” subStr=”Point”

输出− 给定字符串不包含子字符串!

解释− 字符串 Point 不是教程点的子字符串

输入− Str = “globalization” subStr=”global”

输出− 给定字符串包含子字符串!

解释 - 字符串global是全球化的子字符串

下面的程序中使用的方法如下

在这种方法中,我们以递归方式检查subStr是否是Str的子字符串。递归的步骤为:-

  • 1.将两个字符串传递给递归函数,其中指针将指向两个字符串的当前字符位置

  • 如果字符串结束但模式还剩下更多字符,则返回 0,因为未找到模式我们到达了字符串的末尾。

  • 如果当前字符是模式中的最后一个字符,则在字符串中找到它,返回 1。

    li>
  • 如果两个当前字符相同,则将两个指针移动到下一个位置。

  • 如果两个当前字符不匹配,则将指针移动到下一个位置。将字符串移动到下一个位置。

  • 将输入字符串作为字符数组 Str 和 subStr。

  • 函数 match(char *str1, char *substr1) 接受两个子字符串,如果 substr1 和 str1 相同,则返回 1。

  • 两个指针都指向字符串中存在的字符,最初位于起始位置。

  • 如果 substr 为空,则返回 0。

  • 如果两个字符串都为空,则也返回 0。

  • 如果两个字符串都为空,则也返回 0。 >

  • 如果两个当前字符相等,则使用 match(str1 + 1, substr1 + 1) 递归检查下一个字符

  • 函数 checksubString (char *str2, char *substr2) 接受两个字符串,如果 str2 中存在 substr2,则返回 1。

  • 如果 str2 和 substr2 指向的当前字符相同,则检查是否连续的字符也可以使用 match() 函数进行匹配。如果返回 1,则返回 1。

  • 如果到达 str2 的末尾,则返回 0。

  • 否则递归检查str2 的下一个字符使用 checksubString(str2 + 1, substr2);

  • 如果所有条件都失败,则还使用递归检查 checksubString(str2 + 1, substr2);

  • 根据返回值打印结果。

示例

#include<iostream>
using namespace std;
int match(char *str1, char *substr1){
   if (*substr1 == &#39;\0&#39;){
      return 1;
   }
   if (*str1 == &#39;\0&#39;){
      if(*substr1 != &#39;\0&#39;){
         return 0;
      }
   }
   if (*str1 == *substr1){
      return match(str1 + 1, substr1 + 1);
   }
   return 0;
}
int checksubString(char *str2, char *substr2){
   if (*str2 == *substr2){
      if(match(str2, substr2)){
         return 1;
      }
   }
   if (*str2 == &#39;\0&#39;){
      return 0;
   }
   else{
      return checksubString(str2 + 1, substr2);
   }

   return checksubString(str2 + 1, substr2);
}
int main(){
   char Str[]="tutorialspoint";
   char subStr[]="point";

   if(checksubString(Str,subStr)==1){
      cout << "Given string contains substring!";
   }
   else{
      cout << "Given string does not contain substring!"; }
   return 0;
}
Copy after login

输出

如果我们运行上面的代码,它将生成以下输出

Given string contains substring!
Copy after login

The above is the detailed content of Recursive function for substring search in C++. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Tips for dynamically creating new functions in golang functions Tips for dynamically creating new functions in golang functions Apr 25, 2024 pm 02:39 PM

Go language provides two dynamic function creation technologies: closure and reflection. closures allow access to variables within the closure scope, and reflection can create new functions using the FuncOf function. These technologies are useful in customizing HTTP routers, implementing highly customizable systems, and building pluggable components.

Considerations for parameter order in C++ function naming Considerations for parameter order in C++ function naming Apr 24, 2024 pm 04:21 PM

In C++ function naming, it is crucial to consider parameter order to improve readability, reduce errors, and facilitate refactoring. Common parameter order conventions include: action-object, object-action, semantic meaning, and standard library compliance. The optimal order depends on the purpose of the function, parameter types, potential confusion, and language conventions.

How to write efficient and maintainable functions in Java? How to write efficient and maintainable functions in Java? Apr 24, 2024 am 11:33 AM

The key to writing efficient and maintainable Java functions is: keep it simple. Use meaningful naming. Handle special situations. Use appropriate visibility.

Detailed explanation of C++ function recursion: application of recursion in string processing Detailed explanation of C++ function recursion: application of recursion in string processing Apr 30, 2024 am 10:30 AM

A recursive function is a technique that calls itself repeatedly to solve a problem in string processing. It requires a termination condition to prevent infinite recursion. Recursion is widely used in operations such as string reversal and palindrome checking.

Complete collection of excel function formulas Complete collection of excel function formulas May 07, 2024 pm 12:04 PM

1. The SUM function is used to sum the numbers in a column or a group of cells, for example: =SUM(A1:J10). 2. The AVERAGE function is used to calculate the average of the numbers in a column or a group of cells, for example: =AVERAGE(A1:A10). 3. COUNT function, used to count the number of numbers or text in a column or a group of cells, for example: =COUNT(A1:A10) 4. IF function, used to make logical judgments based on specified conditions and return the corresponding result.

A beginner's guide to C++ recursion: Building foundations and developing intuition A beginner's guide to C++ recursion: Building foundations and developing intuition May 01, 2024 pm 05:36 PM

Recursion is a powerful technique that allows a function to call itself to solve a problem. In C++, a recursive function consists of two key elements: the base case (which determines when the recursion stops) and the recursive call (which breaks the problem into smaller sub-problems ). By understanding the basics and practicing practical examples such as factorial calculations, Fibonacci sequences, and binary tree traversals, you can build your recursive intuition and use it in your code with confidence.

C++ function recursion explained: alternatives to recursion C++ function recursion explained: alternatives to recursion May 01, 2024 pm 04:54 PM

Recursion is a technique where a function calls itself, but has the disadvantages of stack overflow and inefficiency. Alternatives include: tail-recursion optimization, where the compiler optimizes recursive calls into loops; iteration, which uses loops instead of recursion; and coroutines, which allow execution to be paused and resumed, simulating recursive behavior.

C++ Recursion Advanced: Understanding Tail Recursion Optimization and Its Application C++ Recursion Advanced: Understanding Tail Recursion Optimization and Its Application Apr 30, 2024 am 10:45 AM

Tail recursion optimization (TRO) improves the efficiency of certain recursive calls. It converts tail-recursive calls into jump instructions and saves the context state in registers instead of on the stack, thereby eliminating extra calls and return operations to the stack and improving algorithm efficiency. Using TRO, we can optimize tail recursive functions (such as factorial calculations). By replacing the tail recursive call with a goto statement, the compiler will convert the goto jump into TRO and optimize the execution of the recursive algorithm.

See all articles