#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct charact {
char ch;
int occurs;
struct charact *next;
};
typedef struct charact Char;
typedef Char *ListofChar;
typedef Char *CharNode_ptr;
void letters(char name[50], ListofChar *chars_ptr);
void report(ListofChar chars);
Char *create_node(char ch);
int main(void) {
char name[50];
ListofChar chars = NULL;
scanf("%49s", name);
letters(name, &chars);
report(chars);
return 0;
}
Char *create_node(char ch) {
CharNode_ptr newnode_ptr;
newnode_ptr = malloc(sizeof(Char));
newnode_ptr->ch = ch;
newnode_ptr->occurs = 0;
newnode_ptr->next = NULL;
return newnode_ptr;
}
void letters(char name[50], ListofChar *lst_ptr) {
int str_lenth = strlen(name);
*lst_ptr = create_node(name[0]);
CharNode_ptr current = *lst_ptr;
for (int i = 1; i < str_lenth; i++) {
CharNode_ptr new = create_node(name[i]);
current->next = new;
current = new;
}
return;
}
void report(ListofChar chars) {
int apostasi = 0;
int epanalipsi = 0;
for (CharNode_ptr current = chars; current != NULL; current = current->next) {
if (current->next != NULL && current->next->ch == current->ch) {
apostasi++;
epanalipsi = apostasi;
}
else {
apostasi = 0;
epanalipsi = 0;
}
current->occurs = apostasi;
printf("%c: %d\n", current->ch, current->occurs);
}
}
I want to make a program that if a letter repeats it self after the first time then I get the distance between them if not then I get a 0.
It know works if the distance between the letter is only 1.
For example:
Input:hello
h:0
e:0
l:1
l:0
o:0
Which is correct.
But for input helol
I get:
h:0
e:0
l:0
o:0
l:0
The correct would be:
h:0
e:0
l:2
o:0
l:0
How do I fix it by only messing with the letters and report functions?