hello everyone, im new in here and im having a headache with my program,the thing is that i need to get a input from the keyboard and then separate it using strtok but have to separate the tokens using 4 diferent cases and in each case i need to print the result and save it to a string like this:
input String : Label Instruction #50,Y; Label <with>
and the output should look like this:
Label: Label
Instruction: Instruction
Character 1: #50
Character 2: Y
Comentaries: Label <with>
also it has to be able to reconize if a instruction is missed like this:
Input String: adda
Output String
Label: -----
Instruction: adda
Character 1: -----
Comentaries: -----
My code can accept the first and correct instruction but when i type a incorrect one like in the second input it ignores it and continue like the first atempt just ading <null> sometimes,i have tryed to use if to be able to separate each token with its delimeter but everitime i compile it it ignores the if statement no matter what argument i gave it i dont know what else to do
Heres my code
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
int main() { char word[256];
fgets(word,256,stdin);
char *token;
while (token != NULL)
{
char delimiter[]="\n , ;";
token=strtok (word,delimiter);
//if(token != "\n")
//{
//char delimiter[]="\n , ;";
//token=strtok (word,delimiter);
//if (delimiter != " "||"\t" || "_")
printf("Label \"%s\"\n", token);
token = strtok (NULL, "\n , ;"); //(NULL, "_,.-")
//}
//token=strtok (word,delimiter);
//}
//printf("Label ----------\n");
//if (delimiter != "\n"||"\t")
//{
printf("Instruction \"%s\"\n", token);
token = strtok (NULL, "\n , ;"); //(NULL, "_,.-")
//}
printf("Character \"%s\"\n", token);
token = strtok (NULL, "\n , ;"); //(NULL, "_,.-")
printf("Character 2 \"%s\"\n", token);
token = strtok (NULL, "\n , ;"); //(NULL, "_,.-")
printf("Comentaries \"%s\"\n",token);
token = strtok (NULL, ";");
// printf("Character 2\"%s\"\n", token);
// token = strtok (NULL, "\n , ;"); //(NULL, "_,.-")
token = NULL;
}
//token = NULL;
//printf("Comentaries \"%s\"\n", token);
//token = NULL;
return(0);
}
the // coments are all my failed attempts to try to make it work =(
Can someone help me please?