【发布时间】:2014-09-29 23:53:04
【问题描述】:
在下面的代码中,我试图将字符串数组“char *wordArray[20]...”传递到 main 上面的函数中,该函数旨在查找 wordArray 中包含用户输入字符的所有字符串,并打印每个这样的字符串。函数“findWords”被定义为期望一个常量字符串数组、它的长度和用户输入字符,因为该数组将是只读的。按照我正在使用的文本中的示例,底部是从指向字符串的指针中读取单个字符以及从指针数组中读取字符串的方法的组合。
#include <stdio.h>
#include <stddef.h>
#include <ctype.h>
int arrLength = 0; // Global variable dec. and initialization
void findWords ( const char *c[], int length, char letter ) {
size_t element = 0;
size_t count = 0;
for (element = 0; element < length; element++) {
for (count = 0; c[count] != '\0'; count++) {
if (c[count] == letter) {
printf("%s", c[element]);
}
else {
printf("%s", c[count]);
}
}
count++;
}
return;
} // End function findWords
int main (void) {
{ // Begin Problem 3
// step 1: printf "Problem 3"
puts("Hiya");
// step 2: create a string array of 3 pointers to strings containing at this point, random
words.
const char *wordArray[3] = { "cake", "foxtrot", "verimax" };
char usrInp; // Holds user-input letter.
// step 3: "Input a letter from the user."
// This do...while loop repeats until the user has entered either a lower- or uppercase
letter.
do {
puts("Please enter one lowercase letter - any you'd like\n"); // One string argument
calls for output function puts(); rather than printf();
usrInp = tolower ( getchar() );
} while ( isalpha (usrInp) == 0 );
findWords( wordArray, arrLength, usrInp );
} // End Problem 3
} // End function main
【问题讨论】:
-
似乎有几个问题,其中一个是编译器返回了关于我比较“c[count]”和“letter”的警告,它被识别为 int 类型。我需要一点帮助来了解如何比较 char var 中的字符。到字符串数组中包含的字符串中的字符。注意:请忽略块引用中 *wordArray[] 的大小。