aboutsummaryrefslogtreecommitdiff
path: root/semestre 2/informatique/td/5- chaînes de caractère.md
blob: 8bea08941d932b0d08b17a50bded48b43552ef57 (plain)
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
---
tags:
  - sorbonne
  - informatique
  - td
semestre: 2
---
Déclarer un caractère : `char c = 'H';`
Déclarer string :
```c
char *c = "Hello";
char c[] = "Hello";
char c[6] = "Hello"; // car il y a \0 à la fin
char c[] = {'H', 'e', 'l', 'l', 'o', '\0'}; // si on ne finit par \0, on fait un array de caractères et non un string
```

> [!warning] Modification d'un string
> Quand on déclare une chaîne avec les `"`, on déclare une constante. Pour la modifier, on a besoin d'utiliser la déclaration en tableau (avec les `{}`)

Toutes les chaînes de caractère finissent par `'\0'`

On peut utiliser la lib `string.h`

```c
int int_to_str(int val){
	int i = 1;
	int tmp_val = val;
	for (i = 1; tmp_val % 10 != tmp_val; i++) tmp_val /= 10;
	char *str = malloc(sizeof(char) * (i+1)); // +1 car on n'oublie pas le '\0'
	str[i] = '\0'; // on rajoute de suite le '\0'
	for (int j = 0; j < i; j++) str[j-1] = val % pow(10, j) + 48;
	return str;
}
```