Learn Arrays and Strings in C Programming: 1D & 2D Arrays, Strings, and String Functions


Master arrays and strings in C programming. This tutorial covers one-dimensional and two-dimensional arrays, string handling functions like strlen, strcpy, strcmp, strcat, and practical exercises for real-world programming practice.

1. Practice: Matrix Operations Using Functions

Problem Statement

Write a C program to perform matrix addition and matrix multiplication using functions.

Matrix Addition Program

Logic

  1. Use a function to read matrices
  2. Use a function to add matrices
  3. Use a function to display the result

Code


#include <stdio.h>

void readMatrix(int a[10][10], int r, int c) {
int i, j;
for(i = 0; i < r; i++)
for(j = 0; j < c; j++)
scanf("%d", &a[i][j]);
}

void addMatrix(int a[10][10], int b[10][10], int sum[10][10], int r, int c) {
int i, j;
for(i = 0; i < r; i++)
for(j = 0; j < c; j++)
sum[i][j] = a[i][j] + b[i][j];
}

void displayMatrix(int a[10][10], int r, int c) {
int i, j;
for(i = 0; i < r; i++) {
for(j = 0; j < c; j++)
printf("%d ", a[i][j]);
printf("\n");
}
}

int main() {
int a[10][10], b[10][10], sum[10][10];
int r, c;

printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);

printf("Enter first matrix:\n");
readMatrix(a, r, c);

printf("Enter second matrix:\n");
readMatrix(b, r, c);

addMatrix(a, b, sum, r, c);

printf("Sum of matrices:\n");
displayMatrix(sum, r, c);

return 0;
}

Matrix Multiplication Program


#include <stdio.h>

void multiplyMatrix(int a[10][10], int b[10][10], int result[10][10], int r1, int c1, int c2) {
int i, j, k;

for(i = 0; i < r1; i++) {
for(j = 0; j < c2; j++) {
result[i][j] = 0;
for(k = 0; k < c1; k++)
result[i][j] += a[i][k] * b[k][j];
}
}
}

int main() {
int a[10][10], b[10][10], result[10][10];
int r1, c1, r2, c2, i, j;

printf("Enter rows and columns of first matrix: ");
scanf("%d %d", &r1, &c1);

printf("Enter rows and columns of second matrix: ");
scanf("%d %d", &r2, &c2);

if(c1 != r2) {
printf("Matrix multiplication not possible");
return 0;
}

printf("Enter first matrix:\n");
for(i = 0; i < r1; i++)
for(j = 0; j < c1; j++)
scanf("%d", &a[i][j]);

printf("Enter second matrix:\n");
for(i = 0; i < r2; i++)
for(j = 0; j < c2; j++)
scanf("%d", &b[i][j]);

multiplyMatrix(a, b, result, r1, c1, c2);

printf("Result matrix:\n");
for(i = 0; i < r1; i++) {
for(j = 0; j < c2; j++)
printf("%d ", result[i][j]);
printf("\n");
}

return 0;
}

2. Practice: Reverse a String Using Function

Problem Statement

Write a C program to reverse a string using a function.

Logic

  1. Pass string to function
  2. Reverse string using loop
  3. Display reversed string

Code


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

void reverseString(char str[]) {
int i, len;
char temp;

len = strlen(str);

for(i = 0; i < len / 2; i++) {
temp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = temp;
}
}

int main() {
char str[100];

printf("Enter a string: ");
gets(str);

reverseString(str);

printf("Reversed string: %s", str);

return 0;
}

3. Practice: Count Vowels in a String Using Function

Problem Statement

Write a C program to count the number of vowels in a string using a function.

Logic

  1. Pass string to function
  2. Check characters against vowels
  3. Return vowel count

Code


#include <stdio.h>

int countVowels(char str[]) {
int i, count = 0;

for(i = 0; str[i] != '\0'; i++) {
if(str[i] == 'a' || str[i] == 'e' || str[i] == 'i' ||
str[i] == 'o' || str[i] == 'u' ||
str[i] == 'A' || str[i] == 'E' || str[i] == 'I' ||
str[i] == 'O' || str[i] == 'U') {
count++;
}
}
return count;
}

int main() {
char str[100];
int vowels;

printf("Enter a string: ");
gets(str);

vowels = countVowels(str);

printf("Number of vowels: %d", vowels);

return 0;
}

Practice Tips for Learners

  1. Convert all programs to menu-driven
  2. Try using pointers instead of arrays
  3. Rewrite programs using return types
  4. Add input validation
  5. Practice without using built-in functions

Summary

These practice problems help you:

  1. Apply functions and return types
  2. Strengthen modular programming
  3. Improve problem-solving skills
  4. Prepare for interviews and exams