Skip to main content

Linguagem C_Exercicios Propostos Solucao_12

Page 1

Linguagem C 25.ª Edição Atualizada e Aumentada FCA Editora (c) 2025

12

EXERCÍCIOS PROPOSTOS – SOLUÇÃO 12.1 1: #include <stdio.h> 2: #include <string.h> 3: #include <stdlib.h> 4: 5: char *repete(char *string, int n) 6: { 7: char *aux; 8: 9: if (n==0) 10: return strdup(""); 11: 12: if ((aux=(char *) calloc(n, strlen(string)+1))==NULL) 13: return aux; /* NULL */ 14: 15: for (int i=1; i<=n; i++) 16: { 17: strcat(aux, string); 18: if (i!=n) 19: strcat(aux, " "); 20: } 21: 22: return aux; 23: } 24: 25: int main(void) 26: { 27: char * ptr; 28: for (int i=4; i>=0; i--) 29: { 30: ptr = repete("Ola", i); 31: printf("|%s|\n", ptr); 32: free(ptr); 33: } 34: return 0; 35: }

PROG1207.C

12.2 1: #include <stdio.h> 2: #include <string.h> 3: #include <stdlib.h> 4: 5: char *metade(char *s) 6: { 7: char * tmp = (char*) malloc(strlen(s)/2+1); 8: if (tmp==NULL) 9: return tmp; 10: 11: strncpy(tmp, s, strlen(s)/2); 12: tmp[strlen(s)/2]='\0';

© FCA

1


Turn static files into dynamic content formats.

Create a flipbook
Linguagem C_Exercicios Propostos Solucao_12 by Grupo Lidel - Issuu