1 of 48

Multi-dimensional array in C 

2 of 48

Multi-dimensional array in C 

  •  multi-dimensional array in C can be defined as an array that has more than one dimension.
  • Having more than one dimension means that it can grow in multiple directions.
  • Some popular multidimensional arrays include 2D arrays which grows in two dimensions, and 3D arrays which grows in three dimensions.

3 of 48

�Types of Multidimensional Arrays�

Types of Multidimensional Arrays

  • In C, there can be many types of arrays depending on their dimensions but two of them are most commonly used:
  • 2D Array - Two Dimensional
  • 3D Array - Three Dimensional

4 of 48

Two-dimensional arrays in C

  • Two-dimensional arrays in C are used to store data in a tabular format, similar to a matrix or a grid.

  •  They can be thought of as an array of one-dimensional arrays, where each element is accessed using two indices: one for the row and one for the column.

5 of 48

Two-dimensional arrays in C Declaration

  • Declaration:
  • The syntax for declaring a 2D array in C is:

data_type array_name[rows][columns];

  • For example, to declare a 2D array named matrix of integers with 3 rows and 3 columns:
  • int matrix[3][3];

6 of 48

Int a[3][3]={{5,10,20},{25,30,35},{1,3,4}};

7 of 48

2D array Initialization:

  • Initialization:
  • 2D arrays can be initialized during declaration or after declaration.
  • int matrix[3][3] = {� {1, 2, 3},� {4, 5, 6},� {7,8,9}�};

8 of 48

After Declaration:

  • int a[2][2];
  • a[0][0] = 10;
  • a[0][1] = 20;
  • a[1][0] = 30;
  • a[1][1] = 40;

9 of 48

Strings

  • In C programming, a string is fundamentally a one-dimensional array of characters that is terminated by a null character (\0). 
  • This null terminator is crucial as it signifies the end of the string, allowing functions to correctly identify its boundaries.

10 of 48

Strings in C�

  • string is an array of characters terminated by a special character '\0' (null character). This null character marks the end of the string and is essential for proper string manipulation.
  • Unlike many modern languages, C does not have a built-in string data type. Instead, strings are implemented as arrays of char.

11 of 48

Declaration and Initialization:

  • Strings can be declared and initialized in several ways:
  • Using a string literal: This is the most common and convenient method. The compiler automatically adds the null terminator.
  • char greeting[] = "Hello";

INDEX

0

1

2

3

4

5

VALUE

H

e

l

l

o

‘\0’

12 of 48

Strings in C

  • Character by character initialization: You explicitly list each character, including the null terminator.

  • char name[] = {‘H', ‘e', ‘l', ‘o', '\0'};

13 of 48

Specifying size:

  • Specifying size: We can declare an array with a specific size, ensuring enough space for the string and the null terminator.
  • char city[20]; // Can hold a string up to 19 characters + '\0'

14 of 48

String Manipulation:�

  • The string.h header file provides a rich set of functions for manipulating strings in C. Some common functions include:
  • strlen(): Returns the length of a string (excluding the null terminator).
  • strcpy(): Copies one string to another.
  • strcat(): Concatenates (joins) two strings.
  • strcmp(): Compares two strings lexicographically.
  • strstr(): Finds the first occurrence of a substring within a string.

15 of 48

�Update�

  • We can change individual characters of a string using their index: str[0] = 'h’.
  • Strings can also be updated using standard library functions like strcpy() to replace the entire string.
  • Ensure the new string fits within the allocated array size to avoid memory issues.

16 of 48

�String built-in functions.�

  • C language provides various built-in functions that can be used for various operations and manipulations on strings.
  • These string functions make it easier to perform tasks such as string copy, concatenation, comparison, length, etc.
  • The <string.h> header file contains these string functions.

17 of 48

strlen()

  • strlen()
  • The strlen() function is used to find the length of a string. It returns the number of characters in a string, excluding the null terminator ('\0').

18 of 48

strlen():

  • Purpose: Calculates the length of a string.
  • Syntax: size_t strlen(const char *str);
  • Description: Returns the number of characters in str before the null terminator.

19 of 48

�strcpy()�

  • strcpy()
  • The strcpy() function copies a string from the source to the destination. It copies the entire string, including the null terminator.

20 of 48

strcpy():�

  • strcpy():
  • Purpose: Copies one string to another.
  • Syntax: char *strcpy(char *dest, const char *src);
  • Description: Copies the entire src string, including the null terminator, to the dest string. Ensure dest has enough allocated memory.

21 of 48

strncpy()

  • strncpy()
  • The strncpy() function is similar to strcpy(), but it copies at most n bytes from source to destination string.
  • If source is shorter than n, strncpy() adds a null character to destination to ensure n characters are written.

22 of 48

strncpy():�

  • strncpy():
  • Purpose: Copies a specified number of characters from one string to another.
  • Syntax: char *strncpy(char *dest, const char *src, size_t n);
  • Description: Copies at most n characters from src to dest. If src is shorter than n, dest will be padded with null characters. If src is longer, dest will not be null-terminated unless n is greater than or equal to strlen(src) + 1.

23 of 48

�strcat()�

  • strcat()
  • The strcat() function is used to concatenate (append) one string to the end of another. It appends the source string to the destination string, replacing the null terminator of the destination with the source string’s content.

24 of 48

