Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

dukebound85

macrumors Core
Original poster
Jul 17, 2005
19,160
4,152
5045 feet above sea level
hi all, i am trying to walk through a 2 d array to find a sequence of letters in c

i tried this

void letterfind(char arr[AMT][AMT], char result[])
{

int i;
int j;

for(i=0; i< AMT; i++)
{
if(result==arr[0]) //row 0 ,any column
printf("%s",result)
}

}

right now i just want to search the top row for the same letter as the desired word. is this on the right track?

Thanks for any help
 

jsw

Moderator emeritus
Mar 16, 2004
22,910
44
Andover, MA
Are you trying to find a string in the array (i.e., find "ing" in "reading is fun"), or trying to see where the array line matches characters in the result string (i.e., result = "ABCDEFG", arr[x] = "JBWDOTG", printed = "BDG")?
 

jsw

Moderator emeritus
Mar 16, 2004
22,910
44
Andover, MA
Well, then, a hint I'd give would be to first determine the search string's length. Then step through each row of the array like you're doing until/unless you find the first character of the search string, then see if the array, starting at that point, equals the search string (which means you need to first check if there is enough room left in the row to fit the search string, then see if each successive array character equals the search string's character at that position.

Non-code:

Code:
search_length = length of search string

for each row in array:
  for each character in the row*:
    see if character equals first character in search string
      if so, see if enough room is left in array row to fit search string
        if so, loop through characters in search string to see if they match


* technically, you only need to go until you're close enough to the end of the row the the search string would no longer fit
 

dukebound85

macrumors Core
Original poster
Jul 17, 2005
19,160
4,152
5045 feet above sea level
thanks

i am able to find the first letter of the answer string in the 2d array but am a little confused about how to compare the surrounding letters to the answer string. the answer string is read from top to bottom or left to right

do i do another for loop?

thanks for the help
 

DrEasy

macrumors regular
Jan 12, 2004
100
0
Once you find the first letter, you can do a while loop to see if you get a match horizontally (incrementing the column), then another while loop to see if you get a match vertically (incrementing the row this time).
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.