How do I tokenize a char array input into a char and a string?

turing042

I'm trying to ask the user for an input of say, 3 characters. I want to separate the first character and the last two from each other. So if "A13" is a user input, I want to store 'A' in a separate char and "13" in a separate char[].

//initializations
char seatName[4], seatRowName, seatNumber[3];

printf("\n\nPick a seat (Row Seat) ");
scanf("%s", seatName);
seatRowName=seatName[0];
seatNumber=strchr(seatName, seatRowName);
//I get the "error: incompatible types in assignment" on the above line

Sample output:

Pick a seat (Row Seat): A13
//seatRowName = A, seatNumber=13

Gyapti Jain

Use below code:

seatRowName=seatName[0];
strcpy(seatNumber, &seatName[1]);  // strncpy if you want to be safe

If you would never change seatName, you can also use const char *seatNumber = &seatName[1];


Why does it work:

             +0  +1  +2  +3
            +---+---+---+---+
   seatName | A | 1 | 3 | \0|
            +---+---+---+---+
             [0] [1] [2] [3]

In memory seatName stores the content in contiguous space. This approach would work fine even for inputs like A3. You should provide other sanity checks to input.

seatNumber=strchr(seatName, seatRowName);

I get the "error: incompatible types in assignment" on the above line

strchr returns char * and type of seatNumber is char [3]. Because types of RHS and LHS are different you are getting above error. Unlike many popular languages C doesn't allow this.

Assigning apples to oranges is almost always incorrect. strcpy(A, B); instead of A = B would work in this case.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I parse a char from a String?

How do I tokenize a string sentence in NLTK?

How do I tokenize a string sentence in NLTK?

How can I convert a String to a char array?

How do I tokenize a string in C++?

How do I convert a char to a String?

How do I use the char value for string?

Do I need to include space for the terminating char in my string array?

How do I add String to a char array?

How to append a char into array and tokenize it C

How do I read a string char by char in C++?

How do I print out an array of string(array) create by char ** in C?

How do I add a char variable to a string?

C - Sort char string in array to equal char user input

How do I compare a a string array with a char array

How do I convert an unsigned char array into a string in C?

How do you convert the input from a JTextfield from a string to a char?

how do I free this char** array?

How do I cast /copy from a char array so that I may store it in a char pointer array?

how do i count the replaced char in a string?

How do I tokenize a char array twice to turn it into a 3D char array?

How do I insert char* to array of structures?

How do I read a stringstream into a char *[40] / char ** array?

How do I count the occurence of a char in a string in i386?

If a string is an array of char, how do you convert it into an array of interger

How do I use tolower() with a char array?

Transfer the input on string array then to a char array

How do I turn a char bubblesort into a string bubblesort by string length

How do I stop newline from scanf being input into a char array that's filled immediately after the scanf input?