Searching for A SUFFIX in a tree - c

So, I'm trying to do an impletation of Sharing Suffixes WITHOUT USING UKKONNENS ALGORITHM (don't understand ukkonnens). So basicaly what I'd do is searching for a word in every node that isn't ending. However, when I try to debug it, it returns a negative value. I honestly don't know how to solve it, I made constant breakpoints so it would come out of the loop and not do as many iterations but it just keeps going.
I'm very new to C so for me this is a hard thing and I appreciate all the help.
#include "trie.h"
tree_t create_trie()
{
int i = 0;
tree_t node = (tree_t)malloc(sizeof(struct _node));
for(i = 0; i < 26; i++)
{
node->sons[i] = NULL;
}
node->ending = false;
return node;
}
void delete_trie(tree_t trie)
{
int i = 0;
for(i = 0; i < 26;i++)
{
if(trie->sons[i]!=NULL)
{
delete_trie(trie->sons[i]);
}
else
{
continue;
}
}
free(trie);
}
tree_t put_trie(tree_t trie, char* s)
{
int i = 0;
char* t = s;
tree_t aux = trie;
if(lookup_trie(trie, s))
return trie;
for(i = 0; t[i]!='\0'; i++)
{
int it = (int)((char)tolower(t[i]) - 'a'); //le index
printf("\niterator %d\n", it);
if(aux->sons[it] == NULL)
{
aux->sons[it] = create_trie();
//aux->sons[it]->s = (char *)malloc(sizeof(char*));
//printf("created tree");
}
aux = aux->sons[it];
}
aux->ending = true;
return trie;
}
bool lookup_trie(tree_t trie, char* s)
{
int i = 0;
char* t = s;
tree_t aux = trie;
for(i = 0; i<strlen(t); i++)
{
int it = (int)((char)tolower(t[i]) - 'a'); //le index
if(aux->sons[it] == NULL)
{
return false;
}
aux = aux->sons[it];
}
if(aux->ending)
{
return true;
}
return false;
}
void print_trie(tree_t trie)
{
int i = 0;
for(i = 0; i<26; i++)
{
if(trie->sons[i] == NULL)
{
continue;
}
else
{
printf("%c", ((char)(i) + 'a'));
print_trie(trie->sons[i]);
}
//print_trie(trie->sons[i])
printf("\n");
}
}
tree_t share_word(tree_t trie, tree_t auxiliar, char* s)
{
int x = 0, j = 0; //auxiliar to compare and for variable
printf("Share word debug\n");
/*if(!is_vide(trie))
return (tree_t)NULL;*/
printf("here");
printf("%s\n",s);
for(j=0; j<26; j++)
{
for(x = 0; x < strlen(s); x++)
{
int it = (int)((char)tolower(s[x]) - 'a');
printf("%d\n", it);
//if its null i'm gonna look into other nodes from the first for
if(trie->sons[j] == NULL)
{
continue;
}
if(trie->sons[j] == NULL)
{
break;
}
else if(trie->sons[it] == NULL) //might pose a problem
{
//if the node is empty, we just jump to the next iteration
if(lookup_trie(trie->sons[j], s)) //if the suffix is present, return the subtree
{
printf("Word belongs\n");
auxiliar = trie->sons[j]; //is auxiliar JUST A POINTER?
return auxiliar;
}
else //if not present, go again through the function
{
auxiliar = share_word(trie->sons[j], auxiliar, s);
}
}
else //trie->sons[it] not null
{
printf("iterator not null\n");
if(lookup_trie(trie->sons[it], s+x)) //if the word is present
{
return trie->sons[it];
}
}
}
}
return auxiliar;
}
tree_t put_word(tree_t trie, char *s)
{
int i = 0;
tree_t auxiliar = create_trie();
printf("Beggining\n");
//missing a condition so it doesnt add all the suffixes;
auxiliar = share_word(trie, auxiliar, s);
/*for(i = 0; i < strlen(s); i++)
{
int it = (int)((char)tolower(s[i]) - 'a');
auxiliar = share_word(trie, auxiliar, s + i); //should look for the suffixes;
if(auxiliar->sons[it] != NULL )
{
if(trie->sons[it] == NULL)
{
trie->sons[it] = create_trie();
trie->sons[it] = auxiliar;
return trie;
}
}
else
{
printf("Auxiliar null\n");
trie->sons[it] = create_trie(); //proceeds to the next iteration by creating the node
}
//}
trie = trie->sons[it];
}*/
return trie;
}
bool is_vide(tree_t trie)
{
int i =0;
for(i =0; i <26;i++)
{
if(trie->sons[i] != NULL)
{
return false;
}
else
{
return true;
}
}
return true;
}
bool leads_leaf(tree_t trie, char *s)
{
int i = 0;
tree_t aux = trie;
for(i = 0; i < strlen(s); i++)
{
int it = (int)((char)tolower(s[i]) - 'a');
aux = aux->sons[it];
if(i == (strlen(s) - 1) && aux->ending)
{
return true;
}
else
{
return false;
}
}
return false;
}
int main()
{
tree_t tree = create_trie();
tree = put_trie(tree, "abcd");
tree = put_word(tree, "decd");
delete_trie(tree);
return 0;
}
I tried testing putting a string in a single way (abcd) and then searching the string (decd), which would add d, e, and when it would go for c it would find the node of the first strings that leads to c -> d -> '\0'. However, it sometimes just keeps veryfing the same letter (d) and doesn't add it or it just never goes to the next (e).
I think there is a problem with the function put_word and how it calls the share suffixes multiple times.

Related

Anyone know why this code gives wrong output in leet code and works fine in vs code

so basically i am trying to solve a leet code problem called [two sum II] using hashing
but i am getting error in this test case 1,2,3,4,4,9,56,90 where i have to find two index those elements sum is equal to target 8
well the answer of this test case is 4,5 because the sum of index4 and index5 in array[1-8] is 8
Here the problem is when i compiled this below code in vs code it works perfectly fine and gives correct output 4,5
but during leet code submission it throws wrong answer and showing output 1,3 instead of 4,5
// here is my hash implemention code
#include <stdio.h>
#include <stdlib.h>
typedef struct Hash {
int value;
int index;
struct Hash *next;
} hash;
hash *Hashes[10];
int hashify(int value) { return abs(value) % 10; }
void insert(int value, int index) {
int key = hashify(value);
if (Hashes[key] == NULL) {
Hashes[key] = malloc(sizeof(hash));
Hashes[key]->value = value;
Hashes[key]->index = index;
Hashes[key]->next = NULL;
return;
}
hash *ptr = Hashes[key];
while (ptr->next != NULL) ptr = ptr->next;
ptr->next = malloc(sizeof(hash));
ptr->next->value = value;
ptr->next->index = index;
ptr->next->next = NULL;
return;
}
int search(int value) {
int key = hashify(value);
if (Hashes[key] == NULL) return -1;
if (Hashes[key]->value == value)
return Hashes[key]->index;
else {
hash *ptr = Hashes[key]->next;
while (ptr != NULL) {
if (ptr->value == value) return ptr->index;
ptr = ptr->next;
}
return -1;
}
}
// here is hash_free function
void Hash_free() {
for (int i = 0; i < 10; i++) {
if (Hashes[i] == NULL)
continue;
else {
if (Hashes[i]->next == NULL) {
free(Hashes[i]);
Hashes[i] = NULL;
} else {
hash *ptr;
while (ptr != NULL) {
ptr = Hashes[i]->next;
free(Hashes[i]);
Hashes[i] = ptr;
}
}
}
}
}
// here is two sum function code
int *twoSum(int *numbers, int numbersSize, int target, int *returnSize) {
int *result;
if (numbersSize == 2) {
result = malloc(2 * sizeof(int));
result[0] = 1;
result[1] = 2;
*returnSize = 2;
return result;
} else {
int val, element;
for (int i = 0; i < numbersSize; i++) {
val = target - numbers[i];
element = search(val);
if (element != -1) {
result = malloc(2 * sizeof(int));
if (element < i) {
result[0] = element + 1;
result[1] = i + 1;
} else {
result[0] = i + 1;
result[1] = element + 1;
}
*returnSize = 2;
Hash_free();
return result;
}
insert(numbers[i], i);
}
}
return NULL;
}
// here is main code
int main() {
int numbers[] = {1, 2, 3, 4, 4, 9, 56, 90};
int target = 8;
int numberSize = sizeof(numbers) / sizeof(int);
int returnSize;
int *res = twoSum(numbers, numberSize, target, &returnSize);
for (int i = 0; i < returnSize; i++) {
printf("%d ", res[i]);
}
free(res);
return 0;
}
Your "hash" variable is global, so it keeps preserving data of previous testcase executed. I have faced the same when using global variable.
Solution:
Just clear your hash or initialize it, in your main function( or the entry point function), so that it will ensure a fresh start for each of the test cases those are executed.

Why is my Hash Map insert function not working?

I am making a Hash Map with people's names as its key using C language. I am using separate chaining to resolve the collision.
This is my code:
#include<stdio.h>
#include<stdlib.h>
#define MinTableSize 1
#include <stdbool.h>
//Colission resolution Using linked list
struct ListNode;
typedef struct ListNode *Position;
struct HashTbl;
typedef struct HashTbl *HashTable;
typedef unsigned int Index;
Index Hash(const char *Key, int Tablesize)
{
unsigned int HashVal = 0;
while(*Key != '\0')
{
HashVal += *Key++;
}
return HashVal % Tablesize;
}
struct ListNode
{
int Element;
Position Next;
};
typedef Position List;
struct HashTbl
{
int TableSize;
List *TheLists;
};
//Function to find next prime number for the size
bool isPrime(int n)
{
if(n <= 1)
{
return false;
}
if(n <= 3)
{
return true;
}
if(n%2 == 0 || n%3 == 0)
{
return false;
}
for(int i = 5; i*i <= n; i = i + 6)
{
if(n%i == 0 || n%(i + 2) == 0)
{
return false;
}
}
return true;
}
int NextPrime(int N)
{
if(N <= 1)
{
return 2;
}
int prime = N;
bool found = false;
while(!found)
{
prime++;
if(isPrime(prime))
{
found = true;
}
}
return prime;
}
HashTable InitializeTable(int TableSize)
{
HashTable H;
int i;
if(TableSize < MinTableSize)
{
printf("Table size is too small\n");
return NULL;
}
H = malloc(sizeof(struct HashTbl));
if(H == NULL)
{
printf("Out of space\n");
return NULL;
}
H->TableSize = NextPrime(TableSize);
H->TheLists = malloc(sizeof(List) * H->TableSize);
if(H->TheLists == NULL)
{
printf("Out of space\n");
return NULL;
}
for(i = 0; i < H->TableSize; i++)
{
H->TheLists[i] = malloc(sizeof(struct ListNode));
if(H->TheLists[i] == NULL)
{
printf("Out of space\n");
return NULL;
}
else
{
H->TheLists[i]->Next = NULL;
}
}
return H;
}
//funtion to find the value
Position Find(const char *Key, HashTable H)
{
Position P;
List L;
L = H->TheLists[Hash(Key, H->TableSize)];
P = L->Next;
while(P != NULL && P->Element != Key)
{
P = P->Next;
}
return P;
}
void Insert(const char *Key, HashTable H)
{
Position Pos;
Position NewCell;
List L;
Pos = Find(Key, H);
if(Pos == NULL)
{
NewCell = malloc(sizeof(struct ListNode));
if(NewCell == NULL)
{
printf("Out of space\n");
return NULL;
}
else
{
L = H->TheLists[Hash(Key, H->TableSize)];
NewCell->Next;
NewCell->Element = Key;
L->Next = NewCell;
printf("Key inserted\n");
}
}
else
{
printf("Key already exist\n");
}
}
int main()
{
char Name[6][20] = {"Joshua", "Erica", "Elizabeth", "Monica", "Jefferson", "Andrian"};
int Size = sizeof(Name[0])/sizeof(Name[0][0]);
HashTable H = InitializeTable(Size);
Insert(Name[0], H);
Insert(Name[1], H);
Insert(Name[2], H);
Insert(Name[3], H);
}
The putout of this code is:
Key inserted
Key inserted
This means, it only successfully inserted two keys, while the other name has not been inserted. I think there's some error in my Insert() function, but I got no clue. I tried using an online compiler and it compile properly.

priority queue utilizing a heap working except for freeC

I have my priority queue working and printing as I expected it to, but the free is invalid and I'm not sure why, i'm following all the protocols for the heap that I have learned so far, but my program will not finish.
#include <stdlib.h>
#include "priority_queue.h"
struct item{
int priority;
int value;
};
typedef struct item Item;
struct priority_queue{
int size;
int capacity;
int front;
Item* data;
};
typedef struct priority_queue Priority_queue;
void priority_queue_fix_down(PRIORITY_QUEUE hQueue, int index, int size);
PRIORITY_QUEUE priority_queue_init_default(void){
Priority_queue* pQueue_ptr;
pQueue_ptr = (Priority_queue*)malloc(sizeof(Priority_queue));
if(pQueue_ptr != NULL){
pQueue_ptr->size = 0;
pQueue_ptr->capacity = 1;
pQueue_ptr->data = (Item*)malloc(sizeof(Item)*pQueue_ptr->capacity);
if (pQueue_ptr->data == NULL){
free(pQueue_ptr);
pQueue_ptr = NULL;
}
}
return pQueue_ptr;
}
Status priority_queue_insert(PRIORITY_QUEUE hQueue, int priority_level, int data_item)
{
Priority_queue* pQueue_ptr = (Priority_queue*)hQueue;
Item* temp, temp2;
//temp = (Item*)malloc(sizeof());
int i;
if (pQueue_ptr->size >= pQueue_ptr->capacity)
{
//not enough space
temp = (Item*)malloc(sizeof(Item) * 2 * pQueue_ptr->capacity);
if (temp == NULL)
{
return FAILURE;
}
for (i = 0; i < pQueue_ptr->size; i++)
{
temp[i] = pQueue_ptr->data[i];
}
pQueue_ptr->front = 0;
pQueue_ptr->capacity *= 2;
free(pQueue_ptr->data);
pQueue_ptr->data = temp;
}
i = pQueue_ptr->size;
(pQueue_ptr->data[i]).priority = priority_level;
(pQueue_ptr->data[i]).value = data_item;
int index_of_parent;
index_of_parent = (i - 1) / 2;
while (i >= 1 && ((pQueue_ptr->data[i]).priority > (pQueue_ptr->data[index_of_parent]).priority))
{
temp2 = pQueue_ptr->data[index_of_parent];
pQueue_ptr->data[index_of_parent] = pQueue_ptr->data[i];
pQueue_ptr->data[i] = temp2;
i = index_of_parent;
index_of_parent = (i - 1) / 2;
}
pQueue_ptr->size++;
// pQueue_ptr->front = 0;
// pQueue_ptr->back = pQueue_ptr->size-1;
return SUCCESS;
}
void print_heap(PRIORITY_QUEUE hQueue){
Priority_queue* pQueue_ptr = (Priority_queue*) hQueue;
for(int x = 0; x<pQueue_ptr->size;x++){
printf("%d\n", pQueue_ptr->data[x].priority);
}
}
Status priority_queue_service(PRIORITY_QUEUE hQueue){
//set variables
Priority_queue* pQueue_ptr = (Priority_queue*) hQueue;
Item temp;
int size, index, index_left_child, index_right_child;
if(pQueue_ptr->size == 0){
return FAILURE;
}
index = 0;
temp = pQueue_ptr->data[0];
pQueue_ptr->data[0] = pQueue_ptr->data[pQueue_ptr->size-1];
pQueue_ptr->data[size-1] = temp;
pQueue_ptr->size--;
priority_queue_fix_down(pQueue_ptr, pQueue_ptr->front, pQueue_ptr->size);
return SUCCESS;
}
int priority_queue_front(PRIORITY_QUEUE hQueue, Status* status)
{
Priority_queue* pPriority_queue = (Priority_queue*)hQueue;
if (priority_queue_is_empty(pPriority_queue))
{
if (status != NULL)
{
*status = FAILURE;
}
return 0;
}
if (status != NULL)
{
*status = SUCCESS;
}
return (pPriority_queue->data[pPriority_queue->front]).value;
}
Boolean priority_queue_is_empty(PRIORITY_QUEUE hQueue){
Priority_queue* pQueue_ptr = (Priority_queue*) hQueue;
if (pQueue_ptr->size <= 0)
return TRUE;
else
return FALSE;
}
This is where I get the error when debugging, on free(pPriority_queue->data); It doesn't even reach the line below. I tried taking someone elses "service" function and it worked, but they implemented "fix down" in their service function, while I'm trying to do it outside in a separate function.
void priority_queue_destroy(PRIORITY_QUEUE* phQueue){
Priority_queue* pPriority_queue = (Priority_queue*)*phQueue;
free(pPriority_queue->data);
free(*phQueue);
// *phQueue = NULL;
return;
}
void priority_queue_fix_down(PRIORITY_QUEUE hQueue, int index, int size){
Priority_queue* pQueue_ptr = (Priority_queue*) hQueue;
Item* temp;
temp = (Item*)malloc(sizeof(Item));
if (temp == NULL)
return NULL;
//int front = priority_queue_front(pQueue_ptr, NULL);
int index_left_child = 2* index + 1;
int index_right_child = 2* index + 2;
// print_heap(pQueue_ptr);
// printf("\nsize: %d\nindex: %d\n", size, index);
// // printf("Front: %d\n", pQueue_ptr->data[0]);
// if(pQueue_ptr->data[index_left_child].priority > (pQueue_ptr->data[index]).priority){
// printf("Left child index %d\nRight child index: %d\nLeft child has highest priority of: %d\n",index_left_child, index_right_child, pQueue_ptr->data[index_left_child].priority);
// }
// else
// printf("Right child index: %d\nLeft child index %d\nRight child has highest priority of: %d\n",index_right_child, index_left_child, pQueue_ptr->data[index_right_child].priority);
if (index_left_child < size){
if (index_right_child < size && pQueue_ptr->data[index_right_child].priority > (pQueue_ptr->data[index_left_child]).priority){
if(pQueue_ptr->data[index_right_child].priority > (pQueue_ptr->data[index]).priority){
*temp = pQueue_ptr->data[index];
pQueue_ptr->data[index] = pQueue_ptr->data[index_right_child] ;
pQueue_ptr->data[index_right_child] = *temp;
priority_queue_fix_down(pQueue_ptr, index_right_child, size);
}
}
if(pQueue_ptr->data[index_left_child].priority > (pQueue_ptr->data[index]).priority){
*temp = pQueue_ptr->data[index];
pQueue_ptr->data[index] = pQueue_ptr->data[index_left_child];
pQueue_ptr->data[index_left_child] = *temp;
priority_queue_fix_down(pQueue_ptr, index_left_child, size);
}
}
}

Access violation in Breadth First Search

I've written this code for BFS, but after showing the result it crashes. I don't find anything wrong with my code. Here's the code
I think this is pointer related problem, because I got the same problem because I did not allocate memeory. Visual studio debugger shows there's a problem in this function.
int getAdjUnvisitedVertex(int vertexIndex) {
for (int j = 0; j < vertexCount; ++j) {
if (adjMatrix[vertexIndex][j] == 1 && vertices[j]->visited == false) { //Is there anything wrong in this logic?
return j;
}
}
return 0;
}
Any help will be much appreciated. This is my full code. DFS is working, its BFS that causing problems.
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<malloc.h>
#include<stdbool.h>
#define SIZE 50
int vertexCount;
struct vertex {
char label;
bool visited;
};
struct vertex *vertices[SIZE];
int adjMatrix[SIZE][SIZE];
int stack[SIZE];
int queue[SIZE];
int top = -1;
int front = -1;
int rear = -1;
void menu();
void addVertex();
void addEdges();
void push(char);
void pop();
void enQueue(char);
int deQueue();
void displayAdjMatrix();
void displayVertex(int);
int getAdjUnvisitedVertex(int);
void DFS();
void BFS();
bool isStackEmpty();
bool isQueueEmpty();
int main() {
menu();
addVertex();
addEdges();
puts("\nDepth first search:");
DFS();
puts("\nBreadth first search:");
BFS();
}
void menu() {
puts("How many vertices?");
scanf("%d", &vertexCount);
}
void addVertex() {
int ascii = (int)'A';
puts("Vertices are:");
for (int i = 0; i < vertexCount; ++i, ++ascii) {
vertices[i] = (struct vertex*)calloc(vertexCount, sizeof(struct vertex));
vertices[i]->label = (char)ascii;
vertices[i]->visited = false;
printf("%c ", vertices[i]->label);
}
}
void addEdges() {
puts("\nEnter edges:");
for (int i = 0; i < vertexCount; ++i) {
for (int j = i + 1; j < vertexCount; ++j) {
printf("%c->%c: ", vertices[i]->label, vertices[j]->label);
scanf("%d", &adjMatrix[i][j]);
if (adjMatrix[i][j] == 1) {
adjMatrix[j][i] = 1;
}
}
}
}
void displayAdjMatrix() {
for (int i = 0; i < vertexCount; ++i) {
for (int j = 0; j < vertexCount; ++j) {
printf("%d\t", adjMatrix[i][j]);
}
puts("");
}
}
void push(char data) {
if (top == SIZE - 1) {
puts("Overflow!");
return;
}
stack[++top] = data;
}
void pop() {
if (top == -1) {
puts("Underflow!");
return;
}
stack[top--];
}
int getAdjUnvisitedVertex(int vertexIndex) {
for (int j = 0; j < vertexCount; ++j) {
if (adjMatrix[vertexIndex][j] == 1 && vertices[j]->visited == false) {
return j;
}
}
return 0;
}
void displayVertex(int vertexIndex) {
printf("%c ", vertices[vertexIndex]->label);
}
void DFS() {
vertices[0]->visited = true;
displayVertex(0);
push(0);
while (!isStackEmpty()) {
int unvisitedVertex = getAdjUnvisitedVertex(stack[top]);
if (!unvisitedVertex) {
pop();
}
else {
vertices[unvisitedVertex]->visited = true;
displayVertex(unvisitedVertex);
push(unvisitedVertex);
}
}
for (int i = 0; i < vertexCount; ++i) {
vertices[i]->visited = false;
}
}
bool isStackEmpty() {
if (top == -1) {
return true;
}
return false;
}
bool isQueueEmpty() {
if (front == -1) {
return true;
}
return false;
}
void enQueue(char data) {
if (rear == SIZE - 1) {
puts("Queue is full!");
return;
}
if (front == -1) {
++front;
}
queue[++rear] = data;
}
int deQueue() {
if (front == - 1) {
puts("Queue is empty!");
return 0;
}
int deleted;
deleted = queue[front];
queue[front++] = 0;
return deleted;
}
void BFS() {
vertices[0]->visited = true;
displayVertex(0);
enQueue(0);
while (!isQueueEmpty()) {
int temp = deQueue();
int unvisitedVertex;
while(unvisitedVertex = getAdjUnvisitedVertex(temp)) {
vertices[unvisitedVertex]->visited = true;
displayVertex(unvisitedVertex);
enQueue(unvisitedVertex);
}
}
for (int i = 0; i < vertexCount; ++i) {
vertices[i]->visited = false;
}
}

Segmentation fault (core dumped) in c linux

