Reduce a string to a valid email address of minimum length by replacing specified substrings | C ++ program for the above approach ; Function to find the minimum length by replacing at with @ and dot with ' . ' such that the string is valid email ; Stores string by replacing at with @ and dot with ' . ' # such that the string is valid email ; append first character ; Stores index ; Check if at ( @ ) already included or not ; Iterate over characters of the string ; at can be replaced at most once ; Update ans ; Update i ; Update notAt ; If current substring found dot ; Update ans ; Update i ; Update ans ; Update i ; Driver code ; To display the result
#include <bits/stdc++.h> NEW_LINE using namespace std ; string minEmail ( string email ) { string ans = " " ; int len = email . length ( ) ; ans += email [ 0 ] ; int i = 1 ; bool notAt = true ; while ( i < len ) { if ( i < len - 3 && notAt && email [ i ] == ' a ' && email [ i + 1 ] == ' t ' ) { ans += ' @ ' ; i += 1 ; notAt = false ; } else if ( i < len - 4 && email [ i ] == ' d ' && email [ i + 1 ] == ' o ' && email [ i + 2 ] == ' t ' ) { ans += ' . ' ; i += 2 ; } else { ans += email [ i ] ; } i += 1 ; } return ans ; } int main ( ) { string email = " geeksforgeeksatgmaildotcom " ; cout << ( minEmail ( email ) ) ; }