Originally Posted by
BEN10
a[i][j] is a char not a string. To read strings replace it with a[i] only.
thanks for that...
now I have just seperated some code(searching) into a function, and doesnt work anymore (can compile, would run until the searching starts)
im sure the problem is in the function parameters
not sure why pointers are used, as yet - as a beginner
I have marked the places which are of pain..
any help would be great..
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int i=0,j=0;
FILE *f;
f = fopen("grid1.txt","r");
char a[50][50];
int width, height;
fscanf(f,"%d %d",&width,&height);
fclose(f);
f = fopen("grid1.txt","r");
for(i=0;i<height+2;i++) {
for(j = 0; j < width && fscanf(f,"%s",a[i])==0; j++)
{
fscanf(f,"%s",a[i]);
}
}
for(i=2;i<height+2;i++) {
for(j = 0; j < width ; j++)
{
printf("%c",a[i][j]);
}
printf("\n");
}
wordSearch("england", a, width, height);
}
wordSearch(char *word[], char *a[50][50], int width, int height)
{
int i,j,x,d,l,b,c;
x=strlen(*word);
for(i=2;i<height+2;i++) {
for(j = 0; j < width ; j++)
{
if(word[0]==a[i][j])
{
c=c+1;
if(width-j>x-1)
{
d=j+1;
for(l=1;l<x;l++)
{
if(word[l]==a[i][d])
{
b=b+1;
d=d+1;
if(b==x-1)
{
printf("horizontal match");
b=0;
}
}
}
}
}
}
}
for(j = 0; j < width ; j++) {
for(i=2;i<height+2;i++)
{
if(word[0]==a[i][j])
{
c=c+1;
if(height-i>x-1)
{
d=i+1;
for(l=1;l<x;l++)
{
if(word[l]==a[d][j])
{
b=b+1;
d=d+1;
if(b==x-1)
{
printf("match");
b=0;
}
}
}
}
}
}
}
}