我试图在2d数组中找到一个1d数组(找到一个带有给定2d数组的单词)并且我得到的一切都是正确的,除非它打印出find_ten数组,它打印',0&#39 ;而不是' ten'然后打印错误的位置' -1,-1'。找到我的问题所在的任何帮助都会很棒。我的程序继续打印:
set found at: 0,0
sat found at: 0,0
ere found at: 0,1
,0 found at: -1,-1
axe found at: -1,-1
而不是这个,这是我想要达到的目的:
set found at: 0,0
sat found at: 0,0
ere found at: 0,1
ten found at: 2,0
axe found at: -1,-1
以下是main的代码:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "find_word.h"
int main(){
char find_set[3] = {'s', 'e', 't'};
char find_sat[3] = {'s', 'a', 't'};
char find_ere[3] = {'e', 'r', 'e'};
char find_ten[3] = {'t', 'e', 'n'};
char find_axe[3] = {'a', 'x', 'e'};
char two_d[3][3] = {{'s', 'e', 't'}, {'a', 'r', 'e'}, {'t', 'e', 'n'}};
word_find(two_d, find_set);
word_find(two_d, find_sat);
word_find(two_d, find_ere);
word_find(two_d, find_ten);
word_find(two_d, find_axe);
return (0);
}
功能:
#include <stdio.h>
#include <stdlib.h>
#include "find_word.h"
#include <string.h>
void word_find(char stringof_3 [3][3], char the_word[3]){
char *row_col;
int print_letter = 0;
int word_count = 0;
if(stringof_3[0][0] == the_word[0] && stringof_3[0][1] == the_word[1] && stringof_3[0][2] == the_word[2])
strcpy(row_col, "0,0 ");
else if(stringof_3[0][0] == the_word[0] && stringof_3[1][0] == the_word[1] && stringof_3[2][0] == the_word[2])
row_col = "0,0 ";
else if(stringof_3[1][0] == the_word[0] && stringof_3[1][1] == the_word[1] && stringof_3[1][2] == the_word[2])
row_col = "1,0 ";
else if(stringof_3[2][0] == the_word[0] && stringof_3[2][1] == the_word[1] && stringof_3[2][2] == the_word[2])
row_col = "2,0 ";
else if(stringof_3[0][1] == the_word[0] && stringof_3[1][1] == the_word[1] && stringof_3[2][1] == the_word[2])
row_col = "0,1 ";
else if(stringof_3[0][2] == the_word[0] && stringof_3[1][2] == the_word[1] && stringof_3[2][2] == the_word[2])
row_col = "0,2 ";
else
row_col = "-1,-1";
for(print_letter = 0; print_letter < 3; print_letter++){
printf("%c", the_word[print_letter]);
}
printf(" found at: %s", row_col);
printf("\n");
}
标题:
extern void word_find(char stringof_3[3][3], char the_word[3]); /*Inputs a 2d char array and finds the given 3 letter word */
答案 0 :(得分:1)
将strcpy(row_col, "0,0 ");
替换为row_col= "0,0 ";
答案 1 :(得分:1)
您没有为row_col缓冲区分配空间。你刚刚宣布它就是为什么它会给你奇怪的输出。所以请为row_col缓冲区分配空间。其次请使用strcpy而不是直接将值赋给row_col缓冲区。下面是word_find函数的代码,纠正了这两个错误。
void word_find(char stringof_3 [3][3], char the_word[3]){
char *row_col;
row_col = malloc(6); // allocate space to buffer
int print_letter = 0;
int word_count = 0;
if(stringof_3[0][0] == the_word[0] && stringof_3[0][1] == the_word[1] && stringof_3[0][2] == the_word[2])
strcpy(row_col, "0,0 ");
else if(stringof_3[0][0] == the_word[0] && stringof_3[1][0] == the_word[1] && stringof_3[2][0] == the_word[2])
strcpy(row_col, "0,0 ");
else if(stringof_3[1][0] == the_word[0] && stringof_3[1][1] == the_word[1] && stringof_3[1][2] == the_word[2])
strcpy(row_col, "1,0 ");
else if(stringof_3[2][0] == the_word[0] && stringof_3[2][1] == the_word[1] && stringof_3[2][2] == the_word[2])
strcpy(row_col, "2,0 ");
else if(stringof_3[0][1] == the_word[0] && stringof_3[1][1] == the_word[1] && stringof_3[2][1] == the_word[2])
strcpy(row_col, "0,1 ");
else if(stringof_3[0][2] == the_word[0] && stringof_3[1][2] == the_word[1] && stringof_3[2][2] == the_word[2])
strcpy(row_col, "0,2 ");
else
strcpy(row_col, "-1,-1");
for(print_letter = 0; print_letter < 3; print_letter++){
printf("%c", the_word[print_letter]);
}
printf(" found at: %s", row_col);
printf("\n");
}