Mask the password of a MongoDB connection string with asterisks

This turned out to be interesting puzzle. Seemingly so simple thing made me think a lot.

Problem statement:

You’re given a string, which is a connection string to MongoDB. It consists of parts. The parts are delimited by various characters:

  • between username and password, there is ‘:’
  • after password comes ‘@’
  • after the ‘@’ comes host name

We want to mask any password characters with ‘*’ character.

Write an algorithm (function) that takes in a string, and writes out a masked string.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: