C Programs: String, Array, Pointer & Control Examples

Section 1: String Programs

1. Reverse Individual Characters of a String

Language: C

#include <stdio.h>
#include <string.h>

int main() {
    char str[100];
    int length, i;
    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);
    length = strlen(str);
    if (length > 0 && str[length - 1] == '\n') {
        str[length - 1] = '\0';
        length--;
    }
    printf("The string in reverse order is: ");
    for (i = length - 1; i >= 0; i--) {
        printf("%c", str[i]);
    }
    printf("\n");
    return 0;
}

2. Count Alphabets, Digits, and Special Characters

Language: C

#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main() {
    char str[100];
    int alphabets = 0, digits = 0, specialCharacters = 0, i = 0;
    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);
    int length = strlen(str);
    if (length > 0 && str[length - 1] == '\n') {
        str[length - 1] = '\0';
    }
    while (str[i] != '\0') {
        unsigned char ch = (unsigned char)str[i];
        if (isalpha(ch)) {
            alphabets++;
        } else if (isdigit(ch)) {
            digits++;
        } else {
            specialCharacters++;
        }
        i++;
    }
    printf("Alphabets: %d\n", alphabets);
    printf("Digits: %d\n", digits);
    printf("Special Characters: %d\n", specialCharacters);
    return 0;
}

3. Compare Two Strings

Language: C

#include <stdio.h>
#include <string.h>

int main() {
    char str1[100], str2[100];
    int i = 0, areEqual = 1;
    printf("Enter first string: ");
    scanf("%s", str1);
    printf("Enter second string: ");
    scanf("%s", str2);
    while (str1[i] != '\0' && str2[i] != '\0') {
        if (str1[i] != str2[i]) {
            areEqual = 0;
            break;
        }
        i++;
    }
    if (str1[i] == '\0' && str2[i] == '\0') {
        printf("The strings are equal.\n");
    } else {
        printf("The strings are not equal.\n");
    }
    return 0;
}

4. Count Total Number of Vowels and Consonants

Language: C

#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main() {
    char str[100];
    int vowels = 0, consonants = 0, i = 0;
    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);
    int length = strlen(str);
    if (length > 0 && str[length - 1] == '\n') {
        str[length - 1] = '\0';
    }
    while (str[i] != '\0') {
        char ch = tolower((unsigned char)str[i]);
        if (isalpha((unsigned char)ch)) {
            if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
                vowels++;
            } else {
                consonants++;
            }
        }
        i++;
    }
    printf("Vowels: %d\n", vowels);
    printf("Consonants: %d\n", consonants);
    return 0;
}

Section 2: DMS Programs

5. Grade Using Else-If Ladder

Language: C

#include <stdio.h>

int main() {
    int marks;
    printf("Enter the marks (0 - 100): ");
    scanf("%d", &marks);
    if (marks >= 90 && marks <= 100) {
        printf("Grade: A\n");
    } else if (marks >= 75 && marks < 90) {
        printf("Grade: B\n");
    } else if (marks >= 60 && marks < 75) {
        printf("Grade: C\n");
    } else if (marks >= 50 && marks < 60) {
        printf("Grade: D\n");
    } else if (marks >= 0 && marks < 50) {
        printf("Grade: F (Fail)\n");
    } else {
        printf("Invalid marks entered! Please enter marks between 0 and 100.\n");
    }
    return 0;
}

6. Arithmetic Operators Using Switch-Case

Language: C

#include <stdio.h>

int main() {
    int choice;
    float num1, num2, result;
    printf("Simple Arithmetic Operations Menu:\n");
    printf("1. Addition\n");
    printf("2. Subtraction\n");
    printf("3. Multiplication\n");
    printf("4. Division\n");
    printf("5. Modulus\n");
    printf("Enter your choice (1-5): ");
    scanf("%d", &choice);
    printf("Enter two numbers: ");
    scanf("%f %f", &num1, &num2);
    switch (choice) {
        case 1:
            result = num1 + num2;
            printf("Result = %.2f\n", result);
            break;
        case 2:
            result = num1 - num2;
            printf("Result = %.2f\n", result);
            break;
        case 3:
            result = num1 * num2;
            printf("Result = %.2f\n", result);
            break;
        case 4:
            if (num2 != 0.0f) {
                printf("Result = %.2f\n", num1 / num2);
            } else {
                printf("Error: Division by zero is not allowed.\n");
            }
            break;
        case 5:
            if ((int)num2 != 0) {
                printf("Result = %d\n", (int)num1 % (int)num2);
            } else {
                printf("Error: Modulus by zero is not allowed.\n");
            }
            break;
        default:
            printf("Invalid choice! Please choose between 1 and 5.\n");
    }
    return 0;
}

