c - 我的字符串输出为一堆废话

标签 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 enterting 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)

{
    char symbol = "\ 25";
    char result;
    printf( "\nThe first word entered is:  %s\n", wordC1);
    printf( "\nThe second word entered is:  %s\n", wordC2);

    result = strcat( wordC1, symbol, wordC2);

    printf("Output: %s\n", result);

    return;
}

每当我为程序的 wordConcat 函数输入 2 个单词时,字符串输出都是一堆废话:( 我所有的指针似乎都指向正确的位置。我做错了什么?

最佳答案

choice == 2 代码调用的是 wordLength() 而不是 wordConcat(),因此 word它传入的变量未设置。

关于c - 我的字符串输出为一堆废话,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13336997/

相关文章:

c - 如果将 Unicode 数据解析为多字节,程序会崩溃吗?

c - C 语言中使用指针处理字符串

c - 将字符数组视为整数 - Learn C the Hard Way 额外学分

c - 如何编写时间复杂度为 O(log n) 的计算 m^n 的迭代版本?

c - 如何将系统符号传递给 char*

变量后面可以输出字符串吗? (一行 printf)

c - 行不提示输入

c - GCC 标准优化行为

c - 将数据存储在两个彼此相关的数组中

python - yield 如何在 Python C 代码中工作,好的和坏的部分