Combining two strings

This is a discussion on Combining two strings within the C Programming forums, part of the General Programming Boards category; I'm trying to create a schedule generator. I have global variables of Code: char *team1 = "random name"; char *team2 ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    39

    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?

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,343
    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]);
    Fact - Beethoven wrote his first symphony in C

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,343
    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]);
    }
    Fact - Beethoven wrote his first symphony in C

  4. #4

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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" */
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Combining 3 Bytes
    By Bladactania in forum C Programming
    Replies: 6
    Last Post: 03-13-2009, 04:20 PM
  2. Combining strings
    By Ayreon in forum C Programming
    Replies: 16
    Last Post: 03-03-2009, 09:33 AM
  3. Combining if statements
    By AmbliKai in forum C Programming
    Replies: 11
    Last Post: 11-19-2007, 04:32 AM
  4. combining C and C++
    By volk in forum C Programming
    Replies: 7
    Last Post: 03-18-2003, 07:41 PM
  5. combining multiple strings into 1 string
    By Leeman_s in forum C++ Programming
    Replies: 3
    Last Post: 01-27-2003, 08:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21