-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
mirror.c
60 lines (56 loc) · 1.4 KB
/
mirror.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
#include <stdio.h>
#include <string.h> // we include the library string.h to the use of string
void saisie(
char *cpointeur); // Prototypes of the three functions used in the program
int compte(char *s);
char *miroir(char *s);
int main(int argc, char *argv[])
{
char chaine[20];
saisie(chaine);
printf("miroir est %s", miroir(chaine));
}
// this function is used to put a string
void saisie(char *cpointeur)
{
printf("saisir une chaine\n");
scanf("%s", cpointeur);
}
/* the function miroir (in french ) it means "mirror" , the major idea is to
permute the first caractere with the last using an auxilary variable (aux) the
the 2nd character with the penultimate one and so on . we made a call to the
function (compte) which counts the length of the string . As you can see clearly
, I substruct 1 from the equation k = compte(s)-1 ; to get rid of the EOF
caractere which is '\0' because it is not a caractere from the string typed */
char *miroir(char *s)
{
int i;
char aux;
int k;
k = compte(s) - 1;
i = 0;
while (i <= k)
{
aux = s[i];
s[i] = s[k];
s[k] = aux;
k--;
i++;
}
return s;
}
// compte plays the role of strlen so we can change it by an strlen function if
// you want that
int compte(char *s)
{
char *p;
int k;
p = s;
k = 0;
while (*p != '\0')
{
p++;
k++;
}
return k;
}