Click here to Skip to main content
15,669,440 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:

void stringConcatenate(char str1[], char str2[]){
    int i,j;
    i = strlen(str1);
    //for (int i = 0; str1[i] != '\0'; ++i){
        for (j = 0; str2[j] != '\0'; i++, j++) {
            str1[i] = str2[j];
    str1[i] = '\0';

int main() {
    char s1[50], s2[30];
    int N,Z;
    scanf("%d", &N);

    printf("%s", s1);
    //printf("%d", )

    return 0;

What I have tried:

The code is working properly, but passes only one test case.
The test cases are -
Rick manord 1 ---> Rickmanord
Hello world 2 ---> Hellowworldworld
Gram piece 3 Grampiecepiecepiece
Anyone please help with why it isn't passing the other testcases
Updated 15-May-22 6:24am

Are you sure you have the correct input parameters? What's the purpose of the number at the end of the line?

Assuming that the number should be to tell you how many strings to concatenate, I'd expect the number to come first e.g.
2 Rick Manood
3 Hello Rick Manood
11 By the pricking of my thumbs something wicked this way comes

Some thoughts:
1) 50 may be too short for some test cases, you're probably better off using dynamic memory, e.g. malloc()
2) The problem states that you need to concatenate n strings, not just 2, so you probably want to use a function something like
char *stringCatenate(size_t nargs, const char **strings)
So you'd pass in a counter of the number of strings, and an array of strings, getting back a dynamically allocated string of the strings concatenated together. If you're going to get a fail for using malloc() (a builtin function - at least for the purposes of this discussion), then you could use a large destination buffer, and then add that as a parameter to stringCatenate e.g.
char *stringCatenate(char *dest, size_t nargs, const char **strings)

Note that any fixed size array has the potential for overflow, leading to unexpected behavior (google "undefined behavior"), but 4K is likely to be enough.
Share this answer
The reason it only passes the first test is that you haven't done anything with the numeric value, other than read it into a variable.
Look at the inputs, and then look at the output. It's pretty obvious what you should be doing with it.

But the arrays you allocate are probably going to cause you real problems: they are pretty small, and if the number gets larger, you will run out of space very quickly and overwrite the input. I'd suggest you look at malloc instead of fixed length arrays.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900