+ 1
How to loop through a string array in C?
How do i loop through a string array in c, outlining every element/word line by line. I can do this with well with python but am still new to c so I'm failing. My attempt: #include <stdio.h> #include <string.h> int main() { char full_name[] = "Drex Holmes"; for (int i = 0; i < full_name.length; i++) { printf("%s", i); } return 0; }
2 Answers
+ 3
Use strlen() to get the length of the string.
for(int i = 0; i < strlen(full_name); i++) {
printf("%c", full_name[i]);
}
Since the string is terminated by '\0', which is 0 in ASCII value, you can also use full_name[i] != 0 for the condition.