0% found this document useful (0 votes)
3 views18 pages

Chapter 5 - Array and String

The document provides an overview of arrays in C programming, covering one-dimensional arrays, string/character arrays, and two-dimensional arrays. It includes syntax for declaration, initialization, and accessing elements, as well as string handling functions and example exercises for practice. The exercises encourage users to implement various array operations and string manipulations.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views18 pages

Chapter 5 - Array and String

The document provides an overview of arrays in C programming, covering one-dimensional arrays, string/character arrays, and two-dimensional arrays. It includes syntax for declaration, initialization, and accessing elements, as well as string handling functions and example exercises for practice. The exercises encourage users to implement various array operations and string manipulations.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

LOGO

Nguyen Van Phuc

[Link]
LOGO

CONTENTS

1 One – dimensional arrays

2 String/Character array

3 Two-dimensional arrays
4 Exercises

[Link]
LOGO

1 One – dimensional arrays

 Declaration:
 Syntax
dataType arrayName[array_size];
 Example
int a[5];

[Link]
LOGO

1 One – dimensional arrays

 Element accessing:
 Format: arrayName [position number]
 Example: int a[5];
a[0] a[1] a[2] a[3] a[4]

[Link]
LOGO

1 One – dimensional arrays

 Initialization:
int a[5]={2,4,34,3,4};
or
int a[ ]={2,4,34,3,4};
a[0] a[1] a[2] a[3] a[4]

2 4 34 3 4

[Link]
LOGO

1 One – dimensional arrays

 Initialization:
int a[ 5 ] = { 5, 7 };

a[0] a[1] a[2] a[3] a[4]

5 7 0 0 0

[Link]
LOGO

2 String

 Declaration:
 Syntax
char arrayName[array_size];
 Example
char s[5];

[Link]
LOGO

2 String

 Initialization:
 char arrayName[ ] = “string content”;
 Example
char s[ ] = “UTE”;
a[0] a[1] a[2] a[3] a[4]

U T E \0

NULL character [Link]


LOGO

2 String

 Input/Output:
 Input: gets/gets_s function
 Output: puts function
Example:
char s[10];
gets(s);
puts(s);
[Link]
LOGO

2 String

 String handling function: <string.h>


Function Description Example

strlen() showing the length of a char a[10] = {“hello”};


string int k;
k = strlen(a); // k = 5
strcpy( ) copying one string into char a[10] = {“hello”};
another string char b[10];
strcpy(b, a); // copy string a into string b
strcmp() comparing two string char a[10] = {“hello”};
char b[10] = {“hi”};
int k;
k = strcmp(a,b); // k > 0
[Link]
LOGO

3 Two-dimensional arrays

 Declaration:
 Syntax
dataType arrayName [rowSize] [columnSize];
 Example
int a[2][3];

[Link]
LOGO

3 Two-dimensional arrays

 Initialization:
int a[2][3] = { {1, 2, 5} , {2, 3 , 1} };

1 2 5

2 3 1

[Link]
LOGO

3 Two-dimensional arrays

 Element accessing:
 Format
arrayName [ rowPosition][columnPosition]
 Example:
a[1][2] = 5;

[Link]
LOGO

4 Exercise
 Exercise 1:

Write a C program to allow user enter


(input) an array of 10 integer numbers then
print all.

[Link]
LOGO

4 Exercise
 Exercise 2:
Write a C program to allow user enter
(input) a array of 10 integer numbers
(negative value is not allowed):
a) print the maximum number
b) print the minimum of even numbers

[Link]
LOGO

4 Exercise
 Exercise 3:

Write a c program to allow user enter


(input) an array of 10 integer numbers. Print
all numbers in the form of increasing order.

[Link]
LOGO

4 Exercise
 Exercise 4:

Write a c program to read full name


a) print the given name
b) counting the number of words in the full
name.

[Link]
LOGO

4 Exercise
 Exercise 5:
Write a C program to read 2D array, the
array’s size is entered first (n row and m
column).
a) print the maximum element of array
b) print the maximum of elements on the
main diagonal
[Link]

You might also like