fork(1) download
  1. #include <stdio.h>
  2.  
  3. // adding const saves you from accidental mutation
  4. int hash(char const * const word) {
  5. int h = 0; // why 1 as initial value?
  6. int i, j;
  7.  
  8. char *A;
  9. char *a;
  10. A = malloc(26);
  11. a = malloc(26);
  12.  
  13. for (i = 0; i < 26; i++) {
  14. A[i] = (char)(i + 65);
  15. a[i] = (char)(i + 97);
  16. }
  17.  
  18. for (i = 0; i < strlen(word); i++) {
  19. for (j = 0; j < 26; j++) {
  20. if (word[i] == A[j] || word[i] == a[j]) {
  21. h = h + j + 1; // a is at index 0
  22. break;
  23. }
  24. }
  25. }
  26. free(A);
  27. free(a);
  28. return h;
  29. }
  30.  
  31. int main(void) {
  32. printf ("%d %d\n", hash("aaa"), hash("ab"));
  33. return 0;
  34. }
Success #stdin #stdout 0s 2184KB
stdin
Standard input is empty
stdout
3 3