N
N
nait1233212015-12-20 21:49:51
C++ / C#
nait123321, 2015-12-20 21:49:51

A subtle bug in simple C code?

Hello, how can I find out how many words (which are in a text file) in the text begin with A using C? There, the words are separated by commas, I tried to do this, but the counter does not work ...

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>

void read_file(char string[100]);

int main()
{
  char string[100];
  read_file(string);
  return 0;
}

void read_file(char string[100])
{
  char *Ptr;
  FILE *str;
  if ((str = fopen("String.txt", "r")) == NULL)
  {
    puts("Error!");
    return (exit(1));
  }
  int i = 0, count = 0;
  while (!feof(str))
  {
    string[i] = fgetc(str);
    i++;
  }
  
  Ptr = strtok(string, ",");
  if (Ptr[0] == 'A')
    count++;
  while ((Ptr = strtok(NULL, ",")) != NULL)
  {
    if (Ptr[0] == 'A')
      count++;
  }
  printf(" Slov s 1 A: %i \n", count);
  fclose(str);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nait123321, 2015-12-20
@nait123321

Found an error. In the check it was English: A, but in the text it was written in Russian / Ukrainian
Closed

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question