I write this program for pattern-matching,but it gives wrong result: -
#include<iostream>
-
#include<conio.h>
-
#include<string.h>
-
using namespace std;
-
main()
-
{
-
char text[100],pat[10];
-
cout<<"text:";cin>>text;
-
cout<<" pattern:";cin>>pat;
-
int pos=0,i=0,j=0;
-
while((i<=strlen(pat))&&(j<=strlen(text)))
-
{
-
if(pat[i]==text[j])
-
{
-
i++;
-
j++;
-
}
-
else
-
{
-
pos++;
-
j=pos;
-
i=0;
-
}
-
-
}
-
if(i>strlen(pat))
-
cout<<"Matching occurs from"<<pos+1;
-
else
-
cout<<"No matching ";
-
}
-
For example,I enter
text:asm
pattern:a
result:No matching!
I think the algo is right,but why it gives such wrong result??
1 2770 JosAH 11,448
Recognized Expert MVP
I write this program for pattern-matching,but it gives wrong result: -
#include<iostream>
-
#include<conio.h>
-
#include<string.h>
-
using namespace std;
-
main()
-
{
-
char text[100],pat[10];
-
cout<<"text:";cin>>text;
-
cout<<" pattern:";cin>>pat;
-
int pos=0,i=0,j=0;
-
while((i<=strlen(pat))&&(j<=strlen(text)))
-
{
-
if(pat[i]==text[j])
-
{
-
i++;
-
j++;
-
}
-
else
-
{
-
pos++;
-
j=pos;
-
i=0;
-
}
-
-
}
-
if(i>strlen(pat))
-
cout<<"Matching occurs from"<<pos+1;
-
else
-
cout<<"No matching ";
-
}
-
For example,I enter
text:asm
pattern:a
result:No matching!
I think the algo is right,but why it gives such wrong result??
Congratulations for hitting that (in)famous OBOE (Off By One Error). Check the
conditions in the while clause: change both '<=' to '<' and you're in busyness.
Also adjust the condition in that last if clause accordingly.
kind regards,
Jos (<--- been bitten by the OBOE too many times ;-)
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: gsv2com |
last post by:
One of my weaknesses has always been pattern matching. Something I
definitely need to study up on and maybe you guys can give me a pointer
here.
I'm looking to remove all of this code and just use pattern matching to
determine if the proper amount of numeric characters has been met. Here is
the function I've already done. Any help you can give in a pattern matching
solution would be much appreciated and very educational.
|
by: Thomas Reichelt |
last post by:
Moin,
short question: is there any language combining the syntax, flexibility and
great programming experience of Python with static typing? Is there a
project to add static typing to Python?
Thank you,
--
greetz tom
|
by: Xah Lee |
last post by:
# -*- coding: utf-8 -*-
# Python
# Matching string patterns
#
# Sometimes you want to know if a string is of
# particular pattern. Let's say in your website
# you have converted all images files from gif
# format to png format. Now you need to change the
# html code to use the .png files. So, essentially
|
by: Henry |
last post by:
I have a table that stores a list of zip codes using a varchar column type,
and I need to perform some string prefix pattern matching search. Let's say
that I have the columns:
94000-1235
94001
94100
If I run a pattern matching search for the string "940", then I should get
the top two rows of data back. If I run a pattern matching search for the
string "94", then I should get all the three rows of data back.
|
by: bpontius |
last post by:
The GES Algorithm
A Surprisingly Simple Algorithm for Parallel Pattern Matching
"Partially because the best algorithms presented in the literature
are difficult to understand and to implement, knowledge of fast and
practical algorithms is not commonplace."
Hume and Sunday, "Fast String Searching", Software - Practice
and Experience, Vol. 21 # 11, pp 1221-48
| |
by: olaufr |
last post by:
Hi,
I'd need to perform simple pattern matching within a string using a
list of possible patterns. For example, I want to know if the substring
starting at position n matches any of the string I have a list, as
below:
sentence = "the color is $red"
patterns =
pos = sentence.find($)
|
by: Jim Lewis |
last post by:
Anyone have experience with string pattern matching?
I need a fast way to match variables to strings. Example:
string - variables
============
abcaaab - xyz
abca - xy
eeabcac - vxw
x matches abc
|
by: Ole Nielsby |
last post by:
First, bear with my xpost. This goes to
comp.lang.c++
comp.lang.functional
with follow-up to comp.lang.c++
- I want to discuss an aspect of using C++ to implement a
functional language, and I'd like the attention of fp as well
as C++ gurus if available.
The language I'm implementing - PILS - is dynamically
|
by: pramodkh |
last post by:
Hi All
I am trying to match a pattern in a file and insert a line. If the pattern matches then insert a line before the matching pattern line.
for example,
I have the following content in a file:
//This is my source file
//this is where i want to insert a line
class Class1
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |