I need to count the number of times a certain char has performed in a
string.
Means to the string:
cefgacaffe
output:
char occurence
a 2
c 2
e 2
f 3
g 1
This is what I've been up to:
Code:
#include<stdio.h>
#include<malloc.h>
typedef struct tree_el {
char data;
int counter;
struct tree_el * right, * left;
}TREE;
typedef TREE *node;
void insert(node * tree, node* item)
{
if(!(*tree)) {
*tree = *item;
return;
}
if((*item)->data<(*tree)->data)
insert(&(*tree)->left, item);
else if((*item)->data>(*tree)->data)
insert(&(*tree)->right, item);
}
char intrav (node tree, char data)
{
if (tree!=NULL)
{
if (intrav(tree->left, data)==data)
++(tree->counter);
printf ("%c | %d\n", tree->data, tree->counter);
intrav(tree->right, data);
}
return data;
}
void CountOcrnc (node tree)
{
node temp;
temp=(node)malloc(sizeof(node));
temp->left= temp->right= NULL;
temp=tree;
while (tree!=NULL)
{
intrav(tree, temp->data);
tree=tree->left;
tree=tree->right;
}
}
int main(void)
{
char ltrs[20]={"cefgacaffe0"};
int count=0;
node curr=NULL, root= NULL;
while ( ltrs[count] != '0' )
{
curr=(node) malloc (sizeof (TREE));
curr->left= curr->right= NULL;
curr->counter=0;
curr->data=ltrs[count];
insert(&root, &curr);
++count;
}
CountOcrnc (root);
return 1;
}
Guess I don't get expected output, cause of a mass I'm facing in intrav function.
Please help me organize it.
TIA,
Ronen