7. Positive / Negative and Even / Odd

Language: C

#include <stdio.h>

int main() {
    int choice, num;
    printf("Enter a number: ");
    scanf("%d", &num);
    printf("\nMenu:\n");
    printf("1. Check Positive or Negative\n");
    printf("2. Check Even or Odd\n");
    printf("Enter your choice (1-2): ");
    scanf("%d", &choice);
    switch (choice) {
        case 1:
            if (num >= 0)
                printf("%d is Positive.\n", num);
            else
                printf("%d is Negative.\n", num);
            break;
        case 2:
            if (num % 2 == 0)
                printf("%d is Even.\n", num);
            else
                printf("%d is Odd.\n", num);
            break;
        default:
            printf("Invalid choice! Please choose 1 or 2.\n");
    }
    return 0;
}

Section 3: Array Programs

8. Display Largest and Smallest Element in an Array

Language: C

#include <stdio.h>

int main() {
    int n, i, choice;
    int largest, smallest;
    printf("Enter the number of elements in the array: ");
    scanf("%d", &n);
    int arr[n];
    printf("Enter the elements of the array:\n");
    for (i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }
    do {
        printf("\nMenu:\n");
        printf("1. Find the largest element\n");
        printf("2. Find the smallest element\n");
        printf("3. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);
        switch (choice) {
            case 1:
                largest = arr[0];
                for (i = 1; i < n; i++) {
                    if (arr[i] > largest) {
                        largest = arr[i];
                    }
                }
                printf("The largest element is: %d\n", largest);
                break;
            case 2:
                smallest = arr[0];
                for (i = 1; i < n; i++) {
                    if (arr[i] < smallest) {
                        smallest = arr[i];
                    }
                }
                printf("The smallest element is: %d\n", smallest);
                break;
            case 3:
                printf("Exiting the program.\n");
                break;
            default:
                printf("Invalid choice. Please try again.\n");
        }
    } while (choice != 3);
    return 0;
}

9. Addition of Two Matrices

Language: C

#include <stdio.h>

int main() {
    int rows, cols, i, j;
    printf("Enter the number of rows: ");
    scanf("%d", &rows);
    printf("Enter the number of columns: ");
    scanf("%d", &cols);
    int mat1[rows][cols], mat2[rows][cols], result[rows][cols];
    printf("Enter the elements of the first matrix:\n");
    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++) {
            scanf("%d", &mat1[i][j]);
        }
    }
    printf("Enter the elements of the second matrix:\n");
    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++) {
            scanf("%d", &mat2[i][j]);
        }
    }
    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++) {
            result[i][j] = mat1[i][j] + mat2[i][j];
        }
    }
    printf("Resulting matrix after addition:\n");
    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++) {
            printf("%d ", result[i][j]);
        }
        printf("\n");
    }
    return 0;
}

10. Sum of Principal Diagonal Elements

Language: C

#include <stdio.h>

int main() {
    int n, i, j, sum = 0;
    printf("Enter the size of the square matrix: ");
    scanf("%d", &n);
    int matrix[n][n];
    printf("Enter the elements of the matrix:\n");
    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }
    for (i = 0; i < n; i++) {
        sum += matrix[i][i];
    }
    printf("The sum of the principal diagonal elements is: %d\n", sum);
    return 0;
}

Section 4: Pointer & Structure Programs

11. Store and Print Student Details Using Structure

Language: C

#include <stdio.h>

struct Student {
    char name[50];
    int roll;
    float marks;
};

int main() {
    struct Student s;
    printf("Enter student name: ");
    scanf("%49s", s.name);
    printf("Enter roll number: ");
    scanf("%d", &s.roll);
    printf("Enter marks: ");
    scanf("%f", &s.marks);
    printf("\nStudent Details:\n");
    printf("Name: %s\n", s.name);
    printf("Roll Number: %d\n", s.roll);
    printf("Marks: %.2f\n", s.marks);
    return 0;
}

12. Swap Two Numbers Using Pointers

Language: C

#include <stdio.h>

void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int main() {
    int x, y;
    printf("Enter two numbers: ");
    scanf("%d %d", &x, &y);
    printf("Before swapping: x = %d, y = %d\n", x, y);
    swap(&x, &y);
    printf("After swapping: x = %d, y = %d\n", x, y);
    return 0;
}