6 Arrays And Strings

C Programming · Unit 6

Arrays and Strings

Exam-focused notes for Arrays and Strings (C Programming, BIT102): what the TU syllabus asks and how it has actually been tested, with 13 solved past questions from this unit.

What this unit covers

  • One-dimensional arrays
  • Two-dimensional arrays
  • Multi-dimensional arrays
  • Array initialization and access
  • String definition and declaration
  • String functions from string.h
  • String manipulation and palindrome checking
  • Pattern generation using arrays

String definition and declaration

208110 marks

What is string?Explain any three string function in "string.h" header file in C with example.Write a program to check whether a word is palindrome or not.[2+3+5]

--- A string in C is a sequence of characters stored in a character array and terminated by a null character (\0). C does not have a built-in string data type; instead, strings are represented as arrays of type char. Declaration: The null character \0 marks...

Full solved answer →
05 marks

How is string defined in C program? Write a program to check if a given string is a palindrome or not. [5]

A string in C is defined as a one-dimensional array of characters terminated by a null character (\0). Unlike some other languages, C does not have a built-in string data type. Syntax: Examples of string declaration and initialization: The null character \0...

Full solved answer →

One-dimensional arrays

208010 marks

What is an array? How an array can be passed as function argument? Write a program to arrange an array having 10 integer numbers in ascending order using function.[10]

--- An array is a collection of elements of the same data type stored in contiguous (adjacent) memory locations, all sharing a common name and accessed individually using an index (subscript). Declaration Syntax: Example: Key Properties: Property Detail ---...

Full solved answer →
0

Array Definition and Differentiation

--- An array is a collection of elements of the same data type stored in contiguous (adjacent) memory locations, all sharing a common name and accessed individually using an index (subscript). General Syntax (C): Example: Here, all 5 integers are stored one...

Full solved answer →

Two-dimensional arrays

20795 marks

Write a program to find the sum of diagonal elements of n*n matrix. [5]

For an n×n matrix, there are two diagonals: - Principal (Main) Diagonal: elements where row index == column index → a[i][i] - Secondary (Anti) Diagonal: elements where row + column = n-1 → a[i][n-1-i] Note: If an element lies on both diagonals (center of od...

Full solved answer →
2081.25 marks

Write a program to add two 3×3 matrices. [5]

--- Step Description ------------------- Step 1 Declare three 3x3 integer arrays: A, B, and C Step 2 Use nested for loops to read elements of matrix A Step 3 Use nested for loops to read elements of matrix B Step 4 Add corresponding elements: C[i][j] = A[i]...

Full solved answer →

Pattern generation using arrays

20795 marks

Write a program to display the following pattern using the string "NEPAL". NNNNENENENEPNEPNEPNEPANEPANEPANEPALNEPALNEPAL[5]

The pattern reaches us as the run-on string NNNNENENENEPNEPNEPNEPANEPANEPANEPALNEPALNEPAL, so the first job is to break it back into rows. Splitting it into consecutive prefixes of "NEPAL" gives and joining these five rows back together reproduces the given...

Full solved answer →
20785 marks

Write a program to generate the following pattern of integers.

1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1

[5]

Looking at the pattern carefully: Each row i prints numbers from 1 to i, then back from i-1 to 1. --- --- --- Row (i) Increasing Part Decreasing Part Full Row --------------------------------------------------------- 1 1 (none) 1 2 1 2 1 1 2 1 3 1 2 3 2 1 1...

Full solved answer →
20775 marks

Write a program to generate the following pattern of Integers.

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

[5]

Looking at the pattern carefully: Pattern Logic: - Row i contains i groups - Each group in row i prints numbers from 1 to i - Except the last group which prints 1 to i (for last row) or ends with the row number --- --- --- --- Concept Description ----------...

Full solved answer →

Multi-dimensional arrays

20785 marks

What do you mean by multi-dimensional array? Write a program to display the sum of two mxn matrices. [5]

A multi-dimensional array is an array that has more than one dimension (index). It can be thought of as an array of arrays. The most common form is the two-dimensional (2D) array, which represents data in rows and columns (like a matrix). General syntax in ...

Full solved answer →
20775 marks

What do you mean by multi-dimensional array? Write program logic to add and display the sum of two m x n matrices. [5]

A multi-dimensional array is an array that has more than one dimension (index). It can be thought of as an array of arrays. The most common form is the two-dimensional (2D) array, which represents data in rows and columns (like a matrix or table). General S...

Full solved answer →

String manipulation and palindrome checking

20775 marks

Write a C program to check whether a given string is palindrome or not. [5]

A palindrome is a string that reads the same forwards and backwards. Example: "madam", "racecar", "level" --- --- Step Description ------------------- 1 Read the string from the user 2 Find the length using strlen() 3 Compare str[0] with str[len-1], str[1] ...

Full solved answer →

String functions from string.h

2081.25 marks

List any five types of functions for string manipulation with their uses. [5]

--- S.N. Function Use / Purpose ------------------------------- 1 strlen() Returns the length of a string (number of characters excluding null character \0) 2 strcpy() Copies one string into another string 3 strcat() Concatenates (joins/appends) one string ...

Full solved answer →