The input file is just 10 random uppercased/lowercased names. I already converted all of them to uppercase and now i am trying to loop through the uppercased names and see how many times each one occurs. That is what I am trying to achieve with the
, etc. However I am getting a warning at all the :
Code:
Warning: Comparison between pointer and integer
Can you guys give me a hand and point me in the right direction as to how I can increment the name counter so the output goes like:
Code:
BOB 3BAB 1
ALEX 1
ALEXIS 1
MARY 3
FRED 1
Here is the code that I have so far:
Code:
# include <stdio.h># include <string.h>
# include <ctype.h>
// To uppercase function.
void to_upper(char *names);
int main()
{
// initializing the variables.
int BOB_count, BAB_count, ALEX_count, ALEXIS_count, MARY_count, FRED_count, i, j;
char names[10], a;
const int MAX_NAME_LENGTH = 10;
// Opening the file.
FILE *ifp = fopen("input.txt","r");
// Checking if the file opens.
if(!ifp)
{
printf("Unable to open file.\n");
return -1;
}
for (i = 0; i<=MAX_NAME_LENGTH; i++)
{
// Reads the file and stores the given names to names
fscanf(ifp, "%s", names);
to_upper(names);
// A is a temp variable to check if name[i] is equal to the given name, and if it is equal, increment the counter.
a = names[i];
if (a == "BOB")
{
BOB_count++;
}
if (a == "BAB")
{
BAB_count++;
}
if (a == "ALEX")
{
ALEX_count++;
}
if (a == "ALEXIS")
{
ALEXIS_count++;
}
if (a == "MARY")
{
MARY_count++;
}
if (a == "FRED")
{
FRED_count++;
}
}
if (BOB_count != 0)
{
printf("BOB %d\n", BOB_count);
}
if (BAB_count != 0)
{
printf("BAB %d\n", BAB_count);
}
if (ALEX_count != 0)
{
printf("ALEX %d\n", ALEX_count);
}
if (ALEXIS_count != 0)
{
printf("ALEXIS %d\n", ALEXIS_count);
}
if (MARY_count != 0)
{
printf("MARY %d\n", MARY_count);
}
if (FRED_count != 0)
{
printf("FRED %d\n", FRED_count);
}
// Closes the file.
fclose(ifp);
return 0;
}
// Converts the file content to uppercase
void to_upper(char *names)
{
int i = 0;
while (names[i] != '\0')
{
names[i] = toupper(names[i]);
i++;
}
}