#include <stdio.h>
#include <stdint.h>
#include <string.h>
void split_string(const char *str, char delimiter, char tokens[10][20], uint8_t *count) {
// Your logic here
int j=0;
int k=0;
int i=0;
while(1){
if(str[i] == delimiter || str[i] == '\0'){
int q;
for(q=0;j<i;j++){
tokens[k][q++] = str[j];
}
tokens[k][q] = '\0';
k++;
j = i+1;
if(str[i] == '\0'){
break;
}
}
i++;
}
*count = k;
// int i = 0, j = 0, k = 0; // i: current position in str, j: position in the current token, k: number of tokens
// while (str[i] != '\0') { // Traverse the string until the end
// if (str[i] == delimiter) { // Delimiter found, end the current token
// tokens[k][j] = '\0'; // Null-terminate the current token
// k++; // Increment token counter
// j = 0; // Reset the index for the next token
// } else {
// tokens[k][j] = str[i]; // Add the current character to the token
// j++; // Move to the next character in the token
// }
// i++; // Move to the next character in the input string
// }
// // Null-terminate the last token
// tokens[k][j] = '\0';
// *count = k + 1; // Set the count of tokens
}
int main() {
char str[101];
char delimiter;
fgets(str, sizeof(str), stdin);
scanf(" %c", &delimiter);
// Remove newline
uint8_t i = 0;
while (str[i]) {
if (str[i] == '\n') { str[i] = '\0'; break; }
i++;
}
char tokens[10][20];
uint8_t count = 0;
split_string(str, delimiter, tokens, &count);
for (uint8_t i = 0; i < count; i++) {
printf("%s\n", tokens[i]);
}
return 0;
}
Input
cmd1,cmd2,cmd3 ,
Expected Output
cmd1 cmd2 cmd3