在这种方法中,我正在查看一个字符串是否出现在单词搜索中。由于一个字符串可以出现在任何方向,我有以下方法检查这些方向:如果语句被跳过
目前我的代码在运行时出现分段错误。这是因为我的方法中的第一个if语句根据gdb被跳过。
网格我与长相的工作是这样的:
a r e o
o n l y
o d d a
的X,Y和在运行时(只是段错误之前)STR值是:
x = 0;
y = 0;
str = "add"
现在的strlen(海峡)评估为3并且x - 3
评估为-3。
-3 < 0
将评估真实并退出我的方法。不幸的是,该方法开始处的if语句在运行时跳过。任何人都知道发生了什么事?
此外,我要补充的是,如果我改变我的网格的第一行:
q r e o
我得到正确的结果。
这里是方法:
bool checkNorth (int x, int y, string str) {
//Deal with trival case and avoid array out of bounds/checking silly things
if (x-strlen(str) < 0){
return false;
}
//for each character in str
for (int i = 0; i < (strlen(str)); i++){
//If the character above in the grid is the next character in the string
if (grid[x-i][y].letter == str[i]){
//keep going
continue;
}else{
//It ain't north
return false;
}
}
//It's north
return true;
}
你为什么要做'strlen(str)'?使用'str.length()'(这是*** std :: string ***,对吧?) – Amit
@Amit:在C?你在想什么? –
@Amit这是C代码,所以我敢打赌'string'确实是'char *'的'typedef'。 –