-5

Possible Duplicate:
How to concatenate 2 strings in C?

#include <stdio.h>
#include <string.h>

/* Function prototypes */
void wordLength ( char *word );
void wordConcat ( char *wordC1, char *wordC2);


int main (void)
{
    int choice;
    char word [20];
    char wordC1 [20];
    char wordC2 [20];

    printf( "Choose a function by entering the corresponding number: \n"
        "1) Determine if words are identical\n"
        "2) Count number of words in sentence provided\n"
        "3) Enter two strings to be strung together\n"
        "4) Quit program\n" );
    scanf( "%d", &choice );
    flushall();

    while (choice >= 1 && choice < 4) 
    {
        /* if statements for appropriate user prompt and calls function */
        if (choice == 1) 
        {
            /* gather user input */
        printf( "\nYou have chosen to determine word length.\n"
                "Please enter the word:\t");
            gets( word );

            /* call function to output string as well as the string length */
            wordLength( word );
        }

        else if (choice == 2)
        {
            printf( "\nYou have chosen to concatenate 2 words with a % symbol in between them.\n"
                "Please enter word 1:\t");

            gets( wordC1 );

            printf("Please enter word 2:\t");

            gets( wordC2 );                     

            /* call function to output string as well as the string length */
            wordLength( word );
        }
    }
}

void wordLength( char *word )
{
    int length;

    printf( "\nThe string entered is:  %s\n\n", word);

    length = strlen (word);

    printf("The string length is: %d\n", length);

    return;
}

void wordConcat(char *wordC1, char *wordC2)
{
    printf( "\nThe first word entered is:  %s\n", wordC1);
    printf( "\nThe second word entered is:  %s\n", wordC2);
}

I am trying to concatenate two words coming from separate strings. I can't seem to find how to do this in the MSDN library. Does it even exist in C? Or do you need some sort of algorithm? How is this done?

GramThanos
  • 3,572
  • 1
  • 22
  • 34
user1724390
  • 71
  • 2
  • 4
  • 2
    Look into `strcat()`, and make sure you have enough space for the resulting string AND it's null terminator. Also beware of [Schlemiel the Painter](http://en.wikipedia.org/wiki/Schlemiel_the_Painter%27s_algorithm) – Fred Larson Nov 11 '12 at 23:59
  • 2
    or `strncat()`, which is safer. – jbowes Nov 12 '12 at 00:00
  • 1
    what happened when you typed "C concatenate string" into a search engine? You did search first, right? – Mitch Wheat Nov 12 '12 at 00:00
  • Research before asking. There's plenty of information on how to do this throughout StackOverflow and the web. – evanmcdonnal Nov 12 '12 at 00:01
  • @jbowes: Sort of. `strncat()` will avoid the immediate buffer overrun, but will not null terminate if it truncates. – Fred Larson Nov 12 '12 at 01:03

1 Answers1

0

strcat() is the standard function to use, and this function is very simple and normally written in C so you could just as easily write it from scratch.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466