My current code is now
Code:
#include <stdio.h>
#include <ctype.h>
typedef struct node
{
char alpha;
int freq;
struct node* next;
}NODE;
int main()
{
FILE* pInfile = NULL;
char buffer[500];
char string[500];
NODE *start = NULL; //list
NODE *temp; //temporary node to store data
NODE *temp2; //for adding node
NODE *current; //node to traverse with
int i = 0;
int bCheck = 0;
pInfile = fopen("data.txt","r");
if(pInfile == NULL)
{
printf("Error: cannot open file");
}
while ( fgets(buffer,127,pInfile)!= NULL )
{
for(i = 0; buffer[i] != '\0' ; i++)
{
printf("%d..",i);
buffer[i] = tolower(buffer[i]);
printf("%c\n",buffer[i]);
bCheck = 0;
if(start == NULL)
{
start = (NODE*)malloc(sizeof(NODE));
start->alpha = buffer[i];
start->freq = 1;
start->next = NULL;
current = start;
//start = current;
}
else if(start != NULL)
{
while(bCheck == 0)
{
current = start;
if((isalpha(buffer[i]))||(isspace(buffer[i])))
{
printf("is alpha\n");
if(current->alpha == buffer[i])
{
printf("test3\n");
current->freq++;
bCheck = 1;
}
else if((current->alpha != buffer[i])&&(current->next == NULL))
{
printf("test4\n");
temp = (NODE*)malloc(sizeof(NODE));
temp->alpha = buffer[i];
temp->freq = 1;
//temp->next = NULL;
printf("TEMP char: %c\t freq: %d\n\n",temp->alpha,temp->freq);
current->next = temp;
//temp->next = NULL;
/*temp2 = start;
while(temp2->next != NULL)
{
temp2 = temp2->next;
}
temp2->next = temp;*/
if(temp->next==NULL)
printf("2TEMP char: %c\t freq: %d\n\n",temp2->next->alpha,temp2->next->freq);
free(temp);
current = start;
while(current != NULL)
{
printf("current char: %c\t freq: %d\n\n",current->alpha,current->freq);
current = current->next;
}
bCheck = 1;
}
else
{
printf("test5\n");
printf("buffer[i] = %c\n",buffer[i]);
printf("current->alpha = %c\n",current->alpha);
current = current->next;
system("pause");
}
}
else
{
printf("not alpha\n");
break;
}
printf("bCheck = %d\n",bCheck);
}
}
}
}
}
My problem is in the else if part
i cannot set temp->next to null because when i set it, it crashes.
and when i don't set it and print it, it shows only the first letter and not the next letter added in
for example my text is "It seems that..."
it prints out
0..i
1..t
is alpha
test4
TEMP char: t freq: 1
current char: i freq: 1
current char: sth that is not an alphabet freq: 4788232
then the program crashes !!!
Please help me :'(