My code returns a Segmentation fault and I do not know why.
It prints part of the output and gives a Segmentation fault. I cannot find the error.
It should number words & sentences & paragraphs & top words with highest frequencies in order.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "preprocessing_header.h"
#include "calculations_header.h"
int main(int argc, char* argv[])
{
if (argc != 3)
{
printf("Error: number of arguments must be 3\n");
exit(-1);
}
char* file_name;
file_name = argv[1];
int N = atoi(argv[2]);
char final_string[number_of_char_aprox];
char *string;
string = Preprocessing(file_name);
strcpy(final_string, string);
printf("%s\n", final_string);
calculations(final_string, N);
return 0;
}
/* The main function for sorting the file in the appropriate form */
char* Preprocessing(char file_name[])
{
char final_string[number_of_char_aprox];
char *string;
FILE *in = fopen(file_name,"r");
if (in == NULL) {
printf("Not found");
exit(-1);
}
Head lines_list = (Head)malloc(sizeof(struct line)); // list of the lines from the file
lines_list->next = NULL;
readFile(lines_list, in); // read the file and put each line in a node in the list
fclose(in);
remove_spaces(lines_list); // make only a single space after each word in the lines
to_lower(lines_list); // convert all charachter in each line to small letter
punctuation_marks(lines_list); // make a one single space after punctuation marks
lines_list = reverse_list(lines_list); // reverse the list
string = concat_string(lines_list);// put all line in one string
strcpy(final_string, string);
free_list(lines_list);
string = Atter_punctuation_marks(final_string); // make letters atter punctuation
strcpy(final_string, string);
string = add_point(final_string);
strcpy(final_string, string);
//string = remove_line_feed(final_string);// remove line feed in the same paragraph
//strcpy(final_string, string);
string = final(final_string);// the final form of the string
return string;
}
/* Make the needed calculations */
void calculations(char final_string[],int N)
{
int i;
int number_of_words;
int number_of_sentences;
int number_of_paragraphs;
Head_word lw[26];// array of linked list of the words
for(i = 0; i < 26; i++)
{
lw[i] = (Head_word)malloc(sizeof(struct word));
lw[i]->next = NULL;
}
number_of_words = count_words(final_string, lw);
number_of_sentences = count_sentences(final_string);
number_of_paragraphs = count_paragraphs(final_string);
printf("\n******************************************************\n\n");
printf("\n******************************************************\n\n");
printf("number_of_words = %d\n",number_of_words);
printf("\nnumber_of_sentences = %d\n",number_of_sentences);
printf("\nnumber_of_paragraphs = %d\n",number_of_paragraphs);
printf("\n******************************************************\n\n");
printf("Top %d words with highest frequencies in order : \n\n",N);
Head_word most_repeated[N];
most_repeated_words(lw,most_repeated, N);
free_lists_words(lw);
sort(most_repeated,N); //sort the most frequently used words
print_most_repeated_words(most_repeated, N);
}
here is preprocessing_header.h
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "preprocessing_header.h"
/*read the file and put each line in a node in a linked list*/
void readFile(Head l, FILE *in)
{
position p;
while(!feof(in))
{
p = (position)malloc(sizeof(struct line));
fgets(p->string,10000,in);
p->next = l->next;
l->next = p;
}
}
/* make only a single space after each word in the lines*/
void remove_spaces(Head l)
{
int i = 0;
int j = 0;
int k;
position p = l->next;
char temp[1000];
while(p != NULL)
{
i = 0;
j = 0;
for(k = 0;k<1000;k++)
{
temp[k] = '\0';
}
while(p->string[i] != '\0')
{
if(p->string[0] == ' ')
{
while(p->string[i] == ' ')
i++;
}
while(p->string[i] != ' ' && p->string[i] != '\0')
{
temp[j] = p->string[i];
i++;
j++;
}
while(p->string[i] == ' ')
{
i++;
}
temp[j++] = ' ';
}
temp[j+1] = '\0';
strcpy(p->string,temp);
p = p->next;
}
}
void to_lower(Head l)
{
position p = l->next;
int i = 0;
while(p != NULL)
{
i = 1;
while(p->string[i] != '\0')
{
if(p->string[i]>= 65 && p->string[i]<=90)
{
p->string[i]+= 32;
}
i++;
}
p = p->next;
}
}
/*make a one single space after punctuation marks and no space before it*/
void punctuation_marks(Head l)
{
position p = l->next;
int i,j,k;
char temp[1000];
while(p != NULL)
{
i = 0;
j = 0;
for(k = 0;k<1000;k++)
{
temp[k] = '\0';
}
while(p->string[i] != '\0')
{
if(p->string[i] == '.' || p->string[i] == ',' || p->string[i] == ';' ||p->string[i] == '?' )
{
if(p->string[i-1] == ' ')
{
temp[--j] = p->string[i];
}
if(p->string[i+1] != ' ')
{
temp[j++] = p->string[i++];
temp[j++] = ' ';
}
}
temp[j++] = p->string[i++];
}
temp[j+1] = '\0';
strcpy(p->string,temp);
p = p->next;
}
}
/*reverse linked list*/
Head reverse_list(Head l)
{
Head l2 = (Head)malloc(sizeof(struct line));
l2->next = NULL;
position p = l->next;
while(p != NULL)
{
l->next = p->next;
p->next = l2->next;
l2->next = p;
p = l->next;
}
free(l);
return l2;
}
/* put all line in one string*/
char* concat_string(Head l)
{
char *temp = malloc(number_of_char_aprox*sizeof(char));
int k;
for(k = 0;k<number_of_char_aprox;k++)
{
temp[k] = '\0';
}
position p = l->next;
while(p != NULL)
{
strcat(temp,p->string);
p = p->next;
}
return temp;
}
/*remove line feed in the same paragraph*/
char* remove_line_feed(char final_string[])
{
int i;
char *temp = malloc(number_of_char_aprox*sizeof(char));
for(i = 0;i<number_of_char_aprox;i++)
{
temp[i] = '\0';
}
int j=0;
//printf("%s",final_string);
for(i = 0;i<strlen(final_string);i++)
{
if(final_string[i] == 13)
{
if(final_string[i-2]== '.')
{
temp[j++] = final_string[i++];
}
else
{
i = i+2;
}
}
temp[j++] = final_string[i];
}
return temp;
}
/*make letters atter punctuation marks capital letters*/
char* Atter_punctuation_marks(char string[])
{
char *temp = malloc(number_of_char_aprox*sizeof(char));
int k;
int i=0,j=0;
for(k = 0;k<number_of_char_aprox;k++)
{
temp[k] = '\0';
}
while(string[i] != '\0')
{
if(string[i] == '.' || string[i] == ';' ||string[i] == '?' || string[i] == '\n' )
{
if(string[i+2] >= 97 && string[i+2]<=122)
{
temp[j++] = string[i++];
temp[j++] = string[i++];
temp[j++] = string[i++]-32;
}
}
if(string[i] == 'i' && string[i-1] == ' ' && string[i+1] == ' ')
{
string[i]-= 32;
}
temp[j++] = string[i++];
}
temp[j+1] = '\0';
return temp;
}
char* to_lower_case(char string[])
{
int i = 0;
while(string[i] != '\0')
{
if(string[i]>= 65 && string[i]<=90)
{
string[i]+= 32;
}
i++;
}
return string;
}
/*make only one empty line between paragraphs and return the stirng with the final form*/
char* final(char s[])
{
int i;
int j = 0;
char *temp = malloc(number_of_char_aprox*sizeof(char));
for(i = 0;i<number_of_char_aprox;i++)
{
temp[i] = '\0';
}
j = 0;
for(i = 0;i<strlen(s);i++)
{
if(s[i]== ' ' && s[i-1] == '\n')
{
i++;
}
if(s[i] == '\n')
{
while(s[i+3] == '\n')
{
i+=3;
}
temp[j++] = '\n';
}
temp[j++] = s[i];
}
return temp;
}
void free_list(Head l)
{
position temp;
while(l->next != NULL)
{
temp = l->next;
l->next = temp->next;
free(temp);
}
free(l);
}
char* add_point(char s[])
{
int i,j=0;
char *temp = (char*)malloc(10000*sizeof(char));
for(i=0;i<strlen(s);i++)
{
if(s[i]==13 && !strchr(".,?;",s[i-1]) && !strchr(".,?;",s[i-2]) )
{
temp[j++] = '.';
i++;
}
temp[j++] = s[i];
}
return temp;
}
and here is my calculations_header.h
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "calculations_header.h"
/*count the number of words in the string and put the word in an array of linked list
which index is the first letter in each word eg (a)re in the linked list of index 0.
The word will be put in the list if it is a new word only if it is repeated the counter
of the node containing the word will increase*/
int count_words(char string[],Head_word lw[])
{
int i,j,k=0, length=0, count=0;
char word[50];
position_word p;
for(j=0;j<50;j++)
{
word[j] = '\0';
}
length= strlen(string);
for (i=0; i<length; i++)
{
if((string[i] == ' ' && string[i+1] != ' ') || string[i] == 10 || string[i] == 13)
{
if(!is_exist(word,lw[word[0]-97]))
{
//printf("%s\n",word);
p = (position_word)malloc(sizeof(struct word));
strcpy(p->string,word);
p->counter = 1;
p->next = lw[word[0]-97]->next;
lw[word[0]-97]->next = p;
}
//printf("%d",word[1]);
//printf("%s\n",word);
if((word[1]>=97 && word[1]<=123) || (word[1]>=65 && word[1]<=91))
count++;
k = 0;
for(j=0;j<50;j++)
{
word[j] = '\0';
}
}
if((string[i]>=97 && string[i]<=123) || (string[i]>=65 && string[i]<=91))
{
if(string[i] >= 65 && string[i] <= 90)
{
word[k] = string[i]+32;
}
else
{
word[k] = string[i];
}
k++;
}
}
return count;
}
/*test if the word is exist in the list*/
int is_exist(char word[], Head_word l)
{
int flag = 0;
Head_word p;
p = l->next;
while(p!=NULL)
{
if(strcmp(word,p->string) == 0)
{
p->counter++;
flag = 1;
break;
}
p = p->next;
}
return flag;
}
/*decide which N word are the most frequently used*/
void most_repeated_words(Head_word lw[],Head_word most_repeated[],int n)
{
int i,j;
int least;
position_word p;
for(i = 0 ;i<n;i++)
{
most_repeated[i] = (Head_word)malloc(sizeof(struct word));
most_repeated[i]->next = NULL;
most_repeated[i]->counter = 0;
}
for(i = 0;i<26;i++)
{
p = lw[i]->next;
while(p!= NULL)
{
for(j = 0 ; j<n;j++)
{
if(p->counter > most_repeated[j]->counter)
{
least = get_least(most_repeated,n);
strcpy(most_repeated[least]->string,p->string);
most_repeated[least]->counter = p->counter;
break;
}
}
p = p->next;
}
}
}
/*get the least repeated word in the array*/
int get_least(Head_word l[],int n)
{
int i;
int least = 10000;
int index;
for(i = 0;i<n;i++)
{
if(l[i]->counter < least)
{
least = l[i]->counter;
index = i;
}
}
return index;
}
/*count the number of sentences*/
int count_sentences(char s[])
{
int i;
int count = 0;
for(i=0;i<strlen(s);i++)
{
if(strchr(".?;,",s[i]))
{
count++;
}
}
return count;
}
/*count the number of paragraphs*/
int count_paragraphs(char s[])
{
int i;
int count = 0;
for(i=0;i<strlen(s);i++)
{
if(s[i] == '\n' && s[i+1]>=65 && s[i+1]<=90 )
{
count++;
}
}
return count+1;
}
/*print the most repeated N words and how many time it has been used*/
void print_most_repeated_words(Head_word most_repeated[],int N)
{
int i;
for(i=0;i<N;i++)
{
printf("%s\t%d\n",most_repeated[i]->string,most_repeated[i]->counter);
}
}
/*sort the N most frequently used word in ascending order */
void sort(Head_word most_repeated[],int N)
{
int i,j;
Head_word temp = (Head_word)malloc(sizeof(struct word));
for (i = 0 ; i < ( N - 1 ); i++)
{
for (j = 0 ; j < N - i - 1; j++)
{
if (most_repeated[j]->counter < most_repeated[j+1]->counter)
{
temp->counter = most_repeated[j]->counter;
strcpy(temp->string,most_repeated[j]->string);
most_repeated[j]->counter = most_repeated[j+1]->counter;
strcpy(most_repeated[j]->string,most_repeated[j+1]->string);
most_repeated[j+1]->counter = temp->counter;
strcpy(most_repeated[j+1]->string,temp->string);
}
}
}
}
void free_lists_words(Head_word l[])
{
int i;
position_word temp;
for(i = 0;i<26;i++)
{
while(l[i]->next != NULL)
{
temp = l[i]->next;
l[i]->next = temp->next;
free(temp);
}
free(l[i]);
}
}
and here is my calculations_header.h:
#ifndef CALCULATIONS_HEADER_H_INCLUDED
#define CALCULATIONS_HEADER_H_INCLUDED
/*This structure to save each word to make calculations*/
typedef struct word *ptr_word;
struct word
{
char string[50];
int counter;
ptr_word next;
};
typedef ptr_word Head_word;
typedef ptr_word position_word;
/*calculation functions prototypes*/
char* Preprocessing(char []);
void calculations(char [],int);
int count_words(char [],Head_word[]);
int is_exist(char [], Head_word);
void print(Head_word[]);
void most_repeated_words(Head_word [],Head_word[],int);
int get_least(Head_word[],int);
int count_paragraphs(char []);
int count_sentences(char []);
void print_most_repeated_words(Head_word[],int);
void sort(Head_word [],int);
void free_lists_words(Head_word []);
#endif // CALCULATIONS_HEADER_H_INCLUDED
and here is my preprocessing_header.h:
#ifndef PREPROCESSING_HEADER_H_INCLUDED
#define PREPROCESSING_HEADER_H_INCLUDED
#define number_of_char_aprox 10000
/*This structure for saving each line read from the file*/
typedef struct line *ptr;
struct line
{
char string[10000];
ptr next;
};
typedef ptr Head;
typedef ptr position;
/*preprocessing functions prototypes*/
void readFile(Head,FILE *);
void remove_spaces(Head);
void to_lower(Head);
void punctuation_marks(Head );
Head reverse_list(Head);
char* concat_string(Head);
void free_list(Head);
char* Atter_punctuation_marks(char []);
char* remove_line_feed(char []);
char* final(char []);
#endif // PREPROCESSING_HEADER_H_INCLUDED
it give me just warning on line 60 in main( string = add_point(final_string); )
i think the function calculations it make the error.
char final_string[number_of_char_aprox];
What's number_of_char_aprox? You don't even declare it as a variable. A segmentation fault happens when you are trying to access not assigned memory. As there is no value for that variable (actually there is no variable at all), it probably defaults to 0 (or NULL, or imploding universe, who knows), and when you use it to reserve memory for final_string you got your segmentation fault.
By the way, DDD is a great graphical debugger.

Resources