Created
March 21, 2021 02:08
-
-
Save alsamitech/a5cca0af0593545823f3159820c72b50 to your computer and use it in GitHub Desktop.
Converts a string into a heap-allocated array of integers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| long unsigned int byte_instances(char* bytes, long unsigned int len, char byte){ | |
| long unsigned int inst=0; | |
| for(long unsigned int i=0;i<len;i++){ | |
| if(bytes[i]==byte){ | |
| inst++; | |
| } | |
| } | |
| return inst; | |
| } | |
| long unsigned int char_instances(char* bytes, char byte){ | |
| long unsigned int inst=0; | |
| for(long unsigned int i=0;bytes[i]!=0;i++){ | |
| if(bytes[i]==byte){ | |
| inst++; | |
| } | |
| } | |
| return inst; | |
| } | |
| long unsigned int until_byte(char* bytes, long unsigned int n,char byte){ | |
| long unsigned int until; | |
| for(until=0;until<n;until++){ | |
| if(bytes[until]==byte){ | |
| return until; | |
| } | |
| } | |
| return n; | |
| } | |
| long unsigned int until_char(char* bytes, char byte){ | |
| long unsigned int until; | |
| for(until=0;bytes[until]!=0;until++){ | |
| if(bytes[until]==byte){ | |
| return until; | |
| } | |
| } | |
| return until; | |
| } | |
| int* str_to_intarr(char* str, char sep, long unsigned int* len){ | |
| long unsigned int i; | |
| long unsigned int inst=char_instances(str, sep); | |
| int* ints=(int*)malloc(inst*sizeof(int)); | |
| long unsigned int added_i=0; | |
| for(i=0;i<inst;i++){ | |
| ints[i]=atoi(str+added_i); | |
| added_i+=(until_char(str, sep)+1); | |
| } | |
| ints[i]=atoi(str+added_i); | |
| *len=inst; | |
| return ints; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment