make functions args const when pertinent + tp2 exo8-10
This commit is contained in:
parent
7157655bd0
commit
e3cbac7576
26
tp2/exo10.c
Normal file
26
tp2/exo10.c
Normal file
@ -0,0 +1,26 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
void f(const char s[], char *ext) {
|
||||
uint i = 0;
|
||||
while (s[i] != 0) {
|
||||
char c = s[i];
|
||||
ext[i] = c + ((c >= 65 && c <= 90) * 32) - ((c >= 97 && c <= 122) * 32);
|
||||
i++;
|
||||
}
|
||||
ext[i] = 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
char s[256];
|
||||
char s1[] = "baab";
|
||||
char s2[] = "kayak";
|
||||
char s3[] = "Hello Word!";
|
||||
f(s1, s);
|
||||
printf("f(\"%s\") = \"%s\"\n", s1, s);
|
||||
f(s2, s);
|
||||
printf("f(\"%s\") = \"%s\"\n", s2, s);
|
||||
f(s3, s);
|
||||
printf("f(\"%s\") = \"%s\"\n", s3, s);
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int f(int t[], uint len, int x) {
|
||||
int f(const int t[], uint len, int x) {
|
||||
uint i = 0;
|
||||
while (i < len) {
|
||||
if (t[i] == x) {
|
||||
|
@ -1,7 +1,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int f(int t[], int len, int x) {
|
||||
int f(const int t[], int len, int x) {
|
||||
int count = 0;
|
||||
while (len >= 0) {
|
||||
count += t[len] == x;
|
||||
|
@ -16,7 +16,7 @@ void f(int t[], uint len) {
|
||||
}
|
||||
}
|
||||
|
||||
void print_t(int t[], uint len) {
|
||||
void print_t(const int t[], uint len) {
|
||||
if (len == 0) {
|
||||
printf("[]");
|
||||
return;
|
||||
|
@ -2,7 +2,7 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
// Expect t to be a non empty list of columns. Expect res to be of size 2
|
||||
void f(int *t, uint rows, uint cols, int res[2]) {
|
||||
void f(const int *t, uint rows, uint cols, int res[2]) {
|
||||
int i = (rows * cols) - 1;
|
||||
int i_max = i;
|
||||
while (i > 0) {
|
||||
@ -15,7 +15,7 @@ void f(int *t, uint rows, uint cols, int res[2]) {
|
||||
res[1] = i_max / cols;
|
||||
}
|
||||
|
||||
void print_t(int t[], uint len) {
|
||||
void print_t(const int t[], uint len) {
|
||||
if (len == 0) {
|
||||
printf("[]");
|
||||
return;
|
||||
|
17
tp2/exo8.c
Normal file
17
tp2/exo8.c
Normal file
@ -0,0 +1,17 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
uint f(const char s[]) {
|
||||
uint i = 0;
|
||||
while (1) {
|
||||
if (s[i] == 0) {
|
||||
return i;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
char s[] = "Hello World!";
|
||||
printf("La taille de \"%s\" est %d\n", s, f(s));
|
||||
}
|
36
tp2/exo9.c
Normal file
36
tp2/exo9.c
Normal file
@ -0,0 +1,36 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int f(const char s[]) {
|
||||
uint len = strlen(s);
|
||||
uint i = 0;
|
||||
while (i < len / 2) {
|
||||
if (s[i] != s[len - 1 - i]) {
|
||||
return 0;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int main() {
|
||||
char s1[] = "baab";
|
||||
char s2[] = "kayak";
|
||||
char s3[] = "Hello Word!";
|
||||
if (f(s1)) {
|
||||
printf("\"%s\" est un palindrome\n", s1);
|
||||
} else {
|
||||
printf("\"%s\" n'est pas un palindrome\n", s1);
|
||||
}
|
||||
if (f(s2)) {
|
||||
printf("\"%s\" est un palindrome\n", s2);
|
||||
} else {
|
||||
printf("\"%s\" n'est pas un palindrome\n", s2);
|
||||
}
|
||||
if (f(s3)) {
|
||||
printf("\"%s\" est un palindrome\n", s3);
|
||||
} else {
|
||||
printf("\"%s\" n'est pas un palindrome\n", s3);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user