codeparrot/xlcost-text-to-code huggingface dataset:
Palindrome Partitioning | DP | Dynamic Programming Solution for Palindrome Partitioning Problem ; Returns the minimum number of cuts needed to partition a string such that every part is a palindrome ; Get the length of the string ; Create two arrays to build the solution in bottom up manner C [ i ] [ j ] = Minimum number of cuts needed for palindrome partitioning of substring str [ i . . j ] P [ i ] [ j ] = true if substring str [ i . . j ] is palindrome , else false Note that C [ i ] [ j ] is 0 if P [ i ] [ j ] is true ; Every substring of length 1 is a palindrome ; L is substring length . Build the solution in bottom up manner by considering all substrings of length starting from 2 to n . The loop structure is same as Matrix Chain Multiplication problem ( See https : www . geeksforgeeks . org / matrix - chain - multiplication - dp - 8 / ) ; For substring of length L , set different possible starting indexes ; Set ending index ; If L is 2 , then we just need to compare two characters . Else need to check two corner characters and value of P [ i + 1 ] [ j - 1 ] ; IF str [ i . . j ] is palindrome , then C [ i ] [ j ] is 0 ; Make a cut at every possible location starting from i to j , and get the minimum cost cut . ; Return the min cut value for complete string . i . e . , str [ 0. . n - 1 ] ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minPalPartion ( string str ) { int n = str . length ( ) ; int C [ n ] [ n ] ; bool P [ n ] [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { P [ i ] [ i ] = true ; C [ i ] [ i ] = 0 ; } for ( int L = 2 ; L <= n ; L ++ ) { for ( int i = 0 ; i < n - L + 1 ; i ++ ) { int j = i + L - 1 ; if ( L == 2 ) P [ i ] [ j ] = ( str [ i ] == str [ j ] ) ; else P [ i ] [ j ] = ( str [ i ] == str [ j ] ) && P [ i + 1 ] [ j - 1 ] ; if ( P [ i ] [ j ] == true ) C [ i ] [ j ] = 0 ; else { C [ i ] [ j ] = INT_MAX ; for ( int k = i ; k <= j - 1 ; k ++ ) C [ i ] [ j ] = min ( C [ i ] [ j ] , C [ i ] [ k ] + C [ k + 1 ] [ j ] + 1 ) ; } } } return C [ 0 ] [ n - 1 ] ; } int main ( ) { string str = " ababbbabbababa " ; cout << " Min ▁ cuts ▁ needed ▁ for ▁ Palindrome " " ▁ Partitioning ▁ is ▁ " << minPalPartion ( str ) ; return 0 ; }
Count subtrees that sum up to a given value x only using single recursive function | C ++ implementation to count subtress that sum up to a given value x ; structure of a node of binary tree ; function to get a new node ; allocate space ; put in the data ; function to count subtress that sum up to a given value x ; if tree is empty ; sum of nodes in the left subtree ; sum of nodes in the right subtree ; sum of nodes in the subtree rooted with ' root - > data ' ; if true ; return subtree 's nodes sum ; utility function to count subtress that sum up to a given value x ; if tree is empty ; sum of nodes in the left subtree ; sum of nodes in the right subtree ; if tree 's nodes sum == x ; required count of subtrees ; Driver program to test above ; binary tree creation 5 / \ - 10 3 / \ / \ 9 8 - 4 7
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * left , * right ; } ; Node * getNode ( int data ) { Node * newNode = ( Node * ) malloc ( sizeof ( Node ) ) ; newNode -> data = data ; newNode -> left = newNode -> right = NULL ; return newNode ; } int countSubtreesWithSumX ( Node * root , int & count , int x ) { if ( ! root ) return 0 ; int ls = countSubtreesWithSumX ( root -> left , count , x ) ; int rs = countSubtreesWithSumX ( root -> right , count , x ) ; int sum = ls + rs + root -> data ; if ( sum == x ) count ++ ; return sum ; } int countSubtreesWithSumXUtil ( Node * root , int x ) { if ( ! root ) return 0 ; int count = 0 ; int ls = countSubtreesWithSumX ( root -> left , count , x ) ; int rs = countSubtreesWithSumX ( root -> right , count , x ) ; if ( ( ls + rs + root -> data ) == x ) count ++ ; return count ; } int main ( ) { Node * root = getNode ( 5 ) ; root -> left = getNode ( -10 ) ; root -> right = getNode ( 3 ) ; root -> left -> left = getNode ( 9 ) ; root -> left -> right = getNode ( 8 ) ; root -> right -> left = getNode ( -4 ) ; root -> right -> right = getNode ( 7 ) ; int x = 7 ; cout << " Count ▁ = ▁ " << countSubtreesWithSumXUtil ( root , x ) ; return 0 ; }
Minimum replacements required to make given Matrix palindromic | C ++ program for the above approach ; Function to count minimum changes to make the matrix palindromic ; Rows in the matrix ; Columns in the matrix ; Traverse the given matrix ; Store the frequency of the four cells ; Iterate over the map ; Min changes to do to make all ; Four elements equal ; Make the middle row palindromic ; Make the middle column palindromic ; Print minimum changes ; Driver Code ; Given matrix ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minchanges ( vector < vector < int > > mat ) { int N = mat . size ( ) ; int M = mat [ 0 ] . size ( ) ; int i , j , ans = 0 , x ; map < int , int > mp ; for ( i = 0 ; i < N / 2 ; i ++ ) { for ( j = 0 ; j < M / 2 ; j ++ ) { mp [ mat [ i ] [ M - 1 - j ] ] ++ ; mp [ mat [ i ] [ j ] ] ++ ; mp [ mat [ N - 1 - i ] [ M - 1 - j ] ] ++ ; mp [ mat [ N - 1 - i ] [ j ] ] ++ ; x = 0 ; for ( auto it = mp . begin ( ) ; it != mp . end ( ) ; it ++ ) { x = max ( x , it -> second ) ; } ans = ans + 4 - x ; mp . clear ( ) ; } } if ( N % 2 == 1 ) { for ( i = 0 ; i < M / 2 ; i ++ ) { if ( mat [ N / 2 ] [ i ] != mat [ N / 2 ] [ M - 1 - i ] ) ans ++ ; } } if ( M % 2 == 1 ) { for ( i = 0 ; i < N / 2 ; i ++ ) { if ( mat [ i ] [ M / 2 ] != mat [ N - 1 - i ] [ M / 2 ] ) ans ++ ; } } cout << ans ; } int main ( ) { vector < vector < int > > mat { { 1 , 2 , 3 } , { 4 , 5 , 3 } , { 1 , 2 , 1 } } ; minchanges ( mat ) ; }
Check if a number starts with another number or not | C ++ program for the above approach ; Function to check if B is a prefix of A or not ; Convert numbers into strings ; Check if s2 is a prefix of s1 or not using starts_with ( ) function ; If result is true , print " Yes " ; Driver Code ; Given numbers ; Function Call
#include <bits/stdc++.h> NEW_LINE #include <boost/algorithm/string.hpp> NEW_LINE using namespace std ; void checkprefix ( int A , int B ) { string s1 = to_string ( A ) ; string s2 = to_string ( B ) ; bool result ; result = boost :: algorithm :: starts_with ( s1 , s2 ) ; if ( result ) { cout << " Yes " ; } else { cout << " No " ; } } int main ( ) { int A = 12345 , B = 12 ; checkprefix ( A , B ) ; return 0 ; }
Check three or more consecutive identical characters or numbers | C ++ program to check three or more consecutive identical characters or numbers using Regular Expression ; Function to check three or more consecutive identical characters or numbers . ; Regex to check valid three or more consecutive identical characters or numbers . ; If the three or more consecutive identical characters or numbers is empty return false ; Return true if the three or more consecutive identical characters or numbers matched the ReGex ; Driver Code ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 : ; Test Case 5 :
#include <iostream> NEW_LINE #include <regex> NEW_LINE using namespace std ; bool isIdentical ( string str ) { const regex pattern ( " \\ b ( [ a - zA - Z0-9 ] ) \\ 1 \\ 1 + \\ b " ) ; if ( str . empty ( ) ) { return false ; } if ( regex_match ( str , pattern ) ) { return true ; } else { return false ; } } int main ( ) { string str1 = " aaa " ; cout << isIdentical ( str1 ) << endl ; string str2 = "11111" ; cout << isIdentical ( str2 ) << endl ; string str3 = " aaab " ; cout << isIdentical ( str3 ) << endl ; string str4 = " abc " ; cout << isIdentical ( str4 ) << endl ; string str5 = " aa " ; cout << isIdentical ( str5 ) << endl ; return 0 ; }
How to validate MAC address using Regular Expression | C ++ program to validate the MAC address using Regular Expression ; Function to validate the MAC address ; Regex to check valid MAC address ; If the MAC address is empty return false ; Return true if the MAC address matched the ReGex ; Driver Code ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 : ; Test Case 5 :
#include <iostream> NEW_LINE #include <regex> NEW_LINE using namespace std ; bool isValidMACAddress ( string str ) { const regex pattern ( " ^ ( [0-9A - Fa - f ] { 2 } [ : - ] ) { 5 } " " ( [0-9A - Fa - f ] { 2 } ) | ( [0-9a - " " fA - F ] { 4 } \\ . [0-9a - fA - F ] " " { 4 } \\ . [0-9a - fA - F ] { 4 } ) $ " ) ; if ( str . empty ( ) ) { return false ; } if ( regex_match ( str , pattern ) ) { return true ; } else { return false ; } } int main ( ) { string str1 = "01-23-45-67-89 - AB " ; cout << isValidMACAddress ( str1 ) << endl ; string str2 = "01:23:45:67:89 : AB " ; cout << isValidMACAddress ( str2 ) << endl ; string str3 = "0123.4567.89AB " ; cout << isValidMACAddress ( str3 ) << endl ; string str4 = "01-23-45-67-89 - AH " ; cout << isValidMACAddress ( str4 ) << endl ; string str5 = "01-23-45-67 - AH " ; cout << isValidMACAddress ( str5 ) << endl ; return 0 ; }
How to validate GUID ( Globally Unique Identifier ) using Regular Expression | C ++ program to validate the GUID ( Globally Unique Identifier ) using Regular Expression ; Function to validate the GUID ( Globally Unique Identifier ) . ; Regex to check valid GUID ( Globally Unique Identifier ) . ; If the GUID ( Globally Unique Identifier ) is empty return false ; Return true if the GUID ( Globally Unique Identifier ) matched the ReGex ; Driver Code ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 :
#include <iostream> NEW_LINE #include <regex> NEW_LINE using namespace std ; bool isValidGUID ( string str ) { const regex pattern ( " ^ [ { ] ? [ 0-9a - fA - F ] { 8 } - ( [ 0-9a - fA - F ] { 4 } - ) { 3 } [ 0-9a - fA - F ] { 12 } [ } ] ? $ " ) ; if ( str . empty ( ) ) { return false ; } if ( regex_match ( str , pattern ) ) { return true ; } else { return false ; } } int main ( ) { string str1 = "123e4567 - e89b - 12d3 - a456-9AC7CBDCEE52" ; cout << isValidGUID ( str1 ) << endl ; string str2 = " { 123e4567 - e89b - 12d3 - a456-9AC7CBDCEE52 } " ; cout << isValidGUID ( str2 ) << endl ; string str3 = "123e4567 - h89b - 12d3 - a456-9AC7CBDCEE52" ; cout << isValidGUID ( str3 ) << endl ; string str4 = "123e4567 - h89b - 12d3 - a456" ; cout << isValidGUID ( str4 ) << endl ; return 0 ; }
How to validate Indian driving license number using Regular Expression | C ++ program to validate the Indian driving license number using Regular Expression ; Function to validate the Indian driving license number ; Regex to check valid Indian driving license number ; If the Indian driving license number is empty return false ; Return true if the Indian driving license number matched the ReGex ; Driver Code ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 : ; Test Case 5 :
#include <iostream> NEW_LINE #include <regex> NEW_LINE using namespace std ; bool isValidLicenseNo ( string str ) { const regex pattern ( " ^ ( ( [ A - Z ] { 2 } [ 0-9 ] { 2 } ) ( ▁ " " ) | ( [ A - Z ] { 2 } - [0-9 ] { 2 } ) ) " " ( (19 ▁ 20 ) [ 0 - " "9 ] [ 0-9 ] ) [0-9 ] { 7 } $ " ) ; if ( str . empty ( ) ) { return false ; } if ( regex_match ( str , pattern ) ) { return true ; } else { return false ; } } int main ( ) { string str1 = " HR - 0619850034761" ; cout << isValidLicenseNo ( str1 ) << endl ; string str2 = " UP14 ▁ 20160034761" ; cout << isValidLicenseNo ( str2 ) << endl ; string str3 = "12HR - 37200602347" ; cout << isValidLicenseNo ( str3 ) << endl ; string str4 = " MH27 ▁ 30123476102" ; cout << isValidLicenseNo ( str4 ) << endl ; string str5 = " GJ - 2420180" ; cout << isValidLicenseNo ( str5 ) << endl ; return 0 ; }
Count subtrees that sum up to a given value x only using single recursive function | C ++ program to find if there is a subtree with given sum ; Structure of a node of binary tree ; Function to get a new node ; Allocate space ; Utility function to count subtress that sum up to a given value x ; Driver code ; binary tree creation 5 / \ - 10 3 / \ / \ 9 8 - 4 7
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * left , * right ; } ; Node * getNode ( int data ) { Node * newNode = ( Node * ) malloc ( sizeof ( Node ) ) ; newNode -> data = data ; newNode -> left = newNode -> right = NULL ; return newNode ; } int countSubtreesWithSumXUtil ( Node * root , int x ) { static int count = 0 ; static Node * ptr = root ; int l = 0 , r = 0 ; if ( root == NULL ) return 0 ; l += countSubtreesWithSumXUtil ( root -> left , x ) ; r += countSubtreesWithSumXUtil ( root -> right , x ) ; if ( l + r + root -> data == x ) count ++ ; if ( ptr != root ) return l + root -> data + r ; return count ; } int main ( ) { Node * root = getNode ( 5 ) ; root -> left = getNode ( -10 ) ; root -> right = getNode ( 3 ) ; root -> left -> left = getNode ( 9 ) ; root -> left -> right = getNode ( 8 ) ; root -> right -> left = getNode ( -4 ) ; root -> right -> right = getNode ( 7 ) ; int x = 7 ; cout << " Count ▁ = ▁ " << countSubtreesWithSumXUtil ( root , x ) ; return 0 ; }
How to validate Indian Passport number using Regular Expression | C ++ program to validate the passport number using Regular Expression ; Function to validate the passport number ; Regex to check valid passport number ; If the passport number is empty return false ; Return true if the passport number matched the ReGex ; Driver Code ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 : ; Test Case 5 :
#include <iostream> NEW_LINE #include <regex> NEW_LINE using namespace std ; bool isValidPassportNo ( string str ) { const regex pattern ( " ^ [ A - PR - WYa - pr - wy ] [1-9 ] " " \\ d \\ s ? \\ d { 4 } [ 1-9 ] $ " ) ; if ( str . empty ( ) ) { return false ; } if ( regex_match ( str , pattern ) ) { return true ; } else { return false ; } } int main ( ) { string str1 = " A21 ▁ 90457" ; cout << isValidPassportNo ( str1 ) << endl ; string str2 = " A0296457" ; cout << isValidPassportNo ( str2 ) << endl ; string str3 = " Q2096453" ; cout << isValidPassportNo ( str3 ) << endl ; string str4 = "12096457" ; cout << isValidPassportNo ( str4 ) << endl ; string str5 = " A209645704" ; cout << isValidPassportNo ( str5 ) << endl ; return 0 ; }
How to validate Visa Card number using Regular Expression | C ++ program to validate Visa Card number using Regular Expression ; Function to validate Visa Card number ; Regex to check valid Visa Card number ; If the Visa Card number is empty return false ; Return true if the Visa Card number matched the ReGex ; Driver Code ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 : ; Test Case 5 :
#include <iostream> NEW_LINE #include <regex> NEW_LINE using namespace std ; bool isValidVisaCardNo ( string str ) { const regex pattern ( " ^ 4[0-9 ] { 12 } ( ? : [0-9 ] { 3 } ) ? $ " ) ; if ( str . empty ( ) ) { return false ; } if ( regex_match ( str , pattern ) ) { return true ; } else { return false ; } } int main ( ) { string str1 = "4155279860457" ; cout << isValidVisaCardNo ( str1 ) << endl ; string str2 = "4155279860457201" ; cout << isValidVisaCardNo ( str2 ) << endl ; string str3 = "4155279" ; cout << isValidVisaCardNo ( str3 ) << endl ; string str4 = "6155279860457" ; cout << isValidVisaCardNo ( str4 ) << endl ; string str5 = "415a27 # # 60457" ; cout << isValidVisaCardNo ( str5 ) << endl ; return 0 ; }
How to validate MasterCard number using Regular Expression | C ++ program to validate Master Card number using Regular Expression ; Function to validate Master Card number ; Regex to check valid Master Card number ; If the Master Card number is empty return false ; Return true if the Master Card number matched the ReGex ; Driver Code ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 : ; Test Case 5 :
#include <iostream> NEW_LINE #include <regex> NEW_LINE using namespace std ; bool isValidMasterCardNo ( string str ) { const regex pattern ( " ^ 5[1-5 ] [ 0-9 ] { 14 } | ^ ( 222[1-9 ] ▁ 22[3-9 ] \\ d ▁ 2[3-6 ] \\ d { 2 } ▁ 27[0-1 ] \\ d ▁ 2720 ) [ 0-9 ] { 12 } $ " ) ; if ( str . empty ( ) ) { return false ; } if ( regex_match ( str , pattern ) ) { return true ; } else { return false ; } } int main ( ) { string str1 = "5114496353984312" ; cout << isValidMasterCardNo ( str1 ) << endl ; string str2 = "2720822463109651" ; cout << isValidMasterCardNo ( str2 ) << endl ; string str3 = "5582822410" ; cout << isValidMasterCardNo ( str3 ) << endl ; string str4 = "6082822463100051" ; cout << isValidMasterCardNo ( str4 ) << endl ; string str5 = "2221149a635 # # 843" ; cout << isValidMasterCardNo ( str5 ) << endl ; return 0 ; }
How to validate CVV number using Regular Expression | C ++ program to validate the CVV ( Card Verification Value ) number using Regular Expression ; Function to validate the CVV ( Card Verification Value ) number ; Regex to check valid CVV ( Card Verification Value ) number ; If the CVV ( Card Verification Value ) number is empty return false ; Return true if the CVV ( Card Verification Value ) number matched the ReGex ; Driver Code ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 :
#include <iostream> NEW_LINE #include <regex> NEW_LINE using namespace std ; bool isValidCVVNumber ( string str ) { const regex pattern ( " ^ [ 0-9 ] { 3,4 } $ " ) ; if ( str . empty ( ) ) { return false ; } if ( regex_match ( str , pattern ) ) { return true ; } else { return false ; } } int main ( ) { string str1 = "561" ; cout << isValidCVVNumber ( str1 ) << endl ; string str2 = "5061" ; cout << isValidCVVNumber ( str2 ) << endl ; string str3 = "50614" ; cout << isValidCVVNumber ( str3 ) << endl ; string str4 = "5a # 1" ; cout << isValidCVVNumber ( str4 ) << endl ; return 0 ; }
How to validate IFSC Code using Regular Expression | C ++ program to validate the IFSC ( Indian Financial System ) Code using Regular Expression ; Function to validate the IFSC ( Indian Financial System ) Code . ; Regex to check valid IFSC ( Indian Financial System ) Code . ; If the IFSC ( Indian Financial System ) Code is empty return false ; Return true if the IFSC ( Indian Financial System ) Code matched the ReGex ; Driver Code ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 :
#include <iostream> NEW_LINE #include <regex> NEW_LINE using namespace std ; bool isValidIFSCode ( string str ) { const regex pattern ( " ^ [ A - Z ] { 4 } 0 [ A - Z0-9 ] { 6 } $ " ) ; if ( str . empty ( ) ) { return false ; } if ( regex_match ( str , pattern ) ) { return true ; } else { return false ; } } int main ( ) { string str1 = " SBIN0125620" ; cout << boolalpha << isValidIFSCode ( str1 ) << endl ; string str2 = " SBIN0125" ; cout << boolalpha << isValidIFSCode ( str2 ) << endl ; string str3 = "1234SBIN012" ; cout << boolalpha << isValidIFSCode ( str3 ) << endl ; string str4 = " SBIN7125620" ; cout << boolalpha << isValidIFSCode ( str4 ) << endl ; return 0 ; }
How to validate GST ( Goods and Services Tax ) number using Regular Expression | C ++ program to validate the GST ( Goods and Services Tax ) number using Regular Expression ; Function to validate the GST ( Goods and Services Tax ) number ; Regex to check valid GST ( Goods and Services Tax ) number ; If the GST ( Goods and Services Tax ) number is empty return false ; Return true if the GST number matched the ReGex ; Driver Code ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 : ; Test Case 5 :
#include <iostream> NEW_LINE #include <regex> NEW_LINE using namespace std ; bool isValidGSTNo ( string str ) { const regex pattern ( " ^ [ 0-9 ] { 2 } [ A - Z ] { 5 } " " [ 0-9 ] { 4 } [ A - Z ] { 1 } [ " "1-9A - Z ] { 1 } Z [ 0-9A - Z ] { 1 } $ " ) ; if ( str . empty ( ) ) { return false ; } if ( regex_match ( str , pattern ) ) { return true ; } else { return false ; } } int main ( ) { string str1 = "06BZAHM6385P6Z2" ; cout << isValidGSTNo ( str1 ) << endl ; string str2 = "06BZAF67" ; cout << isValidGSTNo ( str2 ) << endl ; string str3 = " AZBZAHM6385P6Z2" ; cout << isValidGSTNo ( str3 ) << endl ; string str4 = "06BZ63AHM85P6Z2" ; cout << isValidGSTNo ( str4 ) << endl ; string str5 = "06BZAHM6385P6F2" ; cout << isValidGSTNo ( str5 ) << endl ; return 0 ; }
How to validate HTML tag using Regular Expression | C ++ program to validate the HTML tag using Regular Expression ; Function to validate the HTML tag . ; Regex to check valid HTML tag . ; If the HTML tag is empty return false ; Return true if the HTML tag matched the ReGex ; Driver Code ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 : ; Test Case 5 :
#include <iostream> NEW_LINE #include <regex> NEW_LINE using namespace std ; bool isValidHTMLTag ( string str ) { const regex pattern ( " < ( \" [ ^ \" ] * \" ▁ ' [ ^ ' ] * ' ▁ [ ^ ' \" > ] ) * > " ) ; if ( str . empty ( ) ) { return false ; } if ( regex_match ( str , pattern ) ) { return true ; } else { return false ; } } int main ( ) { string str1 = " < input ▁ value ▁ = ▁ ' > ' > " ; cout << isValidHTMLTag ( str1 ) << endl ; string str2 = " < br / > " ; cout << isValidHTMLTag ( str2 ) << endl ; string str3 = " br / > " ; cout << isValidHTMLTag ( str3 ) << endl ; string str4 = " < ' br / > " ; cout << isValidHTMLTag ( str4 ) << endl ; string str5 = " < input ▁ value ▁ = > ▁ > " ; cout << isValidHTMLTag ( str5 ) << endl ; return 0 ; }
How to validate a domain name using Regular Expression | C ++ program to validate the domain name using Regular Expression ; Function to validate the domain name . ; Regex to check valid domain name . ; If the domain name is empty return false ; Return true if the domain name matched the ReGex ; Driver Code ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 : ; Test Case 5 :
#include <iostream> NEW_LINE #include <regex> NEW_LINE using namespace std ; bool isValidDomain ( string str ) { const regex pattern ( " ^ ( ? ! - ) [ A - Za - z0-9 - ] + ( [ \\ - \\ . ] { 1 } [ a - z0-9 ] + ) * \\ . [ A - Za - z ] { 2,6 } $ " ) ; if ( str . empty ( ) ) { return false ; } if ( regex_match ( str , pattern ) ) { return true ; } else { return false ; } } int main ( ) { string str1 = " geeksforgeeks . org " ; cout << isValidDomain ( str1 ) << endl ; string str2 = " contribute . geeksforgeeks . org " ; cout << isValidDomain ( str2 ) << endl ; string str3 = " - geeksforgeeks . org " ; cout << isValidDomain ( str3 ) << endl ; string str4 = " geeksforgeeks . o " ; cout << isValidDomain ( str4 ) << endl ; string str5 = " . org " ; cout << isValidDomain ( str5 ) << endl ; return 0 ; }
How to validate SSN ( Social Security Number ) using Regular Expression | C ++ program to validate the SSN ( Social Security Number ) using Regular Expression ; Function to validate the SSN ( Social Security Number ) ; Regex to check valid SSN ( Social Security Number ) ; If the SSN ( Social Security Number ) is empty return false ; Return true if the SSN ( Social Security Number ) matched the ReGex ; Driver Code ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 :
#include <iostream> NEW_LINE #include <regex> NEW_LINE using namespace std ; bool isValidSSN ( string str ) { const regex pattern ( " ^ ( ? ! 666 ▁ 000 ▁ 9 \\ d { 2 } ) " " \\ d { 3 } - ( ? !00 ) " " \\ d { 2 } - ( ? !0 { 4 } ) \\ d { 4 } $ " ) ; if ( str . empty ( ) ) { return false ; } if ( regex_match ( str , pattern ) ) { return true ; } else { return false ; } } int main ( ) { string str1 = "856-45-6789" ; cout << isValidSSN ( str1 ) << endl ; string str2 = "000-45-6789" ; cout << isValidSSN ( str2 ) << endl ; string str3 = "856-452-6789" ; cout << isValidSSN ( str3 ) << endl ; string str4 = "856-45-0000" ; cout << isValidSSN ( str4 ) << endl ; return 0 ; }