Find Unique ID and Domain Name of a Website from a string | Java 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
import java . util . * ; class GFG { static boolean ischar ( char x ) { if ( ( x >= ' A ' && x <= ' Z ' ) || ( x >= ' a ' && x <= ' z ' ) ) { return true ; } return false ; } static boolean isnum ( char x ) { if ( x >= '0' && x <= '9' ) return true ; return false ; } static void findIdandDomain ( String S , int N ) { String ID = " " , Domain = " " ; Vector < String > words = new Vector < String > ( ) ; String curr = " " ; for ( int i = 0 ; i < N ; i ++ ) { if ( S . charAt ( i ) == ' ▁ ' ) { words . add ( curr ) ; curr = " " ; } else { if ( S . charAt ( i ) == ' . ' ) { if ( i + 1 == N || ( i + 1 < N && S . charAt ( i + 1 ) == ' ▁ ' ) ) continue ; } curr += S . charAt ( i ) ; } } if ( curr . length ( ) > 0 ) words . add ( curr ) ; for ( String ss : words ) { if ( ss . length ( ) == 10 ) { boolean flag = false ; for ( int j = 0 ; j <= 9 ; j ++ ) { if ( j >= 5 && j < 9 ) { if ( isnum ( ss . charAt ( j ) ) == false ) flag = true ; } else { if ( ischar ( ss . charAt ( j ) ) == false ) flag = true ; } } if ( ! flag ) { ID = ss ; } } if ( ss . length ( ) > 2 && ss . substring ( 0 , 3 ) . equals ( " www " ) && ss . substring ( ss . length ( ) - 3 ) . equals ( " com " ) ) { Domain = ss . substring ( 4 , ss . length ( ) ) ; } } System . out . print ( " ID ▁ = ▁ " + ID + "NEW_LINE"); System . out . print ( " Domain ▁ = ▁ " + Domain ) ; } public static void main ( String [ ] args ) { 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 ) ; } }