当前位置:网站首页>String application -- string violent matching (implemented in C language)
String application -- string violent matching (implemented in C language)
2022-06-30 07:26:00 【Three stinky ginger】
String application — String matching (C Language implementation )
List of articles
Algorithmic thought – for instance
Just look at the picture :
【 Match success 】
① At the beginning :

② The first one can match , Pointer backward :

③ The second one can also match , And the substring has reached the end , Match over , The match is successful .
【 Matching failure 】
① The first step is the same as above : Both pointers point to the first character .

② The second step , Pointer backward :

In the second step, a mismatch is found , At this time , Need to update pointer i The location of , Because the first character has been compared , therefore i Should point to the 2 individual , The pointer j Go back to the beginning every time :

③ Because it doesn't match yet , therefore i Move all the way back , Until the end of the main string :

It has reached the end of the main string , Match over , But because of the mismatch , So the match failed .
Observe the law and you will find :
① If the characters match , be i++,j++;
② If the characters do not match , be i = i - j +1; j = 0 .
When the match ends , if j The value of is equal to its length , Then the match is successful , Otherwise the match fails .
Code implementation
#include<stdio.h>
#include<stdbool.h> // according to C99 standard ,C Language use bool Type needs to add this header file
#include <string.h>//strlen Function header file
bool ForcedMatch(char* MainStr, char* SubStr)
{
int i = 0;// Point to the main string
int j = 0;// Point to substring
while(i<strlen(MainStr)&&j<strlen(SubStr)){
if(MainStr[i]==SubStr[j]){
// If the current character matches
i++;
j++;
}
else{
// If it doesn't match
i = i - j + 1;// Main string adjustment position
j = 0;// Violent matching , Substrings always go back to the beginning
}
}
// It has been matched to the end
return j==strlen(SubStr)?true:false;
}
int main()
{
printf(ForcedMatch("hello","he")?" The match is successful !\n":" Matching failure !\n");
printf(ForcedMatch("hello","heo")?" The match is successful !\n":" Matching failure !\n");
return 0;
}
test

边栏推荐
- 踩坑记录:supervisor 日志返回信息:redis扩展未安装
- Dynamic memory management
- The maximum expression in Oracle database message list is 1000 error
- El input can only input numbers and has a decimal point. At most two digits can be reserved
- 我今年毕业,但我不知道我要做什么
- Matter protocol
- Basic knowledge of compiling learning records
- 记录开发过程中无法使用管理员身份修改系统文件问题
- 网络安全-VLAN和Tunk方法详解
- Introduction to go language pointer
猜你喜欢

Linux server installation redis

QT generate random number qrandomgenerator

Stm32g0 Tim interrupt use
![Experiment 1: comprehensive experiment [process on]](/img/19/6c6e18d7e1f042bfd3ee4832b78542.png)
Experiment 1: comprehensive experiment [process on]

Calculation and parameter quantity of neural network

The first up Master of station B paid to watch the video still came! Price "Persuading" netizens

踩坑记录:supervisor 日志返回信息:redis扩展未安装

Pit stepping record: Supervisor log return information: redis extension is not installed

Test enumeration types with STM32 platform running RT thread

Dynamic memory management
随机推荐
oracle数据库报列表中最大表达式为1000错误
Pool de Threads - langage C
1285_ Expand macros defined by AUTOSAR functions and variables with scripts to improve readability
Egret engine P2 physics engine (2) - Funny physical phenomenon of small balls hitting the ground
[implemented] server jar package startup script and shell script
年轻人搞副业有多疯狂:月薪3000,副业收入3W
Use of ecostruxure (3) creating composite function blocks
系统软件开发基础知识
Network security ARP protocol and defense
Cmake generate map file
Variable storage unit and pointer
LabVIEW程序代码更新缓慢
Keil plug-in Usage Summary
Calculation and parameter quantity of neural network
app quits unexpectedly
模拟接口没声明异常抛出异常
Cubemx completes STM32F103 dual serial port 485 transceiver transmission
Essence of signal slot macros signal and slot
Lt268 the most convenient TFT-LCD serial port screen chip in the whole network
Merge: extension click the El table table data to expand