In this code, I'm taking a credit card number (which is datatype char) that has 12 digits that have spaces (eg: 1254 6789 4331). I want to split the string where the spaces are but when I ran the code on the command prompt, only the first 4 digits get printed (eg: 1254). There were no errors on the command prompt so I'm a little confused about why the code is not working. I would appreciate it if someone could help me, Thanks!
Code:
'''
#include<stdio.h>
#include <string.h>
int main() {
char string[20];
printf("Please enter your credit card number: ");
scanf("%s",string);
// Extract the first token
char * token = strtok(string," ");
// loop through the string to extract all other tokens
while( token != NULL )
{
printf( " %s\n", token ); //printing each token
token = strtok(NULL," ");
}
return 0;
}
'''
CodePudding user response:
Use
scanf("[^\n]",string);
hope it helps