Replace E-mail with Stars, Asterisk (*). A C++ program to replace Email with special character and print it.

 In this program we will replace the Email id provided by user with Asterisk (Star "*"). Only the First letter, the letter after "@" and ".com" will be visible. This is one of the most commonly asked interview program. 


Program to replace Email with Asterisk "*"


Logic :

  • Store special Characters "@" and "." 
  • Input String (Email)
  • Calculate and store length of string 
  • Set a variable to store index of special character
  • Replace characters with star till "@"
  • Print next character and replace rest characters with star till "."
  • Print ".com" 

Code :


#include <iostream>
using namespace std;

int main() {
    
 char a = '@';
 char b = '.';
 
 string s;
 cin>>s;
 
 int l = s.length();
 int k = 0;
 
 for(int i=1;i<l;i++){
     if(s[i]== a) {
         k = i;
         break;
     }
     else{
         s[i] = '*';
     } 
 }
 
 for(int i = k+2; i<l;i++){
     if(s[i]== b){ 
         break;
     }
     else {
         s[i]='*';
     }
 }
 
 cout<<s<<endl;
 
}


***


!! SUBSCRIBE FOR MORE CODES & VIDEOS !!

Post a Comment

0 Comments