parcourir char c
To iterate through a string character by character in the C programming language, you can use a loop and the null-terminated property of C strings. Here's an example:
#include <stdio.h>
int main() {
char str[] = "Hello, World!";
int i = 0;
while (str[i] != '\0') {
printf("%c\n", str[i]);
i++;
}
return 0;
}
In this example, we declare a character array str
containing the string "Hello, World!". We initialize a variable i
to 0, which will be used as an index to access each character of the string. The loop continues until it encounters the null character '\0'
, which marks the end of the string. Inside the loop, we print each character using printf
and increment i
to move to the next character.
This code will output each character of the string on a new line:
H
e
l
l
o
,
W
o
r
l
d
!
Please note that the code provided assumes that the string is null-terminated. If the string is not null-terminated, the loop may result in undefined behavior.