⬅ Previous Next ➡

Arrays & Strings

Arrays & Strings in C
  • Array is a collection of same-type elements stored in continuous memory.
  • Array indexing starts from 0 (first element).
  • Types: 1D (list), 2D (table/matrix), multidimensional (3D+).
  • String in C is a character array ending with a null character \0.
  • Standard string functions are available in <string.h>.

1) One-Dimensional (1D) Array

  • Declaration: type name[size];
  • Example: store marks of 5 subjects.
#include <stdio.h>

int main() {
    int marks[5] = {78, 85, 69, 92, 74};

    printf("First mark: %d\n", marks[0]);
    printf("Last mark: %d\n", marks[4]);

    return 0;
}

2) Array Iteration (Traversal)

  • Use loop to access each element.
#include <stdio.h>

int main() {
    int a[5] = {10, 20, 30, 40, 50};
    int i;

    for (i = 0; i < 5; i++) {
        printf("a[%d] = %d\n", i, a[i]);
    }

    return 0;
}

3) Common Array Operations (Sum, Average, Max, Min, Search)

  • Basic operations are done using loops.
#include <stdio.h>

int main() {
    int a[5] = {10, 5, 20, 8, 15};
    int i, sum = 0, max, min, target = 8, found = 0;

    max = a[0];
    min = a[0];

    for (i = 0; i < 5; i++) {
        sum += a[i];

        if (a[i] > max) max = a[i];
        if (a[i] < min) min = a[i];

        if (a[i] == target) found = 1;
    }

    printf("Sum = %d\n", sum);
    printf("Average = %.2f\n", sum / 5.0);
    printf("Max = %d\n", max);
    printf("Min = %d\n", min);
    printf("Found %d? %s\n", target, (found ? "Yes" : "No"));

    return 0;
}

4) Two-Dimensional (2D) Array

  • Used for matrix/table data.
  • Access: a[row][col]
#include <stdio.h>

int main() {
    int m[2][3] = {
        {1, 2, 3},
        {4, 5, 6}
    };

    int i, j;

    for (i = 0; i < 2; i++) {
        for (j = 0; j < 3; j++) {
            printf("%d ", m[i][j]);
        }
        printf("\n");
    }

    return 0;
}

5) Multidimensional Array (3D Example)

  • Used for layered data (example: section → student → marks).
#include <stdio.h>

int main() {
    int a[2][2][2] = {
        { {10, 20}, {30, 40} },
        { {50, 60}, {70, 80} }
    };

    int i, j, k;

    for (i = 0; i < 2; i++) {
        for (j = 0; j < 2; j++) {
            for (k = 0; k < 2; k++) {
                printf("%d ", a[i][j][k]);
            }
            printf("\n");
        }
        printf("---\n");
    }

    return 0;
}

6) String Handling in C

  • String is a char array ending with \0.
  • Input:
    • scanf("%s", str) reads a word (stops at space)
    • fgets(str, size, stdin) reads a full line (recommended)
#include <stdio.h>

int main() {
    char name[30];

    printf("Enter name: ");
    fgets(name, sizeof(name), stdin);

    printf("Hello, %s", name);
    return 0;
}

7) Standard String Functions (<string.h>)

  • strlen(s) - length of string
  • strcpy(dest, src) - copy string
  • strcat(dest, src) - concatenate (join) strings
  • strcmp(s1, s2) - compare strings (0 if equal)
  • strchr(s, ch) - find character in string
  • strstr(s, sub) - find substring
#include <stdio.h>
#include <string.h>

int main() {
    char a[50] = "Hello";
    char b[50] = "C";

    printf("Length of a = %lu\n", strlen(a));

    strcat(a, " ");
    strcat(a, b);
    printf("After strcat: %s\n", a);

    char copy[50];
    strcpy(copy, a);
    printf("Copy: %s\n", copy);

    printf("Compare (a vs copy): %d\n", strcmp(a, copy)); // 0

    return 0;
}

8) Quick Notes

  • Array size is fixed after declaration.
  • Always keep enough space for string and \0.
  • Prefer fgets() for safe string input.
  • Use loops for array operations like sum, max, min, search, sorting.
⬅ Previous Next ➡