Start learning free

LESSON 73 OF 281 · STRINGS - CHALLENGES

File Extension in C++: Finding the Last Dot with rfind

Practice Read the full explanation

Given a file name fileName, return its extension: the part of the name after the last dot.

Split the name at every dot, and the extension is the last of the parts. A name can have more than one dot, like "archive.tar.gz": only what comes after the last dot is the extension. Keep its letters exactly as they are.

 

Example 1:

Input: fileName = "photo.jpeg"
Output: "jpeg"
Explanation: The name has one dot, and "jpeg" comes after it.

Example 2:

Input: fileName = "archive.tar.gz"
Output: "gz"
Explanation: Splitting at the dots gives "archive", "tar" and "gz". The last part, "gz", is the extension.

Example 3:

Input: fileName = "README.MD"
Output: "MD"
Explanation: The extension keeps its upper-case letters.

 

Constraints:

  • fileName has at least one dot, and doesn't start or end with a dot.
  • fileName has no spaces: only English letters, digits, dots, - and _.
STEP 1 OF 2
    Ctrl + Enter to check

    C++ · STRINGS - CHALLENGES

    File Extension in C++, explained

    Problem overview

    The function fileExtension receives a file name and returns its extension: the part after the last dot. The name "photo.jpeg" has one dot, so the extension is "jpeg". A name can have several dots. In "archive.tar.gz" only what follows the last dot counts, so the answer is "gz". The extension keeps its letters as they are: "README.MD" gives "MD".

    The name always contains at least one dot, and it neither starts nor ends with one. It has no spaces, only English letters, digits, dots, hyphens and underscores.

    Approach

    Many languages would split the name at every dot and take the last part. C++ strings have no split method, but splitting isn't needed. What matters is where the last dot is. Everything after that position is the extension.

    The std::string class offers two searches that fit. find looks from the start and reports the first match, and rfind looks from the end and reports the last one. For this task the last dot is wanted, so the right choice is rfind.

    Step by step

    1. Find the last dot. fileName.rfind(".") returns the index of the last dot, and the lesson stores it in an int named dot. For "photo.jpeg" it is 5, and for "archive.tar.gz" it is 11.
    2. Take the text after it. fileName.substr(dot + 1) starts one character past the dot, which skips the dot itself. With no second argument, substr goes to the end of the string, which is where the extension ends.

    The solution in C++

    class Solution {
    public:
        string fileExtension(string fileName) {
            int dot = fileName.rfind(".");
            return fileName.substr(dot + 1);
        }
    };
    

    What to know about the C++ involved:

    • find versus rfind. On "archive.tar.gz", find(".") gives index 7, the first dot, and the code would return "tar.gz". Only rfind reaches the last dot.
    • The return type of rfind. It returns an unsigned size_t. When the character is missing, it returns the special value string::npos, which is the largest unsigned number. Stored in an int, that value becomes -1, so dot + 1 is 0 and substr(0) returns the whole name. For a name with no dot, "README", the function returns "README". The lesson's names always have a dot, so this case doesn't arise.
    • A string argument. rfind accepts a string literal such as "." or a single character such as '.'.
    • A new string. substr copies the piece it selects and doesn't change fileName.

    Edge cases and common mistakes

    • Using find. It stops at the first dot, which is wrong for names with several dots.
    • Forgetting the + 1. substr(dot) includes the dot, so the result would be ".jpeg".
    • Changing the case. The extension is returned exactly as written, so "MD" stays upper case.
    • Several dots. A name such as "archive.tar.gz" has the extension "gz", and the same two lines handle it.
    • A name without a dot. The result would be the whole name, as explained above. A robust program would check for string::npos first and return an empty string. The lesson's constraints guarantee a dot, so the lesson doesn't need that check.

    Testing

    • "photo.jpeg" gives "jpeg".
    • "archive.tar.gz" gives "gz".
    • "README.MD" gives "MD".
    Keep going with C++1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free