-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexercicio3.c
74 lines (56 loc) · 1.5 KB
/
exercicio3.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <stdio.h>
#include <string.h>
#define TAMFRASE 50
#define TAMFIM 75
int func(char st1[], char st2[], char stfim[], int tamf);
void main()
{
char f1[TAMFRASE], f2[TAMFRASE], final[TAMFIM] = "";
printf("Indique a frase 1: "); gets(f1);
printf("Indique a frase 2: "); gets(f2);
if (func(f1, f2, final, TAMFIM))
printf("Frase modificada : \n % s\n", final);
else
printf("Frase n�o modificada!\n");
}
/*Resolução do exercicio 3*/
int func(char st1[], char st2[], char stfim[], int tamf)
{
int i = 0, k = 0; /*variaveis para controlo dos ciclos for*/
int suporte = 0;
if (strlen(st1) == 0 && strlen(st2) == 0) /*se as duas strings tiverem vazias, devolve 0*/
return 0;
/*o resto do codigo podia ser posto dentro de um else, mas é redudante por que a função termina se a condição do primeiro if for verdadeira*/
if (strlen(st1) < strlen(st2)) /*o suporte vai guardar o valor da frase menor*/
suporte = strlen(st1);
else
suporte = strlen(st2);
/*popular o stfim com o conteudo do st1*/
for (i = 0; i < strlen(st1); i++)
{
stfim[k] = st1[i];
if (i < suporte)
{
k += 2; /*para ir populando 2 a 2*/
}
else /*caso o st1 for a frase maior ele vai continuar a popular em frente 1 a 1*/
{
k++;
}
}
/*popular o stfim com o conteudo do st2*/
k = 1; /*reinicializar o k a 1 para começar a escrever no stfim a partir do stfim[1]*/
for (i = 0; i < strlen(st2); i++)
{
stfim[k] = st2[i];
if (i < suporte)
{
k += 2;
}
else
{
k++;
}
}
return 1;
}