C# Academy · Lesson

Iterating Over Strings

Strings as char sequences.

Lesson 3 of 413 steps

Iterating Over Strings is a free C# Academy lesson on CoddyKit. This is lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, and your progress syncs across the web and the CoddyKit app. The C# Academy course includes 4 lessons in total.

Strings Are Sequences

A string is a sequence of char values in order.

This means you can loop over a string and look at one character at a time. Iterating is how you inspect, count, or transform the characters in text.

string text = "Hello";
// 'H', 'e', 'l', 'l', 'o'

The Length Property

Every string has a Length property that tells you how many characters it holds.

You will use Length to control loops and to know the last valid index, which is always Length - 1.

class Program
{
    static void Main()
    {
        string text = "Hello";
        System.Console.WriteLine(text.Length);
    }
}

Looping With foreach

The simplest way to visit each character is a foreach loop.

It gives you one char per iteration without needing an index. This is the cleanest option when you do not need to know the position.

class Program
{
    static void Main()
    {
        string text = "Hi!";
        foreach (char c in text)
            System.Console.WriteLine(c);
    }
}

Looping With for and Index

A for loop gives you the index, which you need when position matters.

Run the index from 0 up to Length - 1 and read each character with text[i].

class Program
{
    static void Main()
    {
        string text = "Hi!";
        for (int i = 0; i < text.Length; i++)
            System.Console.WriteLine(text[i]);
    }
}

Indexing Starts at Zero

Characters are numbered from 0, so the first character is at index 0.

For "Code", text[0] is 'C' and text[3] is 'e'. Reading an index that is too large throws an error.

string text = "Code";
char first = text[0]; // 'C'
char last = text[3];  // 'e'

Counting Specific Characters

Iteration lets you count characters that match a condition.

This program counts how many vowels are in a word by checking each character against a small set.

class Program
{
    static void Main()
    {
        string text = "banana";
        int count = 0;
        foreach (char c in text)
            if (c == 'a') count++;
        System.Console.WriteLine(count);
    }
}

Combining With char Helpers

Inside a loop you can call the char helpers you learned earlier.

For example, count only the digits in a string by testing each character with char.IsDigit.

class Program
{
    static void Main()
    {
        string text = "a1b2c3";
        int digits = 0;
        foreach (char c in text)
            if (char.IsDigit(c)) digits++;
        System.Console.WriteLine(digits);
    }
}

Building a Transformed Result

You can iterate and collect a new result as you go.

This loop reads each character, uppercases it with char.ToUpper, and prints it. The original string is never changed because strings are immutable.

class Program
{
    static void Main()
    {
        string text = "loud";
        foreach (char c in text)
            System.Console.Write(char.ToUpper(c));
    }
}

Strings Are Immutable

You cannot change a character in place with text[0] = 'X'; that is a compile error.

Strings are immutable, meaning their characters never change. To get different text, you create a new string instead of editing the old one.

string text = "cat";
// text[0] = 'b'; // not allowed

Reversing With Iteration

By iterating from the last index down to 0, you can print characters in reverse order.

Start the index at Length - 1 and decrement until it reaches 0.

class Program
{
    static void Main()
    {
        string text = "abc";
        for (int i = text.Length - 1; i >= 0; i--)
            System.Console.Write(text[i]);
    }
}

foreach vs for

Use foreach when you only need each character and not its position.

Use a for loop when you need the index, want to skip characters, or loop in a custom order like reverse. Both visit the same characters.

Quick Check

Test your understanding of iterating over strings.

Recap

A string is a sequence of characters you can loop over.

Use foreach for simple iteration and a for loop with an index when position matters. The Length property gives the count, indexes start at 0, and strings are immutable so transforming text always creates a new string.

Free to start

Learn C# with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
93
Lessons
346

Frequently Asked Questions

Is the “Iterating Over Strings” lesson free?

Yes — the full text of “Iterating Over Strings” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO. The C# Academy course includes 4 lessons in total.

What will I learn in “Iterating Over Strings”?

Strings as char sequences. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

How long does the “Iterating Over Strings” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. The char Type
  2. Char Helper Methods
  3. Iterating Over Strings
  4. Building Strings
← Back to C# Academy