'Create rectangle from given words by some rules

i got this task to make, but i am completely lost. I don't know how to make this... Could you please give me some ideas? I started making it and only thing i made is that i split the whole line into multiple array chars. This is the task:

Description: We want to write software that can produce interesting logos for companies. One type of logo we want to offer is to form a rectangle from 4 words. The words in the rectangle must read from left to right (NOT right to left) and top to bottom (NOT bottom to top) and the letter at each corner must be in both words. Of course, this may be impossible for a given collection of 4 words. Given 4 words your program should print one rectangle logo made from the words. If this can be done in more than one way, prefer solution with longer word placed in row (not as a column). If all words has same length and there is more solution, use word for 1st row the "smallest" word (nearest to begin of alphabet). If this cannot be done at all, print "IMPOSSIBLE" instead of a rectangle.

Input: The line will contain four words, all in uppercase letters, separated by a single space. Each name will be at least 3 letters long. No line will contain more than 40 characters.

Output: For each case, display your rectangle logo, or the single word IMPOSSIBLE.

Notes:

there is 2 solution in example, but we prefer solution with longer word (JONES) in first row. Consider input: ABC, CDE, AFG, GHE. You can use in first row ABC or AFG. But the rule says: for 1st row use worh nearest to the begin of alphabet. That, we use first word "ABC"

INPUT: JOEL JONES LLOYD SAND
OUTPUT: 
JONES
O   A
E   N
LLOYD
INPUT: JAN PETER MARTIN MISO
OUTPUT: IMPOSSIBLE
int main()
{
    char line[40];
    cin.getline(line,sizeof(line));
    char words[4][50];
    int x = 0;
    for(int i = 0, j = 0; i < sizeof(line); i++)
    {
        if(line[i] != ' ')
        {
            words[x][j++] = line[i];
        }
        else
        {
            words[x][j++] = '\0';
            ++x;
            j = 0;
        }
    }
return 0;
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source