【发布时间】:2019-07-10 04:15:24
【问题描述】:
我正在尝试在 C 中实现一个字谜测试器。调用程序时,用户在双引号中输入两个单词,例如“listen”和“silent”。 我几乎已经让它工作了,但是我编写了一个帮助函数来消除两个输入单词中的空格时遇到了一些麻烦。下面是这个函数的代码:
void noSpaces(char word[100]) {
/*
This is a function to get rid of spaces in a word
It does this by scanning for a space and shifting the
array elements at indices > where the space is
down by 1 as long as there is still a space
there.
*/
for (int i = 0; i < 100; i++) {
while (word[i] == ' ') {
for (int j = i; j < 100; j++) {
word[j] = word[j+1];
}
}
}
}
现在,当我将输入词从 main 函数传递给这个助手时,它工作正常。问题是对该函数的第二次调用。当我在第二个输入上调用此函数时,如果 k 是第一个输入中的空格数,则该函数会删除第二个输入的第一个 k 字母。例如,输入./anagram " banana" "banana" 会给我一个假阴性,如果我添加一个打印语句来查看noSpaces 之后的输入发生了什么
调用他们,我得到以下信息:
banana
anana
这是完整程序的代码:
#include <stdio.h>
int main(int argc, char *argv[]) {
//this if statement checks for empty entry
if (isEmpty(argv[1]) == 0 || isEmpty(argv[2]) == 0) {
//puts("one of these strings is empty");
return 1;
}
//call to noSpaces to eliminate spaces in each word
noSpaces(argv[1]);
noSpaces(argv[2]);
//call to sortWords
sortWords(argv[1]);
sortWords(argv[2]);
int result = compare(argv[1], argv[2]);
/*
if (result == 1) {
puts("Not anagrams");
} else {
puts("Anagrams");
}
*/
return result;
}
int compare(char word1[100], char word2[100]) {
/*
This is a function that accepts two sorted
char arrays (see 'sortWords' below) and
returns 1 if it finds a different character
at entry i in either array, or 0 if at no
index the arrays have a different character.
*/
int counter = 0;
while (word1[counter] != '\0' && word2[counter] != '\0') {
if (word1[counter] != word2[counter]) {
//printf("not anagrams\n");
return 1;
}
counter++;
}
// printf("anagrams\n");
return 0;
}
void sortWords(char word[100]) {
/*
This is a function to sort the input char arrays
it's a simple bubble sort on the array elements.
'sortWords' function accepts a char array and returns void,
sorting the entries in alphabetical order
being careful about ignoring the 'special character'
'\0'.
*/
for (int j = 0; j < 100; j++) {
int i = 0;
while (word[i + 1] != '\0') {
if (word[i] > word[i + 1]) {
char dummy = word[i + 1];
word[i + 1] = word[i];
word[i] = dummy;
}
i++;
}
}
}
void noSpaces(char word[100]) {
/*
This is a function to get rid of spaces in a word
It does this by scanning for a space and shifting the
array elements at indices > where the space is
down by 1 as long as there is still a space there.
*/
for (int i = 0; i < 100; i++) {
while (word[i] == ' ') {
for (int j = i; j < 100; j++) {
word[j] = word[j + 1];
}
}
}
}
int isEmpty(char word[100]) {
// if a word consists of the empty character, it's empty
//otherwise, it isn't
if (word[0] == '\0') {
return 0;
}
return 1;
}
我知道有一个库可以处理字符串,但我真的 想避免不得不使用它。我已经走到这一步了,而且我觉得问题基本上已经解决了,但是对于一件我看不到的小事。
我来自 java 背景,我是 C 新手,如果这解释了我犯的任何错误。
【问题讨论】:
-
当
j为99时,访问word[j+1],即word[100]。但是没有word[100],因为word只有100个条目。 -
为什么要用双引号?这是作业吗?
-
@David Schwartz 感谢您了解这一点,但没有意识到。但是,如果一个词远低于 100 个字符,这是否解释了我所看到的奇怪效果?
-
@n.m 是的,我决定使用命令行参数而不是 scanf,因为我认为让这个任务的第二部分工作可能会出现问题。我只会告诉 TA 在他们的输入周围加上双引号,以防他们有带空格的条目。当我第一次写这篇文章时,我认为我不必处理这个案子。
-
@P.Gillich 没有办法知道。越界访问的影响可能无法预测。