Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZEOFNAME 25
typedef struct names
{
char nameCheck[SIZEOFNAME];
}Names;
void countNames(int qty)
{
FILE *text = fopen("names.txt", "r");
int i = 0;
Names *temp = (Names*)malloc(qty*sizeof(Names));
char str[SIZEOFNAME];
while (!feof(text))
{
fgets(str, SIZEOFNAME, text);
_flushall();
strcpy(temp[i].nameCheck, str);
i++;
}
int nameDuplicates=0;
int flag=0;
for (i = 0; i < qty-1; i++){ // string compare with next line
flag=strcmp(temp[i].nameCheck,temp[i+1].nameCheck);
if (flag)
nameDuplicates++;
}
printf("The total number of names in text is %d and the same names is %d\n", qty, nameDuplicates);
fclose(text);
free(temp);
}
int main()
{
int ch;
int qtyNames = 0; // How many names
int wordCount = 0;
FILE *text = fopen("names.txt", "r");
/**************************************************/
while (!feof(text)) // Counting lines in text documents with names
{
ch = fgetc(text);
if (ch == '\n')
{
qtyNames++;
}
}
fclose(text);
/**************************************************/
countNames(qtyNames);
puts("End\n");
}
Hello, need help please with comparing names from text document
example:
Name1
Name2
Name3
Name1
Target to put names in dynamic array and compare if there is similar names in text document
like Name1 is 2 times it will print there are 2 names are duplicated
if someone can tell me what I'm doing wrong very appreciating your help thank you.