# Written test # string matching regular expression C ++

Source: Internet
Author: User

Questions that students need to write on the computer when they go to Shanghai Tongji Urban Planning and Design Institute

 

Write a function bool fun (char * t, char * s)
T is the template string, where * represents 1 to n arbitrary characters, and s is the matching string. True is returned for matching, whereas false is returned.
For example:
AB * and ABC match
AB * And ACD do not match
A * BC and adebc match
A * BC and adebce do not match
A * BC and a ** BC match
B * de * C and bfddsdessscc match

 

Recursion is used. The following code is modified after I search for a reference on the network. The example is given after the test is passed.

# Include "stdafx. H" <br/> int ismatch (const char * s, const char * P) <br/>{< br/> If (! S &&! P) <br/>{< br/> return 1; <br/>}< br/> else if (! S |! P) <br/>{< br/> return 0; <br/>}< br/> while (* s! = '/0' & * P! = '/0') <br/>{< br/> If (* s = * P) & (* s! = '*') <Br/>{< br/> + + S; <br/> + P; <br/> continue; <br/>}< br/> else if (* P = '*') <br/> {<br/>/* escape all '*/<br/> while (* P = '*') <br/>{< br/> + + P; <br/>}< br/> If (* P = '/0 ') <br/>{< br/> return 1; <br/>}< br/> while (* s! = '/0') <br/>{< br/> If (* s = * P & ismatch (S + 1, p + 1 )) <br/>{< br/> return 1; <br/>}< br/> + + S; <br/>}< br/> else <br/> {<br/> return 0; <br/>}< br/> while (* P = '*') <br/>{< br/> + P; <br/>}< br/> If (* P = '/0') & (* s ='/0 ')) <br/>{< br/> return 1; <br/>}< br/> return 0; <br/>}< br/> int _ tmain (INT argc, _ tchar * argv []) <br/>{< br/> const char * STR = "ABC"; <br/> const char * Pattern = "AB *"; </P> <p> If (ismatch (STR, pattern) <br/>{< br/> fprintf (stdout, "Match OK! /N "); <br/>}< br/> else <br/> {<br/> fprintf (stdout," Match failed! /N "); <br/>}< br/> return 0; <br/>}

 

Previously I did not think of recursion, but it was not well written. The last example was not tested.

# Include "stdafx. H "<br/> # include <iostream> <br/> using namespace STD; <br/> bool fun (char * t, char * s) <br/>{< br/> bool bsingle = false; <br/> while (* t! = '/0') & (* s! = '/0') <br/>{< br/> while (* t = * s) & (* t! = '/0') <br/>{< br/> ++ t; <br/> ++ S; <br/>}< br/> If (* t = '/0') & (* s! = '/0') <br/>{< br/> return false; <br/>}< br/> while (* t = '*') <br/>{< br/> + + T; <br/>}< br/> while (* t! = * S) <br/>{< br/> ++ S; <br/>}< br/> If (* t = '/0') <br/>{< br/> bsingle = true; <br/>}< br/> return bsingle; <br/>}</P> <p> int _ tmain (INT argc, _ tchar * argv []) <br/>{< br/> char * t = "AB *"; <br/> char * s = "ABC "; <br/> bool btemp = fun (t, s); <br/> return 0; <br/>} 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.