Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. //returns the length of a string
  5. int getStringLen(char *str)
  6. {
  7. int count = 0;
  8.  
  9. while (*(str + count) != '.')
  10. {
  11. count++;
  12. }
  13. return count;
  14. }
  15.  
  16. //calculates the minimun index by the string length
  17. int getStartIndex(int len)
  18. {
  19. int index = 0;
  20. for (int i = 0; i < len; i++)
  21. {
  22. index += pow(2, i);
  23. }
  24.  
  25. return index;
  26. }
  27.  
  28. //returns the index associated with the binary "ab" string taken as parameter
  29. int enumerate(char *string)
  30. {
  31. char *characters;
  32. char *index = string;
  33. int num = 0;
  34. int i = 1;
  35. int len = getStringLen(string);
  36. int start = getStartIndex(len);
  37.  
  38. if (string == NULL)
  39. return num;
  40.  
  41. while (*index != '.')
  42. {
  43. int increment = (*index == 'a') ? 0 : pow(2, len - i);
  44. num += increment;
  45. i++;
  46. index++;
  47. }
  48.  
  49. return start + num;
  50. }
  51.  
  52. int main()
  53. {
  54. int index = enumerate("bbb.");
  55. printf("%d\n", index);
  56. return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement