Count lines and words from a C text file

Asked

Viewed 999 times

-1

I have a problem in the project, this code should count the lines and words of a text file in C but the output of the number of lines is right and the number of words is always 0.

//Função que conta as palavras de uma linha

int palavras(char *line)
{
  int p = 0, j;
  char *str = "\t\r\n\v\f";
  char *saveptr1, *str1, *token;

  line[strlen(line) - 1] = '\0';

  for (j = 1, str = line; j++, str1 = NULL ;)
  {
    token = strtok_r(str1, str, &saveptr1);

    if (token==NULL) break;
    p++;
  }

  return p;
}

// Resto do programa
int palavras(char[]);
int main(int argc, char *argv[])
{ 
  FILE * fp;
  ssize_t n = 0;

  int nlinhas = 0, p = 0;
  char * line = NULL;

  fp = fopen(argv[1], "r");
  exit_on_null(fp, "Erro na abertura");

  while ((n = getline(&line, &n, fp)) != -1)
  {
    line[strlen(line) - 1]='\0'; /

    p = palavras(line);
    nlinhas++;
  }

  printf("Número de Linhas: %i\n",nlinhas);
  printf("Número de Palavras: %i\n",p);

  if (line) free(line);

  fclose(fp);
  return 0;
}
  • Take a look at it: for(j=1,str=line;j++,str1=NULL;) because you put the increment together with the condition of ending the loop. It also seems to me that you mixed the use of str and str1 since you initialize str with the separator characters (missing space) but point to the string received by parameter.

  • The problem is that when I compile the code tells me that I expect a ; before the token ) like this: error: expected ?;' before ?)' token for(j=1,str1=line;j++,str1=NULL).

  • Review your command for, it makes no sense. Why these assignments?

  • I want you to go through each line (j being the number of lines) and use the string that comes from the line and then using the strtok_r function to see if it counts as a word or not.

1 answer

0


Corrections:

  • Inclusion of headings stdio.h, stdlib.h and string.h
  • ssize_t -> size_t
  • Removal of exit_on_null
  • line[strlen(line) - 1] = '\0'; / -> line[strlen(line) - 1] = '\0';

  • And many others

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

int palavras(char *line)
{
  int p = 0;
  char *delim = " \t\r\n\v\f";

  char *str = strtok(line, delim);
  for (; str != NULL; ++p)
    str = strtok(NULL, delim);

  return p;
}

int main(int argc, char *argv[])
{ 
  FILE * fp;
  size_t n = 0;

  int nlinhas = 0, p = 0;
  char * line = NULL;

  fp = fopen(argv[1], "r");

  while ((n = getline(&line, &n, fp)) != -1)
  {
    line[strlen(line) - 1] = '\0';

    p = p + palavras(line);
    nlinhas++;
  }

  printf("Número de Linhas: %i\n",nlinhas);
  printf("Número de Palavras: %i\n",p);

  if (line)
    free(line);

  fclose(fp);
  return 0;
}

I may be wrong, but getline and strtok_r are not part of the standard C library so in a way it is better to avoid.

There are so many corrections that it is not worth explaining one by one.

Explanation

1|  // ...
2|  char *str = strtok(line, delim);
3|  for (; str != NULL; ++p)
4|    str = strtok(NULL, delim);
5|  // ...

Linha 2: Flame strtok to get the string up to some delim.

Linha 3: The first part of for is ignored. In the for, forehead if str is not NULL, strtok returns NULL when it does not find the character(in case someone from delim). In the third part is where the words are counted when it increases p.

Linha 4: takes the next string up to someone from delim, case delim is not found strtok returns NULL causing the loop to stop.

  • I really liked your solution will you please explain the cycle to me better? Thank you

  • Which cycle? of word counting?

  • Yes that same.

  • edit the answer with the explanation

Browser other questions tagged

You are not signed in. Login or sign up in order to post.