TP14 Chaines de Caractères

This commit is contained in:
HORVILLE 2021-10-12 16:14:21 +02:00
parent dd6a1b1bbb
commit ee2b132b8e
6 changed files with 145 additions and 0 deletions

14
APL1.1/TP14/initiales.c Normal file
View File

@ -0,0 +1,14 @@
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int argc, char * argv[]) {
char initiales[15];
for (int i = 1; i < argc; i++) initiales[i-1] = argv[i][0];
initiales[strlen(initiales)-1] = '\0';
printf("Initiales : %s\n", initiales);
return EXIT_SUCCESS;
}

24
APL1.1/TP14/lecture.c Normal file
View File

@ -0,0 +1,24 @@
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int argc, char * argv[]) {
char real_password[] = "test 1212 34";
char password[27];
for (int i = 1; i < argc; i++) {
if (i != 1) strcat(password, " ");
if (strlen(password) + strlen(argv[i]) > 26) {
puts("Mot de passe trop long !");
return EXIT_FAILURE;
}
strcat(password, argv[i]);
}
if (strcmp(password, real_password)) {
puts("Authentification réussie.");
} else puts("Mot de passe incorrect.");
return EXIT_SUCCESS;
}

17
APL1.1/TP14/miroir.c Normal file
View File

@ -0,0 +1,17 @@
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int argc, char * argv[]) {
char ligne_commande[200];
for (int i = 0; i < argc; i++) {
if (i != 0) strcat(ligne_commande, " ");
strcat(ligne_commande, argv[i]);
}
ligne_commande[strlen(ligne_commande)] = '\0';
for (int i = strlen(ligne_commande); i >= 0; i--) printf("%c", ligne_commande[i]);
printf("\n");
return EXIT_SUCCESS;
}

View File

@ -0,0 +1,34 @@
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int argc, char * argv[]) {
long x, y;
char* position_x;
char* position_y;
x = strtol(argv[1], &position_x, 10);
y = strtol(argv[2], &position_y, 10);
if ((x == 0) && (position_x==argv[1])) {
puts("Entier invalide.");
return EXIT_FAILURE;
} else if (*position_x != '\0') {
puts("Conversion partielle !");
return EXIT_FAILURE;
}
if ((y == 0) && (position_y==argv[2])) {
puts("Entier invalide.");
return EXIT_FAILURE;
} else if (*position_y != '\0') {
puts("Conversion partielle !");
return EXIT_FAILURE;
}
long z = x * y;
printf("%d * %d = %d\n", x, y, z);
return EXIT_SUCCESS;
}

View File

@ -0,0 +1,31 @@
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_LEN 200
int main(int argc, char * argv[]) {
char str[MAX_LEN];
printf("Ligne de texte : ");
fgets(str, MAX_LEN, stdin);
int e_found = 0; //Compte le nombre d'occurence de la lettre 'e'.
int letters_found[255] = {}; //Répertorie les différentes lettres trouvées.
int diff_letter_found = 0; //Compte le nombre de lettre différentes trouvées.
for (int i = 0; i < strlen(str); i++) {
char letter = str[i];
if (letter == 'e') e_found++;
if (letters_found[letter+128] != 1) {
letters_found[letter+128] = 1;
diff_letter_found++;
}
}
printf("La ligne contient %d e et %d lettre différente.\n", e_found, diff_letter_found);
return EXIT_SUCCESS;
}

25
APL1.1/TP14/truandage.c Normal file
View File

@ -0,0 +1,25 @@
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
long long int textToLL(char* text) {
char new_text[200];
for (int i = strlen(text)-1; i >= 0; i--) { //La chaine doit être montée à l'envers
char new_letter[10];
sprintf(new_letter, "%X", (int)text[i]); //Converti la lettre en sa valeur hexa
strcat(new_text, new_letter); //Rajoute à la suite de la séquence la lettre
}
return strtoll(new_text, NULL, 16); //Converti la chaine en une valeur
}
int main(int argc, char * argv[]) {
long long int n = 32217268732456802LL;
long long int a = textToLL("bonsoir");
printf("%s\n", &n);
printf("%s\n", &a);
return EXIT_SUCCESS;
}