I can normally extract the data of array as:
#include <stdio.h>
int main()
{
char str[100] = "The quick brown fox jumps over the lazy dog";
char *c = (str + 29);
printf("%s\n", c);
return 0;
}
But if I have to extract data from the entire unsorted and n-sized array where n value may be more than 70 as like:
unsigned char buffer[n] = "00,00,00,11,30,45.00,a,1.1,b,d,16,ABC,98.0";
unsigned char buffer1[n] = "A,3,22,18,21,06,03,09,24,15,,,,,2.5,1.6,1.9*3E";
How can I extract some specific data like 45.00 from this unsorted array?
The split() method used in this suggestion was developed by @hmjd to answer the referenced post; I have made arragments on the split() method that will not change the result. The solution is accomplished in five steps marked in the source code:
split() method, the character array is split into tokens according to the ',' character.**tokens pointer.getIndex() method searches for a special character array among the tokens and returns the index if found.#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* (1) Splits a string by separator character. */
char** split(char* a_str, const char a_delim);
/* (2) Returns the total number of parts. */
size_t getSize(char **tokens);
/* (3) Prints tokens. */
void print(char **tokens);
/* (4) Searches for a data in tokens and returns the index of the result. */
int getIndex(char **tokens, char *search, size_t size);
int main()
{
unsigned char buffer[] = "00,00,00,11,30,45.00,a,1.1,b,d,16,ABC,98.0";
char search[] = "45.00";
char **tokens = split(buffer, ','); /* 1 */
size_t size = getSize(tokens); /* 2 */
print(tokens); /* 3 */
size_t index = getIndex(tokens, search, size); /* 4 */
if(index >= 0)
printf("Index: %ld Result: %s", index, *(tokens + index)); /* 5 */
return 0;
}
char** split(char* a_str, const char a_delim)
{
char** result = 0;
size_t count = 0;
char* tmp = a_str;
char* last_comma = 0;
char delim[2];
delim[0] = a_delim;
delim[1] = 0;
while (*tmp)
{
if (a_delim == *tmp)
{
count++;
last_comma = tmp;
}
tmp++;
}
count += last_comma < (a_str + strlen(a_str) - 1);
count++;
result = malloc(sizeof(char*) * count);
if (result)
{
size_t idx = 0;
char* token = strtok(a_str, delim);
while (token)
{
*(result + idx++) = strdup(token);
token = strtok(0, delim);
}
*(result + idx) = 0;
}
return result;
}
int getIndex(char **tokens, char *search, size_t size)
{
int index;
if(tokens)
for(index = 0 ; *(tokens + index) ; index++)
if(strcmp(search, *(tokens + index)) == 0)
return index;
return -1;
}
void print(char **tokens)
{
if (tokens)
for (size_t index = 0; *(tokens + index); index++)
printf("[%ld]: %s\t", index, *(tokens + index));
printf("\n");
}
size_t getSize(char **tokens)
{
size_t counter = 0;
while(*(tokens + counter))
++counter;
return counter;
}
This source code produces the following output:
[0]: 00 [1]: 00 [2]: 00 [3]: 11 [4]: 30 [5]: 45.00 [6]: a [7]: 1.1 [8]: b [9]: d [10]: 16 [11]: ABC [12]: 98.0
Index: 5 Result: 45.00
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With