Last Updated: 26 Nov, 2020

Internet Address

Easy
Asked in companies
MAQ SoftwareSpringworks

Problem statement

We know that the address of the Internet resource has format:

<protocol>://<domain>.ru[/<context>]
where,

1. <protocol> can either be equal to "http" (without the quotes) or "ftp" (without the quotes)
2. <domain> is a non-empty string, consisting of lowercase English letters,  
3. The /<context> part may not be present. If it is present, then <context> is a non-empty string, consisting of lowercase English letters. 

If string isn't present in the address, then the additional character "/" isn't written. Thus, the address has either two characters "/" (the ones that go before the domain), or three (an extra one in front of the context).

For example,

'http://sun.ru/sunm', and 'ftp://guj.ru' are the valid address.

Given a string S, which contains all lower case characters without any punctuation marks (":", "/", "."), you have to find out and print the internet resource address.

Input Format:
The first line contains an integer 'T' which denotes the number of test cases or queries to be run.

The first line of each test case contains one string ‘S’.
Output Format:
For each case, we need to print a string representing the internet address.

The output of each test case will be printed in a separate line.
Note:
You do not need to input or print anything, and it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 5
1 <= |S| <= 50

Time limit: 1 sec.

Approaches

01 Approach

Here we can use the property of substring and divide the string into 3 parts. A substring is a contiguous sequence of characters in a string. So we can take a substring of the first 3 characters of the string and a substring of the first 4 characters of the string and check whether that is equal to “ftp” or “http” respectively. Then we can cut the string from that point and start to find the first occurrence of “ru” in the string. Once we get the occurrence, the string before the occurrence of “ru” becomes our domain, and the string remaining after “ru” is our context. We can put all the three parts in their position provided context is not an empty string. If the context is an empty string, then we don’t need to add a reverse slash after “ru”.

 

Algorithm:

 

  • Declare 3 empty strings as protocol, domain, and context.
  • Declare a variable to store the length of the input string.
  • Check whether the substring of the first 3 characters is equal to “ftp” or not
    • If yes, assign “ftp” to the protocol and cut the string from that point.
    • Otherwise, assign “http” to the protocol and cut the string after the fourth position.
  • Update the length of the new string.
  • Run a for loop from i=0 to “length of the string -1” to find the occurrence of first “ru” in the string
    • Take a substring of 2 characters starting from index ‘i’ and check whether that is equal to “ru” or not.
      • If equal, find the substring of the string till the position ‘i’ and put that in the domain and the substring after “ru” to the context.
      • Break the loop
  • Declare a final string to put protocol, domain, and context together.
  • If our context is not an empty string
    • Put them in the format and print
    • Otherwise, put protocol and domain and don’t add a reverse slash at the end.
  • Return the final string.