From 852801f18b1b6c4b3a41f904cc0dcd387ad3bfe9 Mon Sep 17 00:00:00 2001 From: Frostbite22 <35862364+Frostbite22@users.noreply.github.com> Date: Sat, 27 Jan 2018 10:27:30 +0100 Subject: [PATCH 1/2] Create mirror using pointers & string library --- misc/mirror | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 misc/mirror diff --git a/misc/mirror b/misc/mirror new file mode 100644 index 00000000..2851db76 --- /dev/null +++ b/misc/mirror @@ -0,0 +1,53 @@ +#include +#include + +void saisie (char *cpointeur); +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)); + +} + +void saisie (char *cpointeur) +{ + printf("saisir une chaine\n"); + scanf("%s",cpointeur); +} + +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 ; +} + +int compte (char *s) +{ + char *p ; + int k ; + p=s ; + k=0 ; + while(*p!='\0') + { + p++ ; + k++ ; + } + return k ; +} From 784ecf811dacebd405c44876402488a4e68208e8 Mon Sep 17 00:00:00 2001 From: Frostbite22 <35862364+Frostbite22@users.noreply.github.com> Date: Wed, 31 Jan 2018 05:06:06 +0100 Subject: [PATCH 2/2] Update mirror This program "mirror" is used to reverse a string of character . eg : the string "helloo" becomes "oolleh" --- misc/mirror | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/misc/mirror b/misc/mirror index 2851db76..6db82a80 100644 --- a/misc/mirror +++ b/misc/mirror @@ -1,7 +1,7 @@ #include -#include +#include // we include the library string.h to the use of string -void saisie (char *cpointeur); +void saisie (char *cpointeur); // Prototypes of the three functions used in the program int compte (char *s); char* miroir (char * s); @@ -12,13 +12,16 @@ saisie(chaine); printf("miroir est %s",miroir(chaine)); } - -void saisie (char *cpointeur) +// 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 ; @@ -38,7 +41,8 @@ i++ ; return s ; } -int compte (char *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 ;