As a part of a program, this function receives a pointer to a string and check if it's a valid row of a markdown table, of which data can be extracted.
If it recognizes the table-header, the .md-syntax division between header and table body or a empty row, it may return false, else true.
example:
| Lebensmittel/Gericht | Gewicht in gr | kcal | ->false
| -------------------- | ------------- | ---- | ->false
| Wildkräutersalat | 42 | 8 | ->true
| Rucola | 40 | 10 | ->true
| | | | ->false
| Brot | 73 | 161 | ->true
this is my approach so far:
bool check_line(char* line) {
puts(line); // console output for debugging
if(strcmp(line,"| Lebensmittel/Gericht | Gewicht in gr | kcal |") != 0) { // check for specific header
return false;
}
if(line[0] == '|' //check for underline under header
&& line[1] == ' '
&& line[2] == '-'
&& line[3] == '-'
&& line[4] == '-'
) {
return false;
}
// check for empty table rows
char* start;
char* end;
if(start = strstr(line,'|')){
start++;
if(end = strstr(start,'|')){
for(size_t i; line[i] != '|'; i++ ){ //Iterate between the two '|' if the string is blank-space
if( line[i] != ' ');
return true; //return true if there are chars between two '|'
}
return false; //return false if the string between the two '|' contains ony blank-space
}
}
return false;
}
I have used this approach to check for an empty row between the '|' chars. But it is not working correctly.
By changing the code to split the char* with the delimeter "|", you then can check if the second and third elements of the row only consist of numbers and if you only have 3 elements. If so, the line is valid: