C put crossword horizontal words to new array

185 views Asked by At

I have a full filled crossword and i want to do a program to scan the horizontal words and put them into a new array the crossword is 20x20 array and the new array will be 40x20. ex. If i find 3 horizontal words like: dog, cat, fish the 40x20 must look like
dog --> the first line
cat
fish
0
...
0 --> the 40th line
(Max words will be 40 because 40x20 = 40 lines) one line per word... What i have done so far is not so much.....

#include<stdio.h>
char cross[20][20]={
                {'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
                {'*','T','Y','P','E','*','*','*','*','G','U','I','T','A','R','I','S','T','*','*'},
                {'*','E','*','*','V','A','I','N','*','A','*','*','*','V','*','*','T','*','I','*'},
                {'*','R','O','M','E','*','*','*','*','M','A','R','R','I','A','G','E','*','N','*'},
                {'*','R','*','*','N','O','O','D','L','E','*','O','*','A','*','*','A','N','T','*'},
                {'*','I','N','*','*','*','*','O','*','*','*','P','E','T','R','O','L','*','I','*'},
                {'*','F','*','C','*','C','U','T','*','L','I','E','*','I','*','A','T','O','M','*'},
                {'*','I','*','O','*','U','*','*','M','*','N','*','M','O','U','T','H','*','A','*'},
                {'*','E','N','V','E','L','O','P','E','*','F','*','I','N','*','H','*','A','T','*'},
                {'*','D','*','E','*','T','*','*','A','*','E','*','N','*','*','*','A','G','E','*'},
                {'*','*','A','R','T','*','I','N','T','E','R','I','O','R','*','A','*','O','*','*'},
                {'*','K','*','*','O','*','R','*','*','A','T','*','R','O','B','B','E','R','Y','*'},
                {'*','A','T','*','A','I','R','*','S','T','I','R','*','O','*','O','*','A','*','*'},
                {'*','N','O','*','S','*','I','T','*','*','L','*','S','M','I','L','E','*','S','*'},
                {'*','G','*','*','T','*','T','*','O','*','I','*','O','*','N','I','G','H','T','*'},
                {'*','A','C','E','*','M','A','N','D','A','T','O','R','Y','*','T','O','*','O','*'},
                {'*','R','*','N','Y','*','T','*','E','*','Y','*','T','*','*','I','*','*','P','*'},
                {'*','O','*','D','O','*','E','*','*','*','*','I','*','O','Z','O','N','E','*','*'},
                {'*','O','N','*','U','N','D','E','R','W','A','T','E','R','*','N','O','U','N','*'},
                {'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'}
};
int main(){
    int i,j,a,b;
    char cross1[40][20];

    for(a=0;a<40;a++){
        for(b=0;b<20;b++){
            cross1[a][b]='0';
        }
    }
    for(i=0;i<40;i++){
        for(j=0;j<20;j++){
            printf("%c ",cross1[i][j]);
        }
        printf("\n");
    }
    for(i=0;i<20;i++){
        for(j=0;j<20;j++){
            printf("%c ",cross[i][j]);
        }
        printf("\n");
    }
    for(i=0,a=0;i<20;i++){
        for(j=0;j<20;j++){
            if(cross[i][j]=='*'){}
            else{
                cross1[i][j]=cross[i][j];
            }
        }
    }
    printf("Cross1:\n");
    for(i=0;i<40;i++){
        for(j=0;j<20;j++){
            printf("%c ",cross1[i][j]);
        }
        printf("\n");
    }
    return 0;
}
1

There are 1 answers

1
anatolyg On

Imagine picking words from just one line! Can you do that? If yes, write the code and wrap it with a loop:

for (line = 0; line < 20; ++line)
{
    // cross[line] is just one line
}

If you don't know how to pick words from 1 line, here is an idea. Have a temporary variable "place where the words starts", initialized to -1. Scan the array; if you find a letter, remember where it is. If you find a non-letter, print the word and forget it. A horizontal word should have more than 1 letter in the horizontal direction - this is the way you can distinguish between horizontal and vertical words.

Something like this:

start_index = -1;
for (i = 0; i < 20; ++i)
{
    if (line[i] == '*')
    {
        if (start_index != -1 && i - start_index > 1)
            printf(...);
    }
    else
    {
        if (start_index == -1)
            start_index = i;
    }
}

If you use this code, you should modify it to account for words that terminate on the right margin.