Qualquer sugestão melhor para este c funções copyString, concatString
Fui convidado para fazer duas funções copyString e concatString eu fiz e implementou os dois, mas na saída que eu tenho, me disseram que pode ser feito melhor, mas nunca foi explicado como.
agora está me matando o que eu poderia fazer melhor então aqui está o código e eu ficarei feliz em ouvir qualquer sugestão.
void copyString (char **strDst, const char *strSrc)
{
char *strTmp = NULL;
int length = strlen (src);
if (*strDst== NULL)
{
*strDst= malloc (length);
}
else
{
if (strlen(*strDst) != length)
{
strTmp = *strDst;
}
*strDst= malloc (length);
}
strcpy (*strDst, strSrc);
if (strTmp != NULL)
free (strTmp );
}
void concatString (char **strDst, const char *cat)
{
int cat_length = strlen (cat);
if (cat_length > 0)
{
*strDst= realloc (*strDst, strlen (*strDst) + cat_length);
strcat (*strDst, cat);
}
}
void main(int argc, char *argv[])
{
char *str = NULL;
copyString(&str, "Hello World");
puts(str);
copyString(&str,str+6);
puts(str);
concatString(&str, " Pesron");
}
A saída deve ser como segue:
1.Olá mundo
2. mundo
3. Pessoa do Mundo
Obrigado.