您如何在C中串联字符串? [英] How do you concatenate strings in C?

查看:85
本文介绍了您如何在C中串联字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

如何在C中连接2个字符串?





#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);
}

我试图将来自单独字符串的两个单词连接起来。我似乎在MSDN库中找不到如何执行此操作。它甚至存在于C中吗?还是您需要某种算法?

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?

推荐答案

strcat()是标准功能使用,该功能非常简单,通常使用C语言编写,因此您可以从头开始轻松编写它。

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.

这篇关于您如何在C中串联字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