I have to find out how many times each character repeats in a linked list of strings. The string is stored and read from a file. I have to print out the result in 2 ways: alphabetic order and in growing order.
I have tried to write a function which will count the number of times a given char repeats, but it crashes.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct list {
char *string;
struct list *next;
};
typedef struct list LIST;
int count(struct list* head, char search) // funct to calculate how many
//times 1 string appears
{
struct list* current = head;
int count=0;
while (current!=NULL)
{
if(current->string == search)
count++;
}
return count;
}
int main(void) {
FILE *fp;
char line[128];
LIST *current, *head;
head = current = NULL;
fp = fopen("test.txt", "r");
while(fgets(line, sizeof(line), fp)){
LIST *node = malloc(sizeof(LIST));
node->string = strdup(line);
node->next =NULL;
if(head == NULL){
current = head = node;
} else {
current = current->next = node;
}
}
fclose(fp);
//test print
for(current = head; current ; current=current->next){
printf("%s", current->string);
}
count(head, "a");
return 0;
}
The test.txt file contains:
Astazi nu este maine