Find Unique ID and Domain Name of a Website from a string | 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
function ischar ( x ) { if ( ( x >= ' ' && x <= ' ' ) || ( x >= ' ' && x <= ' ' ) ) { return true ; } return false ; } function isnum ( x ) { if ( x >= ' ' && x <= ' ' ) return true ; return false ; } function findIdandDomain ( S , N ) { let ID , Domain ; let words = [ ] ; let curr = " " ; for ( let i = 0 ; i < N ; i ++ ) { if ( S [ i ] == ' ' ) { words . push ( curr ) ; curr = " " ; } else { if ( S [ i ] == ' ' ) { if ( i + 1 == N || ( i + 1 < N && S [ i + 1 ] == ' ' ) ) continue ; } curr += S [ i ] ; } } if ( curr . length >= 1 ) words . push ( curr ) ; for ( let i = 0 ; i < words . length ; i ++ ) { if ( words [ i ] . length == 10 ) { let flag = 0 ; for ( let j = 0 ; j <= 9 ; j ++ ) { if ( j >= 5 && j < 9 ) { if ( isnum ( words [ i ] [ j ] ) == 0 ) flag = 1 ; } else { if ( ischar ( words [ i ] [ j ] ) == 0 ) flag = 1 ; } } if ( ! flag ) { ID = words [ i ] ; } } if ( words [ i ] . substring ( 0 , 3 ) == " " && words [ i ] . substring ( words [ i ] . length - 3 ) == " " ) { Domain = words [ i ] . substring ( 4 ) ; } } document . write ( " " + ID + " " ) ; document . write ( " " + Domain ) ; } let S = " " + " " + " " ; let N = S . length ; findIdandDomain ( S , N ) ;