473,228 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,228 software developers and data experts.

Pattern matching

21
I write this program for pattern-matching,but it gives wrong result:
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<conio.h>
  3. #include<string.h>
  4. using namespace std;
  5. main()
  6. {
  7.     char text[100],pat[10];
  8.     cout<<"text:";cin>>text;
  9.     cout<<" pattern:";cin>>pat;
  10.     int pos=0,i=0,j=0;
  11.     while((i<=strlen(pat))&&(j<=strlen(text)))
  12.     {
  13.         if(pat[i]==text[j])
  14.         {
  15.             i++;
  16.             j++;
  17.         }
  18.         else
  19.         {
  20.             pos++;
  21.             j=pos;
  22.             i=0;
  23.         }
  24.  
  25.     }
  26.     if(i>strlen(pat))
  27.         cout<<"Matching occurs from"<<pos+1;
  28.     else
  29.         cout<<"No matching ";
  30. }
  31.  
For example,I enter
text:asm
pattern:a
result:No matching!
I think the algo is right,but why it gives such wrong result??
Jun 9 '07 #1
1 2735
JosAH
11,448 Expert 8TB
I write this program for pattern-matching,but it gives wrong result:
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<conio.h>
  3. #include<string.h>
  4. using namespace std;
  5. main()
  6. {
  7.     char text[100],pat[10];
  8.     cout<<"text:";cin>>text;
  9.     cout<<" pattern:";cin>>pat;
  10.     int pos=0,i=0,j=0;
  11.     while((i<=strlen(pat))&&(j<=strlen(text)))
  12.     {
  13.         if(pat[i]==text[j])
  14.         {
  15.             i++;
  16.             j++;
  17.         }
  18.         else
  19.         {
  20.             pos++;
  21.             j=pos;
  22.             i=0;
  23.         }
  24.  
  25.     }
  26.     if(i>strlen(pat))
  27.         cout<<"Matching occurs from"<<pos+1;
  28.     else
  29.         cout<<"No matching ";
  30. }
  31.  
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 ;-)
Jun 9 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
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...
176
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? ...
9
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...
1
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...
10
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,...
5
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...
9
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...
2
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...
5
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...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.