cstrtok

remove the addition of '\n' at the end of the last occurrence of strtok


I would like to remove the '\n' addition from the last occurrence of strtok. I tried to remove it manually but it didn't work

Mon code est :

int parseCommand(char *inputCommand, struct Command *command){
    int i = 0;
    char *token;
    
    token = strtok(inputCommand, " ");

    while(token != NULL){
        command->argv[i] = token; 
        token =  strtok(NULL, " ");
        i++;
    }
    
    command->argc = i;
    command->commandName = command->argv[0];

    /*-----------FOR DEBUG-----------*/
    /*
    printf("%s : est le nom de la commande\n", command->commandName);
    
    for(int j = 0; j<i; j++){
        printf("command->argv[%i] %s\n",j, command->argv[j]);
    }
    */ 
}

inputCommand is a string


Solution

  • Either add \r \n to the delimiters of strtok(), or try:

    buffer[strcspn(token, "\n")] = 0;