Batman
Batman

Reputation: 1324

gets() problem in C

I wrote the following code:

#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;
}

The problem is that the gcc gives me an error for the 'int const count': " increment of read-only variable ‘count’".

What seems to be wrong ?

Thank !

Upvotes: 2

Views: 10368

Answers (7)

Christian Kastberg
Christian Kastberg

Reputation: 11

This will do:

#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;
}

Upvotes: 0

Christian Kastberg
Christian Kastberg

Reputation: 11

To make this example work you should also change the line:

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

into

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

Full working example is now:

#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;
}

Upvotes: 1

Athabaska Dick
Athabaska Dick

Reputation: 4281

Always use fgets() instead of gets. Also there are lots of stuff to fix. You shouldnt use standard library functions for creating user interface. Standard library is really not designed for that. Instead you should use curses library or something similar. You could also write the program to accept arguments as input.

Short example of proper use of the standard library. This version does not have any error checking so it assumes that user input is correct.

#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;
}

Usage: ./count "Hello, World!" l

Output: The char ‘l’ appears 3 times.


EDIT: As for the original code. Change == to !=.

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

to:

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

The comparison was wrong.

Upvotes: 1

Khemka
Khemka

Reputation: 108

gets is dangerous because it can take in more data than the size of the variable. Thereby exposing the system to attacks and compromising security. fgets should be used as it limits no. of characters to be read.

Upvotes: 0

codaddict
codaddict

Reputation: 455142

Try using fgets instead as:

fgets (string, SIZE, stdin);

Why gets is unsafe, has been answered several times on SO. You can see this.

Upvotes: 3

gizgok
gizgok

Reputation: 7649

gets is dangerous because it lets you read in more data than you've allocated space for, you can use fgets which specifies how many characters it is going to read in and stops if it finds a newline.

Upvotes: 0

Artem Barger
Artem Barger

Reputation: 41232

Consider replace with "scanf( "%s", &string)" instead.

Upvotes: 0

Related Questions