If counter in a text file

Asked

Viewed 95 times

0

Guys I’m trying to make a program that counts the number of ifs in any text, but for some reason it doesn’t count all, if you can help me thank you

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

void main()
{
    FILE *arq;
    char str[24];
    int count = 0;
    arq = fopen("texto.txt","r+");

    while(fgets(str,sizeof(str),arq) != NULL){
        if(strstr(str,"if") != NULL){
            count++;
        }       
    }

    fclose(arq);

    printf("Qt : %i",count);
}

Contents of "text.txt"

1 if if if if
2 if if if if if if if if
3 if if if if if if if if if if if if
4 if if if if if if if if if if if if if if if if
total = 40

When I run the program only 8 are shown.

  • Hello, can you share where you got that logic? I tested your code and now it reads normally, but I changed only one line and I would like to understand better how this logic works.

  • The way you declared your string str you need to make another loop to count on each string you read. For example line 4 contains 49 characters.

  • By chance the application of this algorithm would be to calculate the cyclomatic complexity of a particular code ?

  • @anonymity, the answer to that is no, vain is necessary.

  • @Brewerton: But the function strstr always part of the beginning of the string. In the example of the post it counts only the first occurrence of each line read, as it declared str[24] and there are lines with more than 24 characters to function fgets makes several readings of such lines. I believe the function strtok is that the pointer varies at each location.

3 answers

0

I did it in a more performative way in relation to memory, since the functions of the string.h copy the strings. In performance issue is not notorious, only in incredibly large files. I also do not use lib string.h, follows the code:

#include <stdio.h>

#define MAX_LEN 256

int main() {
    FILE* file;
    char str[256];
    int total = 0;
    const char* strif = "if";

    file = fopen("txt.txt", "r+");

    if (file == NULL) {
        return -1;
    }

    while (fscanf(file, "%s", str) != EOF) {
        if (str[0] == 'i' && str[1] == 'f' && str[3] == '\0') {
            total++;
            continue;
        } else {
            int i;
            for (i = 0; i < MAX_LEN - 1; i++) {
                if(str[i] == '\0'){
                    break;
                }
                if (str[i] == 'i' && str[i + 1] == 'f') {
                    total++;
                }
            }
        }
    }

    printf("Total: %d\n", total);
    fclose(file);
    return 0;
}

0

Hi, change the size of str to 4 and the result will be as expected.

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

void main()
{
    FILE *arq;
    char str[4];
    int count = 0;
    arq = fopen("texto.txt","r+");

    while(fgets(str,sizeof(str),arq) != NULL){
        if(strstr(str,"if") != NULL){
            count++;
        }       
    }

    fclose(arq);

    printf("Qt : %i",count);
}

0

Option traversing the read string:

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

void main() {
    FILE *arq;
    char str[255], *p;
    int count = 0;
    arq = fopen("texto.txt","r+");
    while(fgets(str,sizeof(str),arq) != NULL){
        p = str;
        while ((p = strstr(p, "if")) != NULL){
            count++;
            p++;
        }       
    }
    fclose(arq);
    printf("Qt : %i",count);
}

Browser other questions tagged

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