-4

Any idea how can I do this C# loop in C?

foreach (char c in data)

loop through a string by character.

bsteo
  • 1,738
  • 6
  • 34
  • 60

1 Answers1

4

There is no foreach or equivalent in C. You'll have to use a regular for loop:

for (size_t i = 0; i < strlen(data); ++i)
{
    char c = data[i];
    // ...
}

(Assuming data is a pointer to a null-terminated string.)

TypeIA
  • 16,916
  • 1
  • 38
  • 52
  • 2
    Significantly faster: `for (size_t s = strlen(data), i = 0; i < s; ++i)` – alk Feb 13 '14 at 19:44
  • @alk Only if `data` is not `const` and/or the compiler used is not able to make this optimization. Any decent modern optimizing compiler should have no trouble with this. – TypeIA Feb 13 '14 at 19:46