diff --git a/tp2/exo10.c b/tp2/exo10.c new file mode 100644 index 0000000..70ff976 --- /dev/null +++ b/tp2/exo10.c @@ -0,0 +1,26 @@ +#include +#include +#include + +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); +} \ No newline at end of file diff --git a/tp2/exo4.c b/tp2/exo4.c index 7375791..cf5f911 100644 --- a/tp2/exo4.c +++ b/tp2/exo4.c @@ -1,7 +1,7 @@ #include #include -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) { diff --git a/tp2/exo5.c b/tp2/exo5.c index de04e8b..e581990 100644 --- a/tp2/exo5.c +++ b/tp2/exo5.c @@ -1,7 +1,7 @@ #include #include -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; diff --git a/tp2/exo6.c b/tp2/exo6.c index 049ba65..a8ab178 100644 --- a/tp2/exo6.c +++ b/tp2/exo6.c @@ -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; diff --git a/tp2/exo7.c b/tp2/exo7.c index 71e3acf..f844d86 100644 --- a/tp2/exo7.c +++ b/tp2/exo7.c @@ -2,7 +2,7 @@ #include // 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; diff --git a/tp2/exo8.c b/tp2/exo8.c new file mode 100644 index 0000000..995cb46 --- /dev/null +++ b/tp2/exo8.c @@ -0,0 +1,17 @@ +#include +#include + +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)); +} \ No newline at end of file diff --git a/tp2/exo9.c b/tp2/exo9.c new file mode 100644 index 0000000..e5d1f6f --- /dev/null +++ b/tp2/exo9.c @@ -0,0 +1,36 @@ +#include +#include +#include + +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); + } +} \ No newline at end of file