strcat():�

  • strcat():
  • Purpose: Concatenates (joins) two strings.
  • Syntax: char *strcat(char *dest, const char *src);
  • Description: Appends the src string to the end of the dest string. The null terminator of dest is overwritten, and a new null terminator is added at the end of the concatenated string. Ensure dest has enough allocated memory.

25 of 48

�strncat()�

  • strncat()
  • In C, there is a function strncat() similar to strcat(). This function appends not more than n characters from the string pointed to by source to the end of the string pointed to by destination plus a terminating NULL character.

26 of 48

�strncat():�

  • strncat():
  • Purpose: Concatenates a specified number of characters from one string to another.
  • Syntax: char *strncat(char *dest, const char *src, size_t n);
  • Description: Appends at most n characters from src to dest. A null terminator is always added to dest.

27 of 48

�strcmp()�

  • strcmp()
  • The strcmp() is a built-in library function in C. This function takes two strings as arguments, compares these two strings lexicographically and returns an integer value as a result of comparison.

28 of 48

strcmp():�

  • strcmp():
  • Purpose: Compares two strings.
  • Syntax: int strcmp(const char *str1, const char *str2);
  • Description: Returns 0 if str1 and str2 are identical, a value less than 0 if str1 is lexicographically smaller than str2, and a value greater than 0 if str1 is lexicographically larger than str2.

29 of 48

�strncmp()�

  • strncmp()
  • This function lexicographically compares the first n characters from the two null-terminated strings and returns an integer based on the outcome.

30 of 48

strncmp():�

  • strncmp():
  • Purpose: Compares a specified number of characters of two strings.
  • Syntax: int strncmp(const char *str1, const char *str2, size_t n);
  • Description: Compares at most n characters of str1 and str2. Returns values similar to strcmp().

31 of 48

�strchr()�

  • strchr()
  • The strchr() function is used to find the first occurrence of a given character in a string. If the character is found, it returns a pointer to the first occurrence of the character; otherwise, it returns NULL.

32 of 48

strchr():�

  • strchr():
  • Purpose: Finds the first occurrence of a character in a string.
  • Syntax: char *strchr(const char *str, int c);
  • Description: Returns a pointer to the first occurrence of character c in str, or NULL if not found.

33 of 48

�strrchr()�

  • strrchr()
  • In C, strrchr() function is similar to strchr() function used to find the last occurrence of a given character in a string.

34 of 48

strrchr() 

  • The strrchr() function returns a pointer to the position of the last occurrence of a character in a string.
  • Syntax
  • strrchr(char * str, int character);

35 of 48

�strstr()�

  • The strstr() function in C is used to search the first occurrence of a substring in another string. If it is not found, it returns a NULL.
  • The strstr() function returns a pointer to the position of the first occurrence of a string in another string.

36 of 48

strstr() 

  • Syntax

  • strstr(char * str, char * substring);

37 of 48

strlwr() 

  • The strlwr() function in C is used to convert all uppercase letters within a given string to lowercase.

38 of 48

strlwr

  • Syntax
  • char *strlwr(char *str);

39 of 48

strupr() 

  • The strupr() function in C is used to convert all lowercase characters within a given string to their uppercase equivalents. It modifies the original string in place.

40 of 48

Strupr()

  • syntax
  • char *strupr(char *str);

41 of 48

Strrev()

  • The strrev() function in C is used to reverse a given string. It is a built-in function found in the string.h header file. 

42 of 48

strrev()

  • Syntax
  • char *strrev(char *str);

43 of 48

sprintf()�

  • sprintf()
  • The sprintf() function is used to format a string and store it in a buffer. It is similar to printf(), but instead of printing the result, it stores it in a string.

44 of 48

Function

Description

Syntax

Find the length of a string excluding '\0' NULL character.

strlen(str);

Copies a string from the source to the destination.

strcpy(dest, src);

Copies n characters from source to the destination.

strncpy( dest, src, n );

Concatenate one string to the end of another.

strcat(dest, src);

Concatenate n characters from the string pointed to by src to the end of the string pointed to by dest.

strncat(dest, src, n);

Compares these two strings lexicographically.

strcmp(s1, s2);

strncmp()

Compares first n characters from the two strings lexicographically.

strncmp(s1, s2, n);

Find the first occurrence of a character in a string.

strchr(s, c);

Find the last occurrence of a character in a string.

strchr(sch);

First occurrence of a substring in another string.

strstr(s, subS);

Format a string and store it in a string buffer.

sprintf(s, format, ...);

Split a string into tokens based on specified delimiters.

strtok(s, delim);

45 of 48

Function

Description

Syntax

Find the length of a string excluding '\0' NULL character.

strlen(str);

Copies a string from the source to the destination.

strcpy(dest, src);

46 of 48

Copies n characters from source to the destination.

strncpy( dest, src, n );

Concatenate one string to the end of another.

strcat(dest, src);

Concatenate n characters from the string pointed to by src to the end of the string pointed to by dest.

strncat(dest, src, n);

47 of 48

Compares these two strings lexicographically.

strcmp(s1, s2);

strncmp()

Compares first n characters from the two strings lexicographically.

strncmp(s1, s2, n);

Find the first occurrence of a character in a string.

strchr(s, c);

48 of 48

Find the last occurrence of a character in a string.

strchr(sch);

First occurrence of a substring in another string.

strstr(s, subS);

Format a string and store it in a string buffer.

sprintf(s, format, ...);