-1
I’m a beginner in programming and I’m learning how to manipulate strings, but I’m still a little confused on some kkkkkk things. Finally, regarding the exercise, we have a txt file containing the following content "maria Joao maria maria maria". The user when typing a name (maria or joão) the program returns an integer with the amount of times this name appears in the file.
Entree: Maria
exit: 3
I couldn’t make much progress on the show, but he’s like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
FILE *file;
char frase [27];
char palavra [8];
file = fopen ("arquivo.txt", "r");
if (file == NULL)
{
printf ("Erro na abertura do arquivo");
exit(0);
}
scanf ("%s", &palavra);
while (fgets(frase, 27, file)!= NULL){
retorno = strcmp (palavra, frase);
}
printf ("O número de vezes e %d", retorno);
fclose (file);
}
So I was thinking of going through the user-typed string, character by character, and comparing if each value of that in the strcmp function returns zero and then trying something.
use
fscanf()
to read from disk so gets the string ready to compare with the typed– arfneto