Hello, I'm trying to split a string into 4 separate strings using a delimiter "\n"
The initial string is:
"/usr/bin/ssh \n * \n 147.188.195.15, 147.188.193.15, 147.188.193.16 \n 22"
I would like it to be split up into four different char's so that
char *programnames = "/usr/bin/ssh \n"
char *uids = " * \n"
char *destips = "147.188.195.15, 147.188.193.15, 147.188.193.16 \n"
char *ports " 22"
Hopefully you should see the pattern I'm aiming for. The original string first starts off like "/usr/bin/ssh * 147.188.195.15, 147.188.193.15, 147.188.193.16 22" but i have to split it up using a BNF grammar where there are n number of spaces but no , before the space and I've done this by replacing the last space in n spaces with "\n". Thanks in advance.
Below is the code I have produced so far:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char *programnames;
char *uids;
char *destips;
char *ports;
char *result = "/usr/bin/ssh * 147.188.195.15, 147.188.193.15, 147.188.193.16 22";
char src[200];
strncpy(src, result, 200);
int i = 0;
while (i <= 200) {
//clause to not new line if there was a , comma followed by n number of spaces
if (((src[i] == ' ') && ((((src[(i - 1)] != ',') && (src[(i - 1)] != '\n')) && (src[(i - 1)] != ' ')) && (isalnum(src[(i + 1)]) || (src[(i + 1)] == '*'))))) {
src[i] = '\n';
}
//clause to new line if there are n number of spaces between a letter, numer and *
if (src[i] == ' ' && src[(i + 1)] == '*') {
src[i] = '\n';
}
if (src[i] == '*' && src[(i + 1)] == ' ' && src[(i + 2)] == ' ') {
src[i + 1] = '\n';
}
if (isdigit(src[i]) && src[(i + 1)] != ',' && src[(i + 2)] == ' ') {
src[i + 1] = '\n';
}
src[i] = src[i];
i++;
}
printf("LINE SPLIT RESULTS USING BNF GRAMMAR:\n %s", src);
printf("\n");
printf("TOKENS:\n");
char basetokens[200];
strncpy(basetokens, src, 200); //copy the result of the src into a basetokens to be processed into separate tokens
char *tokens;
tokens = strtok(basetokens, "\n");
//printf(tokens);
return 0;
}