Help me in solving GSCP14 problem

My issue

Hey Guys,

I have a issue in this code the following output is working fine with my custom and the given condition but the program is not submit its show it was worng please tell what mistake i made. I appreciate if you correct my mistake

My code

// Update the '_' in the code below

#include <stdio.h>
#include <string.h>

#define MAX_LEN 1001 // maximum length of input string

int main() {
    int t;
    scanf("%d", &t);
    char S[MAX_LEN]; // declare a character array to store the input string
    for (int i = 0; i < t; i++) {
        scanf("%s", &S); // read in the input string
        int len = strlen(S); // get the length of the input string
        char X[MAX_LEN * 2]; // declare a character array to store the concatenated string
        int j;
        for (j = 0; j < len; j++) {
            X[j] = S[j]; // copy S into X
        }
        int k;
        for (k = 0; k < len; k++) {
            X[k + 2] = S[k]; // concatenate S with itself
        }
        X[j+k] = '\0'; // terminate the string
        printf("%s\n", X); // output the concatenated string
    }
    return 0;
}
     

Learning course: Logic Building in C
Problem Link: CodeChef: Practical coding for everyone

for better understanding you can try my approach its to easy.
here is my code !
// Update the ā€˜_ā€™ in the code below

include <stdio.h>
include <string.h>

define MAX_LEN 1001

int main() {
int t;
scanf(ā€œ%dā€, &t);
// char S[MAX_LEN]; // declare a character array to store the input string
for (int i = 0; i < t; i++) {
char S[1000];
scanf(ā€œ%sā€, S); // read in the input string
// int len = strlen(S); // get the length of the input string
// char X[MAX_LEN * 2]; // declare a character array to store the concatenated string
// int j;
// for (j = 0; j < len; j++) {
// X[] = S[]; // copy S into X
// }
// int k;
// for (k = 0; k < len; k++) {
// X[+] = S[_]; // concatenate S with itself
// }
// X[j+k] = ā€˜\0ā€™; // terminate the string
printf(ā€œ%s%sā€,S,S);
printf(ā€œ\nā€);
// output the concatenated string
}
return 0;
}

Hi tamtakartanu,

thanks for replaying but can you elaborate my mistake in this code I tried many time it was working correctly in the output but its not executing.

in your code :
for (k = 0; k < len; k++) {
X[k + 2] = S[k]; // concatenate S with itself
}
here you are doing mistake
why you are adding 2, you have to add len and also increase len buy 1 .
like this :
int a=len;
for(int k=0;k<len;k++){
X[k+a]=S[k];
a++;
}

1 Like