2

我写了以下代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SIZE 128

int main ()

{
    char mychar , string [SIZE];
    int i;
    int const count =0 ;    

    printf ("Please enter your string: \n\n");
    fgets (string, SIZE, stdin);

    printf ("Please enter char to find: ");
    mychar = getchar();

    for (i=0 ; (string[i] == '\0') ; i++ )
        if ( string[i]  == mychar )
            count++;

    printf ("The char %c appears %d times" ,mychar ,count);

    return 0;
}

问题是gcc给了我一个'int const count'的错误:“只读变量'count'的增量”。

似乎有什么问题?

谢谢 !

4

7 回答 7

3

尝试使用fgets代替:

fgets (string, SIZE, stdin);

为什么gets不安全,在SO上已经回答了好几次了。你可以看到这个

于 2011-04-12T08:17:16.200 回答
1

始终使用fgets()而不是gets. 还有很多东西要修。您不应该使用标准库函数来创建用户界面。标准库真的不是为此而设计的。相反,您应该使用curses 库或类似的东西。您还可以编写程序以接受参数作为输入。

正确使用标准库的简短示例。此版本没有任何错误检查,因此它假定用户输入是正确的。

#include <stdio.h>

int main(int artc, char *argv[])
{
    /* arguments are strings so assign only the first characte of the
     * third argument string. Remember that the first argument ( argv[0] ) 
     * is the name of the program. 
     */
    char  mychar = argv[2][0];
    char *string = argv[1];
    int i, count = 0;

    /* count the occurences of the given character */
    for(; *string != '\0'; ++string)
        if(*string == mychar) ++count;

    printf("The char ‘%c’ appears %d times.\n", mychar, count);

    return 0;
}

用法: ./count "Hello, World!" l

输出: The char ‘l’ appears 3 times.


编辑:至于原始代码。更改==!=

for (i=0 ; (string[i] == '\0') ; i++ )

到:

for (i=0 ; (string[i] != '\0') ; i++ )

比较是错误的。

于 2011-04-12T08:41:52.867 回答
1

要使此示例正常工作,您还应该更改以下行:

if(*string == mychar) ++count;

进入

if(string[i] == mychar) ++count;

完整的工作示例现在是:

#include <stdio.h>

int main(int artc, char *argv[])
{
/* arguments are strings so assign only the first characte of the
 * third argument string. Remember that the first argument ( argv[0] ) 
 * is the name of the program. 
 */
char  mychar = argv[2][0];
char *string = argv[1];
int i, count = 0;

/* count the occurences of the given character */
for (i=0 ; (string[i] != '\0') ; i++ )
    if(string[i] == mychar) ++count;

printf("The char ‘%c’ appears %d times in the sentence: %s\n", mychar, count, string);

return 0;
}
于 2012-05-22T09:26:25.180 回答
0

考虑用“ scanf( "%s", &string)”代替。

于 2011-04-12T08:16:57.757 回答
0

get 是危险的,因为它允许您读取比分配空间更多的数据,您可以使用 fgets 指定它将读取多少个字符并在找到换行符时停止。

于 2011-04-12T08:18:41.233 回答
0

获取是危险的,因为它可以接收比变量大小更多的数据。从而使系统受到攻击并危及安全。fgets 应该被使用,因为它限制没有。要读取的字符数。

于 2011-04-12T08:50:05.313 回答
0

这将做:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SIZE 128

int main()
{
  char mychar, string[SIZE];
  int i;
  int count=0;    

  printf("Please enter your string: ");
  fgets(string, SIZE, stdin);

  printf("Please enter char to find: ");
  mychar = getchar();

  for (i = 0; (string[i] != '\0'); i++)
    if (string[i] == mychar) ++count;

  printf("The char %c appears %d times in the sentence: %s" ,mychar ,count, string);

  return 0;
}
于 2012-05-22T09:48:00.950 回答