Here is an example of how to count occurrences of the same character present in a string in C
. (This example does not consider 'A' and 'a' chars equal').
#include "stdio.h"
#include "stdlib.h"
int isAlpha(char c) {
if(c >= 48 && c <= 57) { // [0-9]
return 1;
}
if(c >= 65 && c <= 90) { // [A-Z]
return 1;
}
if(c >= 97 && c <= 122) { // [a-z]
return 1;
}
return 0;
}
int main(void) {
int *map = (int *)calloc(128, sizeof(int));
int i = 0;
char *test = "banana";
char tmp;
do {
tmp = test[i++];
if (isAlpha(tmp)) {
map[tmp]++;
}
} while (tmp != '\0');
for (i = 0; i < 128; i++) {
if (map[i] != 0) {
printf("%c = %d\n", i, map[i]);
}
}
return 0;
}
You can use a 0 initialized vector in all 128 houses and then for each letter
l
docount[l]++
– Jefferson Quesado
Without using native C function, or without creating a function to do so?
– Sveen
Stackoverflow does not distribute code, just fix or review
– Sveen
@Jeffersonquesado to using 2 is to make the comparison of one inside the other to and adding to see if you can pull the sum, it seems that is flowing, the way you sent there! Thank you
– Adriel Gama
I’m pretty sure this is duplicated, but I haven’t been able to find it yet. I’ve seen this question a few times around
– Isac