-
Combining two strings
I'm trying to create a schedule generator.
I have global variables of
Code:
char *team1 = "random name";
char *team2 = "someone else";
What I want to do is make a different string that will be
Code:
char * newString = "random name vs. someone else"
The way I have it set up is that I know that they're are only 45 possible matchup combinations, so I will make a variable for everyone (there's more than 2 teams, and I know this probably isn't the most efficient method, but its all I can think of at the moment.)
So how do I do this without having to type in all the names every time?
-
An array of strings is what you are probably after
Code:
char players[6][10] = {
"Player 1",
"Player 2",
"Player 3",
"Player 4",
"Player 5",
"Player 5",
};
printf("It's %s vs. %s!\n", players[2], players[3]);
-
Or better yet, something like this:
Code:
#include <stdio.h>
#include <stdlib.h>
void print_players(int p1, int p2);
int main (void)
{
print_players(2, 3);
return EXIT_SUCCESS;
}
void print_players(int p1, int p2)
{
char players[6][10] = {
"Player 1",
"Player 2",
"Player 3",
"Player 4",
"Player 5",
"Player 5",
};
printf("It's %s vs. %s!\n", players[p1], players[p2]);
}
-
-
Code:
/* declare an array of string constants */
const char *names[] = {
"one",
"two",
/* ... */
};
char match[BUFSIZ]; /* make sure this is big enough to hold two names */
sprintf(match, "%s vs %s", names[0], names[1]);
/* now match contains "one vs two" */