i did some files - makefile , functions(down), main(down) and h-declaration file and when i want to enter some input from file like "once upon a time" and this build me char char like a tree and print how much words i have . and i get always error
Segmentation fault (core dumped)
help?
all node build like that
Code:
typedef struct node {
char letter;
long unsigned int count;
struct node* children[NUM_LETTERS];
} node;
Code:
#include <stdio.h>
#include <string.h>
#include "treeFunctions.h"
#include <stdlib.h>
int main(int argc, char* argv[]) {
char* r = "r";
node* head = newNode();
buildTree(head);
int biggestWordLength=bigWordLength(head, 0);
char* word = (char*)malloc(biggestWordLength);
if( (strcmp(argv[1],r)==0) && (argc==2) )
{
printWordReverse(head,word,0);}
else {
printWord(head,word,0);
}
free(word);
freeLevelsOnTree(head);
return 0;
}
////
Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "treeFunctions.h"
#include <stdlib.h>
node* newNode() {
node* n=NULL;
n= (node*)malloc(sizeof(node));
if (n==NULL){
printf("error-malloc not allocate memory");
return NULL; }
n->count = 0;
int i;
for (i = 0; i < NUM_LETTERS; i++){
n->children[i] = NULL;}
return n;
}
void buildTree(node* head)
{
char character;
node* pointNode = head;
do{
character = getchar();
character = tolower(character); //Turns a big letter into a small letter lib <ctype.h>
if(character == EOF)
{
pointNode->count++;
return;
}
// if(!( character>='a' && character<='z') )
// {continue;}
if (!isCharLeagal(character)) continue;
if (character=='\n' || character==' ' || character=='\t')
{
if ( (pointNode!=NULL) && (pointNode != head) )
{
pointNode->count++;
pointNode= NULL;
}
continue;
}
if(pointNode ==NULL){
pointNode = head;}
pointNode = addword(head, pointNode, character);
}while(character!=EOF);
}
node* addword(node* head,node* pointNode,char character){
int i = (((int)character)-((int)'a')) ; //calculate the place of the index
if( (pointNode->children[i])==NULL ){
pointNode->children[i] = newNode();
pointNode->children[i]->letter = character;
}
pointNode = pointNode->children[i];
return pointNode;
}
void freeLevelsOnTree(node* head)
{
node* nPointer = head;
for (size_t i=0; i<NUM_LETTERS;i++)
{
if(nPointer->children[i] !=NULL){
freeLevelsOnTree(nPointer->children[i]);
}
}
free(nPointer);
}
int bigWordLength(node* n, int counter){
int i;
int length = counter;
for(i = 0; i < NUM_LETTERS; i++)
{
if(n->children[i]!=NULL)
{
int length2 = bigWordLength(n->children[i], counter + 1);
if(length2 > length)
{
length = length2;
}
}
}
return length;
}
void printWord(node* head,char strWord[], int levelTree){
node* nPointer = head;
if (nPointer == NULL){
return;}
if (nPointer->count > 0){
strWord[levelTree] = '\0';
printf("%s\t %ld \n",strWord,nPointer->count);}
int i;
for(i = 0; i < NUM_LETTERS; i++)
{
if(nPointer->children[i] != NULL)
{
strWord[levelTree]=nPointer->children[i]->letter;
printWord(nPointer->children[i], strWord,levelTree + 1);
}
}
}
void printWordReverse(node* head,char strWord[], int levelTree){
node* nPointer2 = head;
if(nPointer2 == NULL){
return;}
if(nPointer2->count > 0){
strWord[levelTree] = '\0';
printf("%s\t %ld \n",strWord,nPointer2->count); }
int i;
for(i = NUM_LETTERS-1; i >= 0; i--)
{
if(nPointer2->children[i]!= NULL)
{
strWord[levelTree] = nPointer2->children[i]->letter;
printWordReverse(nPointer2->children[i], strWord, levelTree + 1);
}
}
}
int isCharLeagal(char c){
if((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c == '\n' || c == ' ' || c == '\t')){
return 1;}
return 0;}