+ 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; }

7th Jun 2021, 3:37 PM
David Holmes Ng'andu
David Holmes Ng'andu - avatar
1 Answer
+ 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.
7th Jun 2021, 3:52 PM
äœ çŸ„é“èŠć‰‡ïŒŒæˆ‘äčŸæ˜Ż
äœ çŸ„é“èŠć‰‡ïŒŒæˆ‘äčŸæ˜Ż - avatar