Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- //returns the length of a string
- int getStringLen(char *str)
- {
- int count = 0;
- while (*(str + count) != '.')
- {
- count++;
- }
- return count;
- }
- //calculates the minimun index by the string length
- int getStartIndex(int len)
- {
- int index = 0;
- for (int i = 0; i < len; i++)
- {
- index += pow(2, i);
- }
- return index;
- }
- //returns the index associated with the binary "ab" string taken as parameter
- int enumerate(char *string)
- {
- char *characters;
- char *index = string;
- int num = 0;
- int i = 1;
- int len = getStringLen(string);
- int start = getStartIndex(len);
- if (string == NULL)
- return num;
- while (*index != '.')
- {
- int increment = (*index == 'a') ? 0 : pow(2, len - i);
- num += increment;
- i++;
- index++;
- }
- return start + num;
- }
- int main()
- {
- int index = enumerate("bbb.");
- printf("%d\n", index);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement