1. ## Help with inputs

Hey All
I've just started learning the C programming language at uni and am in need of some help.

If I have a input such as "3H 6S KH 7D AC"

Is there a way to group the second and first terms in the input e.g

First terms= 3,6,K,7,A
Second terms= H,S,H,D,C

Also is there a way to give the letters numerical values within the source code such as

J=11
Q=12
K=13

Any help would be appreciate my knowledge of C is very basic
Thanks
Murock

2. The first question is easy. Simply have two parallel arrays, one of letters and one of values. If the letters go A-Z with no gap, you don't even need an actual array of letters, just access with values[ch - 'A'];

For the second question, you can write scratch code to handle this particular case. But really what you need to do is to define exactly what inputs are allowed (are they always pairs, always separated by a single space, are there always five pairs in a line?). Then you need to say what information you are getting, for example is a list of characters adequate, or do you need more structure?

An example function would be

Code:
```/*
get first and second terms in list of values
Params: input - input in the form 3H 6S KH 7D AC
frist - return pointer for first values in pair
second - return pointer for second values in pair.
Returns: number of pairs parsed, -1 on error
*/
int parselist(const char *input, char *first, char *second)
{
/* logic goes here */
}

/*
Now to use it
*/
char *line = getinput();
char ch1[256];
char ch2[256];
int i;

/* error in input, handle here */
else
{
printf("Pair %d char one %c char two %c\n", i, ch1[i], ch2[i]);
}```

3. There are always 5 its for 5 cards so like A=Ace H=Hearts 2=2 etc so they are always in this format

So for the arrays would I do something like

char suit[3];
char value[12];

4. Also found this
if (('2' <= CARD) && (CARD <= '9')) VALUE = CARD - '2';
else if ('J' == CARD) VALUE = 11;
else if ('Q' == CARD) VALUE = 12;
else if ('K' == CARD) VALUE = 13;
else if ('A' == CARD) VALUE = 1;

If I could somehow apply this to work for all 5 card value inputs

5. If the 10 card is represented with a single value like T I would just make a string to search for the value, since all the values are single characters. The code you found is also useful as an alternative, but it doesn't handle the 10 card. It should be easy to fix though if you know what's happening.

6. Originally Posted by murock
So for the arrays would I do something like

char suit[3];
char value[12];
There are usually 4 suits and 13 values in a deck.

Code:
`char array[n]`
is an array for n chars with valid indeces 0 <= i < n (i.e. 0, 1, 2, ..., n-1)

Bye, Andreas

7. so isn't char suit [3] ok since its makes (0,1,2,3) giving 4?
My ten card has to be represented in the input as the number "0" which i am finding difficult to change its value to 10 for obvious reasons.

8. Originally Posted by murock
so isn't char suit [3] ok since its makes (0,1,2,3) giving 4?
No. Declaring a character array "char suit[3]" means that there are three elements, called suit[0], suit[1] and suit[2]. Each element stores a char.

Originally Posted by murock
My ten card has to be represented in the input as the number "0" which i am finding difficult to change its value to 10 for obvious reasons.
What's wrong with using '0' to represent the 10 card? Choosing a character to represent each card is up to you. You could use '!' or '\$' to represent 10 if you want. Each character, written inbetween single quotes, is actually a small integer corresponding to your character encoding, so the value 10 (without single quotes) is also a valid value.

9. Anyone know a good place for tutorial videos or just plain tutorials not sure how to do a string search

10. There are a lot of tutorials on algorithms and data structures here: Algorithms and data structures in C/C++ - Cprogramming.com. There are also books written on this subject too. In particular I think the article on linear search will help you: Linear Search and Binary Search - Cprogramming.com. After you learn what linear search is, all you have to do is write one for your purpose. For example if I make the values one long string like "234567890JQKA" and as input I have 9, the search will return the location in the string, 7, which is the value of the card under this scheme.

11. Right well I know how a linear search works now But I'm still lost on how to do this could you guys show me an example lets be simply say my stdin is "3H KS" and I want to split the variables up so that 3 is a stand alone variable from H and K is stand alone from S. I'm not sure if this makes sense. So my stdin is in the form "ab cd" and a=3, b=15, c=13 d=16.
So eventually i want to end up with a program that can do the below.

0=10 H=15
J=11 S=16
Q=12 C=17
K=13 D=18
A=14

inputs are in the form
ab cd ef gh ij

12. Perhaps you could write a function to return a value based on the character passed to it. Then you can just read the input as a string, and run a loop that calls this function for each character in the string.

Code:
```int getValue(char userInput)
{
int result = 0;

switch(userInput)
{
case '2':
result = 2;
break;
case '3':
result = 3;
break;
// ...
}

return result;
}```
This is not a full solution, but might help you get started with an easier implementation.

13. inputs are in the form
ab cd ef gh ij