Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

208
Visualizações
strlen not giving correct string length C

I am reading from my dictionary and printing out the word + the length of the word for testing purposes.

I use strlen to get the length of the string. However, the numbers I got are not correct. I believe strlen doesn't count the \0 character.

I am reading the first 10 words in the dictionary. My expected output should be:

W:A L:1
W:A's L:3
W:AA's L:4
W:AB's L:4
W:ABM's L:5
W:AC's L:4
W:ACTH's L:6
W:AI's L:3
W:AIDS's L:6
W:AM's L:4

But this is what I got (Notice how the L:'s are on another line. I think this is where the problem is):

W:A
 L:2
W:A's
 L:4
W:AA's
 L:5
W:AB's
 L:5
W:ABM's
 L:6
W:AC's
 L:5
W:ACTH's
 L:7
W:AI's
 L:5
W:AIDS's
 L:7
W:AM's
 L:5

Below is my code:

FILE* dict = fopen("/usr/share/dict/words", "r"); //open the dictionary for read-only access 
   if(dict == NULL) {
      return;
   }

   int i;
   i = 0;

   // Read each line of the file, and insert the word in hash table
   char word[128];
   while(i < 10 && fgets(word, sizeof(word), dict) != NULL) {
      printf("W:%s L:%d\n", word, (int)strlen(word));

      i++;
   }
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

fgets() reads in the newline into the buffer if there's enough space. As a result, you see the newline printed when you print word. From the fgets manual:

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte ('\0') is stored after the last character in the buffer.

(emphasis mine)

You have to trim it yourself:

while(i < 10 && fgets(word, sizeof(word), dict) != NULL) {
  size_t len = strlen(word);
  if ( len > 0 &&  word[len-1] == '\n' )  word[len] = '\0';

  printf("W:%s L:%d\n", word, (int)strlen(word));
  i++;
}
over 4 years ago · Santiago Trujillo Relatório

0

The reason is because fgets pulls the newline character '\n' into your buffer word each time, leading to a higher count by 1 each time.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda