1 Replies - 12352 Views - Last Post: 27 October 2012 - 04:19 AM Rate Topic: -----

#1 brady42589  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-October 12

word search program

Posted 26 October 2012 - 07:14 PM

Hello everyone,
I'm creating a word search program and I've run it a bit of a brick wall on tranversing through multidimensonal string arrays to search for words in 8 different directions. Any help or guidance would be greatly appreciated.
Here is my code.
#include <ctype.h>
//void int ReadArray(char array[40][40])
    //int i, j, k;
    //for(i=1; i<[40];i++){
    //for(j=0; j<[40]; j++)
    //if (array[i][j] == word[0])    
void int CheckRow(char array[40][40],int i, int j)
     int size, count;
     char string[];
     for(count = 0; count<size ; count++){
               string[count] = array[i][j];

int main()
     char file_name[32];
     int rows, columns, i=0, j=0;
     printf("Enter file name: ");
     scanf("%31s", file_name);
     FILE *fp = fopen(file_name, "r");
            printf("error: file did not open\n");
            return 0;

     fscanf(fp,"%d %d", &rows, &columns);
      printf("Rows = %d, Columns = %d\n", rows, columns);
     char array[rows][columns];
     int ch;

     while((ch = getc(fp)) != EOF){
             array[i][j] = towlower(ch);
             if(j == columns){
             j = 0;
     for(i=0; i < rows; i++){
         for(j=0; j < columns; j++){         
         printf("%c", array[i][j]);
     printf("Enter a word to search:");
     scanf("%s", &word);
     word_len = strlen(word);


*** Edit ***
Please use code tags when posting code
Posted Image

This post has been edited by GunnerInc: 26 October 2012 - 07:29 PM
Reason for edit:: Added code tags

Is This A Good Question/Topic? 0
  • +

Replies To: word search program

#2 jimblumberg  Icon User is offline

  • member icon

Reputation: 4736
  • View blog
  • Posts: 14,768
  • Joined: 25-December 09

Re: word search program

Posted 27 October 2012 - 04:19 AM

I have noticed a couple of problems. First when declaring arrays you must specify a size the following is not legal:
     char string[];// Must state a size:
// char string[120];

Next unless you are using a C compiler that supports the C99 standard Variable Length Arrays are also not legal.
     fscanf(fp,"%d %d", &rows, &columns);
      printf("Rows = %d, Columns = %d\n", rows, columns);
     char array[rows][columns];// Not allowed unless using C99 compiler

I really don't recommend using VLA even if you are using a C99 compiler. You would be better off using dynamic memory (malloc/free), which is supported by all versions of the standard.

Other than the above issues you will need to ask specific questions.

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1