Find Unique ID and Domain Name of a Website from a string | C ++ program for the above approach ; Function to check if a character is alphabet or not ; Function to check if a character is a numeric or not ; Function to find ID and Domain name from a given string ; Stores ID and the domain names ; Stores the words of string S ; Stores the temporary word ; Traverse the string S ; If the current character is space ; Push the curr in words ; Update the curr ; Otherwise ; If curr is not empty ; If length of ss is 10 ; Traverse the string ss ; If j is in the range [ 5 , 9 ) ; If current character is not numeric ; Mark flag 1 ; Otherwise ; If current character is not alphabet ; Mark flag 1 ; If flag is false ; Assign ss to ID ; If substring formed by the first 3 character is " www " and last 3 character is " moc " ; Update the domain name ; Print ID and Domain ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool ischar ( char x ) { if ( ( x >= ' A ' && x <= ' Z ' ) || ( x >= ' a ' && x <= ' z ' ) ) { return 1 ; } return 0 ; } bool isnum ( char x ) { if ( x >= '0' && x <= '9' ) return 1 ; return 0 ; } void findIdandDomain ( string S , int N ) { string ID , Domain ; vector < string > words ; string curr = " " ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] == ' ▁ ' ) { words . push_back ( curr ) ; curr = " " ; } else { if ( S [ i ] == ' . ' ) { if ( i + 1 == N || ( i + 1 < N && S [ i + 1 ] == ' ▁ ' ) ) continue ; } curr += S [ i ] ; } } if ( curr . length ( ) ) words . push_back ( curr ) ; for ( string ss : words ) { if ( ss . size ( ) == 10 ) { bool flag = 0 ; for ( int j = 0 ; j <= 9 ; j ++ ) { if ( j >= 5 && j < 9 ) { if ( isnum ( ss [ j ] ) == 0 ) flag = 1 ; } else { if ( ischar ( ss [ j ] ) == 0 ) flag = 1 ; } } if ( ! flag ) { ID = ss ; } } if ( ss . substr ( 0 , 3 ) == " www " && ss . substr ( ss . length ( ) - 3 , 3 ) == " com " ) { Domain = ss . substr ( 4 , ss . size ( ) - 4 ) ; } } cout << " ID ▁ = ▁ " << ID << endl ; cout << " Domain ▁ = ▁ " << Domain ; } int main ( ) { string S = " We ▁ thank ▁ ABCDE1234F ▁ for ▁ visiting ▁ " " us ▁ and ▁ buying ▁ " " products ▁ item ▁ AMZrr @ ! k . ▁ For ▁ more ▁ " " offers , ▁ visit ▁ " " us ▁ at ▁ www . amazon . com " ; int N = S . length ( ) ; findIdandDomain ( S , N ) ; return 0 ; }