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」のエラーを表示することです:「読み取り専用変数 'c​​ount' のインクリメント」。

何が間違っているようですか?

ありがとう!

4

7 に答える 7

3

代わりにfgetsを次のように使用してみてください。

fgets (string, SIZE, stdin);

なぜgets安全でないのか、SOで何度か回答されています。これを見ることができます。

于 2011-04-12T08:17:16.200 に答える
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 に答える
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 に答える
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 に答える
0

gets は、変数のサイズよりも多くのデータを取り込むことができるため危険です。これにより、システムが攻撃にさらされ、セキュリティが侵害されます。no を制限するため、fgets を使用する必要があります。読み取る文字数。

于 2011-04-12T08:50:05.313 に答える
0

代わりに " " に置き換えることを検討してくださいscanf( "%s", &string)

于 2011-04-12T08:16:57.757 に答える
0

gets は、割り当てられたスペースよりも多くのデータを読み取ることができるため危険です。 fgets を使用して、読み取る文字数を指定し、改行が見つかった場合に停止することができます。

于 2011-04-12T08:18:41.233 に答える