Create String Array Iterators in C

CBeginner
Practice Now

Introduction

In this lab, you will learn how to create and iterate through a string array in C programming. You will declare a string array, iterate through it using a while loop, handle null-terminated strings, and print each string in the array. These skills are essential for working with text data and building more complex C applications.

The lab covers the following steps: declaring a string array, iterating through the array with a while loop, handling null-terminated strings, printing each string in the array, and compiling and running the C program. By completing this lab, you will gain a solid understanding of working with string arrays in C, which is a fundamental concept in C programming.

Declare a String Array

In this step, you will learn how to declare a string array in C programming. String arrays are powerful data structures that allow you to store multiple strings in a single variable.

  1. Open the WebIDE and create a new file named string-array.c in the ~/project directory:
cd ~/project
touch string-array.c
  1. Open the file in the WebIDE and add the following code:
#include <stdio.h>

void main() {
    const char* fruits[] = {
        "Apple",
        "Banana",
        "Cherry",
        "Date",
        NULL
    };
}

This code demonstrates how to declare a string array in C. Let's break down the key components:

  • const char* indicates an array of constant string pointers
  • fruits[] is the name of the array
  • The array contains four fruit names as strings
  • NULL is used to mark the end of the array, which will be useful in future iterations

Note that this array is null-terminated, which means the last element is NULL. This is a common practice in C for string arrays, as it helps determine the end of the array during iteration.

Iterate Through the Array with a While Loop

In this step, you will learn how to iterate through a string array using a while loop in C programming. Building upon the previous step, we'll modify the string-array.c file to print out the contents of the fruits array.

  1. Open the string-array.c file in the WebIDE:
  2. Update the code to include a while loop for iterating through the array:
#include <stdio.h>

void main() {
    const char* fruits[] = {
        "Apple",
        "Banana",
        "Cherry",
        "Date",
        NULL
    };

    int i = 0;
    while (fruits[i]) {
        printf("Fruit: %s\n", fruits[i]);
        ++i;
    }
}

Let's break down the iteration logic:

  • int i = 0 initializes an index counter
  • while (fruits[i]) continues the loop until it reaches the NULL terminator
  • printf("Fruit: %s\n", fruits[i]) prints each fruit name
  • ++i increments the index to move to the next element
  1. Compile the program:
gcc string-array.c -o string-array
  1. Run the compiled program:
./string-array

Example output:

Fruit: Apple
Fruit: Banana
Fruit: Cherry
Fruit: Date

The while loop allows you to iterate through the array by checking for the NULL terminator, which signals the end of the array. This method is a common and efficient way to traverse string arrays in C.

Handle Null Terminated Strings

In this step, you will learn about null-terminated strings and how they are used to mark the end of string arrays in C programming. Understanding null termination is crucial for working with string arrays.

  1. Open the string-array.c file in the WebIDE:
  2. Modify the code to demonstrate null termination and string length:
#include <stdio.h>
#include <string.h>

void main() {
    const char* fruits[] = {
        "Apple",
        "Banana",
        "Cherry",
        "Date",
        NULL
    };

    int i = 0;
    while (fruits[i]) {
        printf("Fruit: %s\n", fruits[i]);
        printf("Length of %s: %lu\n", fruits[i], strlen(fruits[i]));
        ++i;
    }

    printf("Total number of fruits: %d\n", i);
}

Key modifications:

  • Added #include <string.h> to use the strlen() function
  • Used strlen() to demonstrate string length
  • Added a count of total fruits using the loop index
  1. Compile the program:
gcc string-array.c -o string-array
  1. Run the compiled program:
./string-array

Example output:

Fruit: Apple
Length of Apple: 5
Fruit: Banana
Length of Banana: 6
Fruit: Cherry
Length of Cherry: 6
Fruit: Date
Length of Date: 4
Total number of fruits: 4

Understanding null termination:

  • The NULL at the end of the array acts as a sentinel value
  • It helps determine the end of the array during iteration
  • strlen() works by counting characters until it reaches the null terminator (\0)
  • The loop stops when it encounters the NULL pointer

In this step, you will explore different ways to print strings from an array, including formatting options and advanced printing techniques.

  1. Open the string-array.c file in the WebIDE:
  2. Update the code to demonstrate multiple printing techniques:
#include <stdio.h>
#include <string.h>

void main() {
    const char* fruits[] = {
        "Apple",
        "Banana",
        "Cherry",
        "Date",
        NULL
    };

    // Method 1: Basic printing with index
    printf("Method 1: Basic Printing\n");
    int i = 0;
    while (fruits[i]) {
        printf("%d: %s\n", i + 1, fruits[i]);
        ++i;
    }

    // Method 2: Formatted printing with alignment
    printf("\nMethod 2: Formatted Printing\n");
    i = 0;
    while (fruits[i]) {
        printf("| %-10s | Length: %2lu |\n", fruits[i], strlen(fruits[i]));
        ++i;
    }

    // Method 3: Printing with additional formatting
    printf("\nMethod 3: Advanced Printing\n");
    i = 0;
    while (fruits[i]) {
        printf("Fruit #%d: [%s] has %lu characters\n",
               i + 1, fruits[i], strlen(fruits[i]));
        ++i;
    }
}
  1. Compile the program:
gcc string-array.c -o string-array
  1. Run the compiled program:
./string-array

Example output:

Method 1: Basic Printing
1: Apple
2: Banana
3: Cherry
4: Date

Method 2: Formatted Printing
| Apple      | Length:  5 |
| Banana     | Length:  6 |
| Cherry     | Length:  6 |
| Date       | Length:  4 |

Method 3: Advanced Printing
Fruit #1: [Apple] has 5 characters
Fruit #2: [Banana] has 6 characters
Fruit #3: [Cherry] has 6 characters
Fruit #4: [Date] has 4 characters

Printing techniques demonstrated:

  • Basic indexing with printf()
  • Formatted printing with width and alignment
  • Advanced formatting with additional information
  • Using strlen() to get string length

Summary

In this lab, you learned how to declare a string array in C programming, which allows you to store multiple strings in a single variable. You also learned how to iterate through the array using a while loop, handling null-terminated strings, and printing each string in the array. These techniques are fundamental for working with string data structures in C and provide a solid foundation for more advanced string manipulation tasks.