Thread: Printing words into a 2D array

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    1

    Printing words into a 2D array

    Hi I'm making a crossword program which takes an input and prints it out onto a crossword board. The input is in the following format:

    Puzzle-Word Row-Letter Column-Number Direction Clue

    I can separate this input into its components. However I am having trouble taking the puzzle word and putting into the crossword grid. Is it possible for someone to give me some guidance to a possible solution for this?

    Your help is much appreciated.

    Here is the code I have written so far:

    Code:
    #include <stdio.h>
    #define MAX_SIZE 12
    #define DOT '.'
    #define MAX_PUZZLE 10
    #define CLUE_LENGTH 25
    
    
    void printClues ();
    void printBoard ();
    int  main(int argc, char *argv[]) {
        
        printf("Enter new clue (type . if finished): ");
        printClues ();
        printBoard (puzzleWord);
        
        
       
    return 0;
    }
    
    
    void printClues () {
        
        char  puzzleWord[MAX_PUZZLE];
        char  rowLetter;
        int   columnNum;
        char  direction;
        char  clue[CLUE_LENGTH];
        //separates the input into its components
        scanf("%s %c %d %c", puzzleWord, &rowLetter, &columnNum, &direction);
        fgets(clue, CLUE_LENGTH, stdin); 
        //prints the separate components of the input to check for successful separation
        printf("Puzzle-word: %s\n", puzzleWord);
        printf("Row Letter: %c\n", rowLetter);
        printf("Column Number: %d\n", columnNum);
            if (direction == 'a'){
                printf("Direction: ACROSS\n");
            } else if (direction == 'd') {
                    printf("Direction: DOWN\n");
              }
        printf("Clue:%s", clue);
        
    } 
        
    void printBoard (char  board[MAX_SIZE][MAX_SIZE]) {
        int  i, j;
        char c = 'a';
        //INTIALISES THE EMPTY CROSSWORD GRIDE
        for (i=0; i<MAX_SIZE; i++) {
            for (j=0; j<MAX_SIZE; j++) {
                board[i][j] = DOT;
            }
        }
        //PRINTS THE BOARD
        printf("  0 1 2 3 4 5 6 7 8 91011\n");
        for (i=0; i<MAX_SIZE; i++) {
            printf("%c", c++);
                for (j=0; j<MAX_SIZE; j++) {
                    printf(" %c", board[i][j]);
                }
         printf("\n");
         }
         printf("\n");
    }

  2. #2
    Registered User
    Join Date
    Aug 2007
    Location
    MD, USA
    Posts
    71
    puzzleWord is an array or type char right?
    So iterate through the array:
    Code:
      char str[] = "hello world";
      int i;
      
      for (i=0; str[i] != '\0'; i++) {
        printf("The letter is: %c \n", str[i]);
      }
    Maybe you could incorporate the grid position changes (where you want t copy chars to) right into that loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing words to console screen
    By Shadow20 in forum C Programming
    Replies: 19
    Last Post: 04-08-2010, 07:17 PM
  2. no. printing in words
    By rits in forum C Programming
    Replies: 2
    Last Post: 09-09-2009, 10:11 AM
  3. Printing out 2 largest words
    By 0rion in forum C Programming
    Replies: 2
    Last Post: 06-02-2004, 12:51 AM
  4. extracting words from an array of words
    By axon in forum C++ Programming
    Replies: 2
    Last Post: 04-02-2003, 11:21 PM
  5. printing in words
    By arun in forum C Programming
    Replies: 3
    Last Post: 12-28-2001, 12:23 AM