简体   繁体   中英

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.

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 . So you are effectively checking if eg 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 (i = 0; s1 != '\0'; ++i)

unless the string is null, this will never be 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

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. Then you increment j, and then s1[i+1] becomes s2[1], and so on.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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