dimanche 19 avril 2020

returning pattern in string

I am writing programm from codewars site It should return pattern ie.

1
22
333
4444
55555
666666
7777777
88888888
999999999
10101010101010101010

my code below

char* pattern(const int n)
{
    char *result = (char*)malloc(sizeof(char)*n*n*n*n);
    char *temp = (char*)malloc(sizeof(char) * 6);
    memset(result, '\0', (n*n*n*n));
  memset(temp,'\0',6);
    if (n<1)
        return "";
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= i; j++)
        {
            sprintf(temp, "%d", i);
      strcat(result,temp);
      memset(temp,'\0',6);
        }
        strcat(result, "\n");
    }
    result[strlen(result) - 1] = '\0';
    return result;
}

I pass all basic test but random tests i get some erros. Where I have made mistake?

Aucun commentaire:

Enregistrer un commentaire