c iterate string
To iterate through a string in C, you can use a loop and access each character individually. Here is an example:
#include <stdio.h>
int main() {
char str[] = "Hello, World!";
int i;
for (i = 0; str[i] != '\0'; i++) {
printf("%c\n", str[i]);
}
return 0;
}
In this example, we define a character array str
that contains the string "Hello, World!". We then use a for
loop to iterate through each character in the string. The loop continues until it reaches the null character '\0'
, which marks the end of the string. Inside the loop, we can perform any operations we want with each character.
In this case, we are simply printing each character using printf
. You can modify the loop body to perform any desired actions with each character.