简体   繁体   English

将两个字符串连接成一个的麻烦

[英]Trouble with with concatenating two strings in to one

I need to write a program that concatenates two strings in to the first string. 我需要编写一个将两个字符串连接到第一个字符串的程序。 I can't use a third parameter to hold the new string. 我不能使用第三个参数来保存新字符串。 When I run the program I get no output, so I'm pretty sure the problem is in my function that concatenates the two strings. 当我运行该程序时,没有任何输出,因此,我很确定问题出在连接两个字符串的函数中。

#include <stdio.h>

#define MAX_SZ 81

int concatString(char s1[], char s2[]); // concatenates s2 onto s1, returns length of s1
int getString(char c[], int len);

main(void)
{
    char array1[MAX_SZ * 2];
    char array2[MAX_SZ];
    int string1 = 0;
    int string2 = 0;
    int concat = 0;

    printf("Please String # 1 up to 80 characters long:\n");
    string1 = getString(array1, MAX_SZ);
    printf("Please enter String #2 up to 80 characters long:\n");
    string2 = getString(array2, MAX_SZ);

    concat = concatString(array1, array2);

    printf("You entered \"%s\" (length = %i)\n", array1, concat);

    return 0;
}

int getString(char c[], int len)
{
    int i = 0;
    char d = 0;
    while (d != '\n' && i < len)
    {
        d = getchar();
        c[i++] = d;
    }

    c[--i] = '\0';
    return (i);
}

int concatString(char s1[], char s2[])
{
  int i, j;

  for (i = 0; s1 != '\0'; ++i)
    s1[i] = s1[i];

  for (j = i; s2 != '\0'; ++j)
    s1[j] = s2[j];

  s1 [i + j] = '\0';
  return (i + j);
}

The problem is in these lines: 问题出在以下几行:

for (j = i; s2 != '\0'; ++j)
  s1[j] = s2[j];

Here j is not zero, so you start indexing in s2 at a non-zero index, possible even running out of bounds. 这里j不为零,因此您将在s2中以非零索引开始索引,甚至可能超出范围。

But that's only one problem. 但这只是一个问题。 The second problem is the condition of the loop, it will never be false leading to an infinite loop. 第二个问题是循环的条件,它永远不会是错误的,从而导致无限循环。

The first loop has the same problem with the loop condition. 第一个循环的循环条件存在相同的问题。

As for why the loop condition is wrong, the character '\\0' is equal to zero which is also on most systems equal to NULL . 至于为什么循环条件错误,字符'\\0'等于零,这在大多数系统上也等于NULL So you are effectively checking if eg s1 != NULL . 因此,您正在有效地检查是否s1 != NULL Since both strings are compile-times arrays the pointers passed to the functions will never bel null pointers. 由于两个字符串都是编译时数组,因此传递给函数的指针将永远不会为空指针。

There are a few issues, mainly with the condition in the for loops: 有几个问题,主要是与for循环中的条件有关:

for (i = 0; s1 != '\0'; ++i)

unless the string is null, this will never be true. 除非字符串为null,否则永远不会为true。

What you want to put instead is 您要输入的是

for (i = 0; s1[i] != '\0'; ++i)

this checks if the character you're looking at is the null terminating char, which is probably what you wanted. 这将检查您正在查看的字符是否为空终止字符,这可能就是您想要的。

The second loop is also a problem. 第二个循环也是一个问题。

for (j = i; s2 != '\0'; ++j)
s1[j] = s2[j];

You're starting s2 at j, not at zero 您是从j开始而不是从s2开始

for (j = 0; s2[j] != '\0'; ++j)
s1[i + j] = s2[j];

This walks you through s2, adding s2[0] to s1[i], where s1[i] is the null character from your last for loop. 这将引导您完成s2,将s2 [0]添加到s1 [i],其中s1 [i]是最后一个for循环中的空字符。 Then you increment j, and then s1[i+1] becomes s2[1], and so on. 然后递增j,然后s1 [i + 1]变成s2 [1],依此类推。

Additionally: 另外:

Appending the null character can (possibly) be done as : 可以通过以下方式添加空字符(可能):

for (j = 0; s2[j-1] != '\0'; ++j)
s1[i + j] = s2[j];

if you know FOR SURE that the character before s2 is not null, this can be done by declaring: 如果您肯定知道s2之前的字符不为null,则可以通过声明:

char array1[MAX_SZ * 2];
char placeholder = 0xff
char array2[MAX_SZ];

if that didn't make sense, ignore it too and append the null char the way you were doing it before 如果那没有意义,也请忽略它,并以之前的方式附加null char

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM