0% found this document useful (0 votes)
10 views50 pages

Introduction To C Programming: Array and S T R I ngs/0

Uploaded by

varshithgoud333
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)
10 views50 pages

Introduction To C Programming: Array and S T R I ngs/0

Uploaded by

varshithgoud333
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
You are on page 1/ 50

Introduction to C

Programming Lecture 6:
Ar r ay a n d S t r i n g s \0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

1 / 50
Outline

1 Arrays
1D Array
2D Array

2 Strings

Introduction to C Programming 2 / 50
Opening Discussion (1)
• Given we have following problem
• We have 10 students in the class
• We want to get average/sum/max/min score of their math course
• We also want to rank the scores
• Based on what we learned
• We should keep 10 variables of the same type
• How about we have 100 students??
1 #i n c l u d e < s t d i o . h>
2 v o i d main ( )
3 {
4 f l o a t x1 , x2 , x3 , x4 , x5 , x6 , x7 , x8 , x9 , x10 ;
5 f l o a t sum = 0 , avg = 0 ;
6 s c a n f ( ”%f ” , &x1 ) ;
7 sum += x1 ;
8 s c a n f ( ”%f ” , &x2 ) ;
9 sum += x2 ;
10 ...
11 avg = sum / 1 0 ;
12 }

Introduction to C Programming 3 / 50
Opening Discussion (2)

1 #i n c l u d e < s t d i o . h>
2 v o i d main ( )
3 {
4 f l o a t x1 , x2 , x3 , x4 , x5 , x6 , x7 , x8 , x9 , x10 ;
5 f l o a t sum = 0 , avg = 0 ;
6 s c a n f ( ”%f ” , &x1 ) ;
7 sum += x1 ;
8 s c a n f ( ”%f ” , &x2 ) ;
9 sum += x2 ;
10 ...
11 avg = sum / 1 0 ;
12 }

• Even that it is hard to do sorting


• Try your best to figure out how you can put fourty variables in order
• This is where the array comes

Introduction to C Programming 4 / 50
Outline

1 Arrays
1D Array
2D Array

2 Strings

Introduction to C Programming 5 / 50
1D Array: declaration (1)

• 1D array is defined in following form

type arrayName[size];
• type could be any type defined in C, e.g. int, float,...
• “arrayName” should be unique
• It is actually a variable/constant, so rules to other variables/constants
apply too
• “size” should be an integer or an integer constant greater than 0
i n t a [ 0 ] ; // i t i s grammar OK, b u t m e a n i n g l e s s

Introduction to C Programming 6 / 50
1D Array: declaration (2)

type arrayName[size];
• type could be any type defined in C, e.g. int, float,...
• “arrayName” should be unique
• It is actually a variable/constant, so rules to other variables/constants
apply too
• “size” should be an integer or an integer constant greater than 0

1 i n t main ( ) 1 i n t main ( )
1 i n t main ( )
2 { 2 {
2 {
3 const i n t N = 40; 3 i n t N = 40;
3 float x [40];
4 f l o a t x [N ] ; 4 f l o a t x [N ] ;
4 ....
5 .... 5 ....
5 return 0;
6 return 0; 6 return 0;
6 }
7 } 7 }

Introduction to C Programming 7 / 50
1D Array: declaration (3)

type arrayName[size];
• type could be any type defined in C, e.g. int, float,...
• “arrayName” should be unique
• It is actually a variable/constant, so rules to other variables/constants
apply too
• “size” should be an integer or an integer constant greater than 0

1 #d e f i n e N 40 1 i n t main ( ) 1 #d e f i n e N 40
2 i n t main ( ) 2 { 2 i n t main ( )
3 { 3 const i n t N = 40; 3 {
4 f l o a t x [N ] ; 4 f l o a t x [N ] ; 4 f l o a t x [ 3 ∗N ] ;
5 .... 5 .... 5 ....
6 return 0; 6 return 0; 6 return 0;
7 } 7 } 7 }

Introduction to C Programming 8 / 50
1D Array: visit array element (1)

• Element in an array is visited by the subscript


• Subscript starts from ‘0’ to ‘N-1’
• For example, visit the 3rd element of x[N], we write “x[2]”

1 #i n c l u d e < s t d i o . h>
2 i n t main ( )
3 {
4 float x [40];
5 x [0] = 5.0;
6 x [2] = 3.1;
7 p r i n t f ( ” x [ 0 ] = %f ” , x [ 0 ] ) ;
8 return 0;
9 }

Introduction to C Programming 9 / 50
1D Array: visit array element (2)

1 i n t main ( )
2 {
3 float x [40];
4 int i = 0;
5 f o r ( i = 0 ; i < 4 0 ; i ++)
6 {
7 p r i n t f ( ” I n p u t %d : ” , i ) ;
8 /∗−−be c a r e f u l below−−∗/
9 s c a n f ( ”%f ” , &(x [ i ] ) ) ;
10 }
11 return 0;
12 }

• You are not allowed to use subscript beyond 39


• You invade other’s territory!!

Introduction to C Programming 10 / 50
1D Array: how array looks like (1)

4 bytes 1 byte
10127 x[0] 3.1 10127 ch[0] c
10131 x[1] 4.2 10128 ch[1] b
10135 x[2] 5.0 10129 ch[2] e
… ... ... … ... ...
10279 x[38] 3.3 10165 ch[38] f
10283 x[39] 4.2 10166 ch[39] x

• The system opens a continuous memory block for an array


• Actual size depends on both the type and length of an array

Introduction to C Programming 11 / 50
1D Array: how array looks like (2)

1 #i n c l u d e < s t d i o . h>
4 bytes 2 i n t main ( )
3 {
10127 x[0] 3.1 4 int a [10] , b = 3;
10131 x[1] 4.2 5 char c [ 1 0 ] ;
6 p r i n t f ( ” a : %d\n” , s i z e o f ( a ) ) ;
10135 x[2] 5.0 7 p r i n t f ( ”b : %d\n” , s i z e o f ( b ) ) ;
… ... ... 8 p r i n t f ( ” c : %d\n” , s i z e o f ( c ) ) ;
return 0;
10279 x[38] 3.3 9
10 }
10283 x[39] 4.2

• The system opens a continuous memory block for an array


• Actual size depends on both the type and length of an array

Introduction to C Programming 12 / 50
1D Array: how array looks like (3)

1 #i n c l u d e < s t d i o . h>
2 i n t main ( )
3 { [Output]
4 int a [10] , b = 3;
5 char c [ 1 0 ] ; 1 a : 40
6 p r i n t f ( ” a : %d\n” , s i z e o f ( a ) ) ; 2 b: 4
7 p r i n t f ( ”b : %d\n” , s i z e o f ( b ) ) ; 3 c : 10
8 p r i n t f ( ” c : %d\n” , s i z e o f ( c ) ) ;
9 return 0;
10 }

• Actual size depends on both the type and length of an array

Introduction to C Programming 13 / 50
1D Array: initialization (1)

• No initialization, what happens


[1: local] [2: static] [3: external]
#i n c l u d e < s t d i o . h> #i n c l u d e < s t d i o . h> #i n c l u d e < s t d i o . h>
i n t main ( ) i n t main ( ) extern a [10];
{ { i n t main ( )
int a [10]; static int a [10]; {
int i = 0; int i = 0; int i = 0;
f o r ( ; i < 1 0 ; i ++) f o r ( ; i <10; i ++) f o r ( ; i <10; i ++)
p r i n t f ( ”%d ” , a [ i ] ) ; p r i n t f ( ”%d ” , a [ i ] ) ; p r i n t f ( ”%d ” , a [ i ] ) ;
return 0; return 0; return 0;
} } }

1 Initialize to random numbers


2 Initialize to zeros
3 Initialize to zeros

Introduction to C Programming 14 / 50
1D Array: initialization (2)

• Initializations as follows are valid

1 #i n c l u d e < s t d i o . h>
1 #i n c l u d e < s t d i o . h>
2 i n t main ( )
2 i n t main ( )
3 {
3 {
4 i n t a [ 1 0 ] = {3 , 2 , 5 ,
4 i n t a [ ] = {3 , 2 , 5 , 1};
1};
5 int i = 0;
5 int i = 0;
6 f o r ( ; i < 4 ; i ++)
6 f o r ( ; i < 1 0 ; i ++)
7 p r i n t f ( ”%d ” , a [ i ] ) ;
7 p r i n t f ( ”%d ” , a [ i ] ) ;
8 return 0;
8 return 0;
9 }
9 }

Introduction to C Programming 15 / 50
1D Array: initialization (3)

• Initializations as follows are invalid

1 #i n c l u d e < s t d i o . h>
1 #i n c l u d e < s t d i o . h>
2 i n t main ( )
2 i n t main ( )
3 {
3 {
4 int a [10];
4 i n t a = {3 , 2 , 5 , 1};
5 a [ 1 0 ] = {3 , 2 , 5 , 1};
5 int i = 0;
6 int i = 0;
6 f o r ( ; i < 4 ; i ++)
7 f o r ( ; i < 1 0 ; i ++)
7 p r i n t f ( ”%d ” , a [ i ] ) ;
8 p r i n t f ( ”%d ” , a [ i ] ) ;
8 return 0;
9 return 0;
9 }
10 }

Introduction to C Programming 16 / 50
1D Array Example-1 (1)

• Given an array: a[10] = {3, 21, 5, 8, 5,11, 22,14,9,51}


• Flip the array to: {51, 9, 14, 22, 11, 5, 8, 5, 21, 3}

5 minutes to think about the solution

Introduction to C Programming 17 / 50
1D Array Example-1 (2)

• Given an array: a[10] = {3, 21, 5, 8, 5,11, 22,14,9,51}


• Flip the array to: {51, 9, 14, 22, 11, 5, 8, 5, 21, 3}
• The idea is that, we only need to swap two elements each time
• One for the header, one from the rear
• We do this for 10
2 times

Introduction to C Programming 18 / 50
1D Array Example-1 (3)

• Given an array: a[10] = {3, 21, 5, 8, 5,11, 22,14,9,51}


• Flip the array to: {51, 9, 14, 22, 11, 5, 8, 5, 21, 3}
N
1 For i from 0 to 2 do
2 Exchange a[i] with a[N-i-1]
3 End-for
• Let’s do it, give you another 5 minutes ...

Introduction to C Programming 19 / 50
1D Array Example-1 (4)
1 For i from 0 to N2 do
2 Exchange a[i] with a[N-i-1]
3 End-for
1 #i n c l u d e < s t d i o . h>
2 i n t main ( )
3 {
4 i n t a [ 1 0 ] ={3 ,21 ,5 ,8 ,5 ,11 ,22 ,14 ,9 ,51};
5 int t = 0 , i = 0;
6 f o r ( ; i < 5 ; i ++){
7 t = a[ i ];
8 a [ i ] = a [10 − i − 1 ] ;
9 a [10 − i −1] = t ;
10 }
11 f o r ( i = 0 ; i < 1 0 ; i ++){
12 p r i n t f ( ”%d ” , a [ i ] ) ;
13 }
14 return 0;
15 }

Introduction to C Programming 20 / 50
Dynamic Array Example-2 (1)

• An integer number given by user


• int a[n]
• Input “n” numbers assign to “a[n]”
• Print out array “a”

5 minutes to think about the solution

Introduction to C Programming 21 / 50
Dynamic Array Example-2 (2)

1 #i n c l u d e < s t d i o . h>
2 i n t main ( )
3 {
4 int n = 0 , i = 0;
5 s c a n f ( ”%d” , &n ) ;
6 int a[n ];
7 f o r ( i = 0 ; i < n ; i ++)
8 {
9 s c a n f ( ”%d” , &a [ i ] ) ;
10 }
11 p r i n t f ( ”%d\n” , s i z e o f ( a ) ) ;
12 f o r ( i = 0 ; i < n ; i ++)
13 {
14 p r i n t f ( ” a[%d ] = %d\n” , i , a [ i ] ) ;
15 }
16 return 0;
17 }

Introduction to C Programming 22 / 50
1D Array Example-3 (1)

• Given a sorted array: a[7] = {3, 14, 15, 18, 22,35}


• Insert an input number to the array
• Keep the array sorted after the insertion

5 minutes to think about the solution...

Introduction to C Programming 23 / 50
1D Array Example-3 (2)
1 #i n c l u d e < s t d i o . h>
2 i n t main ( ) {
3 i n t a [ 7 ] = {3 ,14 ,15 ,18 ,22 ,35};
4 int b = 7 , i = 0 , j = 0;
5 s c a n f ( ”%d” , &b ) ;
6 f o r ( i = 0 ; i < 6 ; i ++){
7 i f (b < a [ i ]) {
8 break ;
9 }
10 }
11 f o r ( j = 6 ; j > i ; j −−){
12 a [ j ] = a [ j −1];
13 }
14 a[ j ] = b;
15 f o r ( i = 0 ; i < 7 ; i ++){
16 p r i n t f ( ” a[%d ] = %d\n” , i , a [ i ] ) ;
17 }
18 return 0;
19 }

Introduction to C Programming 24 / 50
1D Array Example-4 (1)

• Given an array: a[10] = {21, 3, 5, 8, 5,11, 22,14,51,9}


• Sort the array in ascending order: {3, 5, 5, 8, 9, 11, 14, 21, 22, 51}

5 minutes to think about the solution...

Introduction to C Programming 25 / 50
1D Array Example-4 (2)

• Given an array: a[10] = {21, 3, 5, 8, 5,11, 22,14,9,51}


• Sort the array in ascending order: {3, 5, 5, 8, 9, 11, 14, 21, 22, 51}
• The idea is bubble sort, which is a classic method for sorting
• Each time, we move the largest to the rear of the array
• Repeat this on sub-array for N times

Introduction to C Programming 26 / 50
1D Array Example-4 (3)

21 3 5 8 5 11 22 14 51 9
3 21 5 8 5 11 22 14 51 9
3 5 21 8 5 11 22 14 51 9
3 5 8 21 5 11 22 14 51 9
3 5 8 5 21 11 22 14 51 9
3 5 8 5 11 21 22 14 51 9
3 5 8 5 11 21 22 14 51 9
3 5 8 5 11 21 14 22 51 9
3 5 8 5 11 21 14 22 51 9
3 5 8 5 11 21 14 22 9 51

do bubble sort on this again

Max

Figure: Demo of one round of bubble sort

Introduction to C Programming 27 / 50
1D Array Example-4 (4)

• Let’s now outline the procedure


1 For i from 0 to N do
2 For j from 0 to N-i do
3 Check a[j] and a[j+1]
4 If a[j] > a[j+1]
5 swap them
6 End-if
7 End-for(j)
8 End-for(i)

Introduction to C Programming 28 / 50
1D Array Example-4 (5): the code
1 #i n c l u d e < s t d i o . h>
2 i n t main ( )
3 {
4 i n t a [ 1 0 ] = {3 , 5 , 5 , 8 , 9 , 11 , 14 , 21 , 22 , 51};
5 int i = 0 , j = 0 , t = 0;
6 f o r ( i = 0 ; i < 1 0 ; i ++) {
7 f o r ( j = 0 ; j < (10− i −1) ; j ++) {
8 i f ( a [ j ] > a [ j +1])
9 {
10 t = a[ j ];
11 a [ j ] = a [ j +1];
12 a [ j +1] = t ;
13 } // i f ( a [ j ] )
14 } // f o r ( j )
15 } // f o r ( i )
16 f o r ( i = 0 ; i < 1 0 ; i ++) {
17 p r i n t f ( ”%d ” , a [ i ] ) ;
18 }
19 return 0;
20 }

Introduction to C Programming 29 / 50
Outline

1 Arrays
1D Array
2D Array

2 Strings

Introduction to C Programming 30 / 50
Opening Discussion: 2D Array

• Continue with the opening example in the last section


• In your class, you might have several courses for each student
• So we need several 1D arrays
• Alternatively, we can use a 2D array

1 i n t main ( )
2 { 1 i n t main ( )
3 f l o a t math [ 4 0 ] ; 2 {
4 float c [40]; 3 float courses
5 float phis [40]; [40][4];
6 f l o a t bio [ 4 0 ] ; 4 ...
7 ... 5 }
8 }

Introduction to C Programming 31 / 50
2D Array: declaration

type arrayName[row][column];
• Similar as 1D array, type is required
• “arrayName” should be unique
• “row” and “column” should be constant expressions

1 i n t main ( )
2 {
3 f l o a t a [ 4 0 ] [ 4 ] ; // t h e r e 40 r o w s and 4 c o l u m n s i n e a c h row
4 a [ 3 ] [ 2 ] = 3.14;
5 return 0;
6 }

Introduction to C Programming 32 / 50
2D Array: initialization (1)

1 i n t main ( )
2 {
3 f l o a t a [ 3 ] [ 4 ] = {{1 ,3 ,1 ,1} ,{1 ,2 ,1 ,3} ,{1 ,12 ,1 ,2}};
4 return 0;
5 }

• Following way is also valid

1 i n t main ( )
2 {
3 f l o a t a [ 3 ] [ 4 ] = {1 ,3 ,1 ,1 ,1 ,2 ,1 ,3 ,1 ,12 ,1 ,2};
4 return 0;
5 }

Introduction to C Programming 33 / 50
2D Array: initialization (2)

1 i n t main ( )
2 {
3 f l o a t a [ ] [ 4 ] = {{1 ,3 ,1 ,1} ,{1 ,2 ,1 ,3} ,{1 ,12 ,1 ,2}};
4 return 0;
5 }

• Following way is also valid, row = ⌈ N4 ⌉

1 i n t main ( )
2 {
3 f l o a t a [ ] [ 4 ] = {1 ,3 ,1 ,1 ,1 ,2 ,1 ,3 ,1 ,12 ,1 ,2};
4 return 0;
5 }

• If no initialization, set to 0 by default

Introduction to C Programming 34 / 50
2D Array: initialization (3)

1 i n t main ( )
2 {
3 f l o a t a [ ] [ 4 ] = {{1 ,3 ,1 ,1} ,{1 ,2 ,1 ,3} ,{1 ,12 ,1 ,2}};
4 return 0;
5 }

• Following way is also invalid

1 i n t main ( )
2 {
3 f l o a t a [ 4 ] [ ] = {1 ,3 ,1 ,1 ,1 ,2 ,1 ,3 ,1 ,12 ,1 ,2};
4 return 0;
5 }

• It is organized in row major order

Introduction to C Programming 35 / 50
2D Array: how it looks like

4 bytes
10127 a[0][0] 3.1
10131 a[0][1] 4.2
10135 a[0][2] 5.0
… a[0][3] 0
a[1][0] 7
... ...
??? a[2][1] 3.1
??? a[2][2] 3.3
10171 a[2][3] 4.2

• 3(row)×4(column)×4 bytes

Introduction to C Programming 36 / 50
2D Array: visit element of the array

1 i n t main ( )
2 {
3 f l o a t a [ ] [ 4 ] = {1 ,3 ,1 ,1 ,1 ,2 ,1 ,3 ,1 ,12 ,1 ,2};
4 int i = 0 , j = 0;
5 f o r ( i = 0 ; i < 3 ; i ++)
6 {
7 f o r ( j = 0 ; j < 4 ; j ++)
8 {
9 p r i n t f ( ”%f ” , a [ i ] [ j ] ) ;
10 }
11 p r i n t f ( ” \n” ) ;
12 }
13 return 0;
14 }

Introduction to C Programming 37 / 50
Example-1: transpose a matrix (1)

• Given a 2D matrix, kept in a 1D array


• a[12] = {12,13,5,5,7,21,6,4,10,5,5,9}
 
12 13 5  
 5 7 21  12 5 6 5
A=  6 4 10 

⇒ B = AT =  13 7 4 5 
5 21 10 9 4×3
5 5 9 3×4
• a[12] ={12,5,6,5,13,7,4,5,5,21,10,9}
1 A[i][j] ⇄ A[j][i]
2 A[i][j] ⇒ a[i*c1+j]
3 B[j][i] ⇒ b[j*c2+i]
4 a[i*c1+j] ⇌ b[j*c2+i]

Introduction to C Programming 38 / 50
Example-1: transpose a matrix (2)
1 #i n c l u d e < s t d i o . h>
2 i n t main ( ) {
3 int a [12] = {12 ,13 ,5 ,5 ,7 ,21 ,6 ,4 ,10 ,5 ,5 ,9};
4 i n t b [ 1 2 ] = {0};
5 i n t i = 0 , j = 0 , tmp = 0 ;
6 f o r ( i = 0 ; i < 4 ; i ++){
7 f o r ( j = 0 ; j < 3 ; j ++){
8 b [ j ∗4+ i ] = a [ i ∗3+ j ] ;
9 }
10 }
11 f o r ( i = 0 ; i < 3 ; i ++){
12 f o r ( j = 0 ; j < 4 ; j ++){
13 p r i n t f ( ”%4d” , b [ i ∗4+ j ] ) ;
14 }
15 p r i n t f ( ” \n” ) ;
16 }
17 return 0;
18 }

Introduction to C Programming 39 / 50
Outline

1 Arrays
1D Array
2D Array

2 Strings

Introduction to C Programming 40 / 50
Opening discussion
• Now, we are going to discuss a special kind of array
• Array of chars, we give it a new name string
• Different from integer array, empty elements are set to ‘\0’

1 #i n c l u d e < s t d i o . h>
2 i n t main ( )
1 #i n c l u d e < s t d i o . h>
3 {
2 i n t main ( )
4 c h a r h i [ 8 ] ={ ’ h ’ , ’ e ’ , ’ l ’
3 {
, ’ l ’ , ’o ’ };
4 c h a r h i [ 8 ] ={ ’ h ’ , ’ e ’ , ’ l ’
5 int i = 0;
, ’ l ’ , ’o ’ };
6 f o r ( i < 8 ; i ++)
5 int i = 0;
7 {
6 p r i n t f ( ”%s ” , h i ) ;
8 p r i n t f ( ”%c ” , h i [ i ] ) ;
7 return 0;
9 }
8 }
10 return 0;
9 [ Output : h e l l o ]
11 }
10
12 [ Output : h e l l o ]
13

Introduction to C Programming 41 / 50
String: definition and initialization

• First of all, it is an array


• We can initialize it as an array

1 #i n c l u d e < s t d i o . h>
2 i n t main ( )
3 {
4 c h a r ch [ 6 ] = { ’H ’ , ’ e ’ , ’ l ’ , ’ l ’ , ’ o ’ , ’ \0 ’ } ;
5 c h a r ch [ ] = { ’H ’ , ’ e ’ , ’ l ’ , ’ l ’ , ’ o ’ , ’ \0 ’ } ;
6 /∗we h a v e 6 c h a r s t h e r e ∗/
7 c h a r ch [ 6 ] = { ’H ’ , ’ e ’ , ’ l ’ , ’ l ’ , ’ o ’ } ;
8 /∗ ’ \ 0 ’ i s a u t o m a t i c a l l y appended ∗ ∗/
9 c h a r ch [ 6 ] = { ” H e l l o ” } ;
10 c h a r ch [ 6 ] = ” H e l l o ” ;
11 c h a r ch [ ] = ” H e l l o ” ;
12 return 0;
13 }

Introduction to C Programming 42 / 50
String Operation: strcpy

• Copy one string to another


• strcpy(destine, source)

1 #i n c l u d e < s t d i o . h>
2 #i n c l u d e < s t r i n g . h>
3 i n t main ( )
4 { [Output:]
5 c h a r ch [ 1 0 ] ;
6 s t r c p y ( ch , ” h i ” ) ; 1 hi
7 p r i n t f ( ”%s \n” , ch ) ; 2 ha
8 s t r c p y ( ch , ” ha ” ) ;
9 p r i n t f ( ”%s \n” , ch ) ;
10 return 0;
11 }

Introduction to C Programming 43 / 50
String Operation: strcmp (1)
• Compare whether two strings are equal or not

1 #i n c l u d e < s t d i o . h>
2 #i n c l u d e < s t r i n g . h>
3 i n t main ( )
4 {
5 c h a r ch1 [ 1 0 ] , ch2 [ 1 0 ] ;
6 s t r c p y ( ch1 , ” h i ” ) ;
7 s t r c p y ( ch2 , ” ha ” ) ;
8 i f ( s t r c m p ( ch1 , ch2 ) == 1 ) {
9 p r i n t f ( ” ch1 > ch2 \n” ) ; [Output]
10 } e l s e i f ( s t r c m p ( ch1 , ch2 )==−1)
ch1 > ch2
11 {
12 p r i n t f ( ” ch1 < ch2 \n” ) ;
13 }
14 e l s e i f ( s t r c m p ( ch1 , ch2 )==0){
15 printf (” identical ”) ;
16 }
17 }

Introduction to C Programming 44 / 50
String Operation: strcmp (2)
• Compare whether two strings are equal or not
1 #i n c l u d e < s t d i o . h>
2 #i n c l u d e < s t r i n g . h>
3 i n t main ( )
4 {
5 c h a r ch1 [ 1 0 ] , ch2 [ 1 0 ] ;
6 s t r c p y ( ch1 , ” he ” ) ;
7 s t r c p y ( ch2 , ”we” ) ;
8 i f ( s t r c m p ( ch1 , ch2 ) == 1 )
9 {
10 p r i n t f ( ” ch1 > ch2 \n” ) ; [Output]
11 } e l s e i f ( s t r c m p ( ch1 , ch2 )==−1)
ch1 < ch2
12 {
13 p r i n t f ( ” ch1 < ch2 \n” ) ;
14 }
15 e l s e i f ( s t r c m p ( ch1 , ch2 )==0)
16 {
17 printf (” identical ”) ;
18 }
19 }

Introduction to C Programming 45 / 50
String Operation: strcmp (3)
• Compare whether two strings are equal or not
1 #i n c l u d e < s t d i o . h>
2 #i n c l u d e < s t r i n g . h>
3 i n t main ( )
4 {
5 c h a r ch1 [ 1 0 ] , ch2 [ 1 0 ] ;
6 s t r c p y ( ch1 , ” h i ” ) ;
7 s t r c p y ( ch2 , ” h i ” ) ;
8 i f ( s t r c m p ( ch1 , ch2 ) !=0)
9 { [Output]
10 printf (” different ”) ;
identical
11 }
12 e l s e i f ( s t r c m p ( ch1 , ch2 )==0)
13 {
14 printf (” identical ”) ;
15 }
16 return 0;
17 }

Introduction to C Programming 46 / 50
String Operation: strlen (1)

• Calculate the length of the string


• Pass the string until it encounters ‘\0’

1 #i n c l u d e < s t d i o . h> 1 #i n c l u d e < s t d i o . h>


2 #i n c l u d e < s t r i n g . h> 2 #i n c l u d e < s t r i n g . h>
3 i n t main ( ) 3 i n t main ( )
4 { 4 {
5 char a [ 2 0 ] = ” h e l l o ” ; 5 char a [ 2 0 ] = ” h e l l o world ” ;
6 int l = strlen (a) ; 6 int l = strlen (a) ;
7 p r i n t f ( ” l e n g t h i s : %d” , l ) ; 7 p r i n t f ( ” l e n g t h i s : %d” , l ) ;
8 return 0; 8 return 0;
9 } 9 }

[length is: 5] [length is: 11]

Introduction to C Programming 47 / 50
String Operation: strlen (2)

• Calculate the length of the string


• Pass the string until it encounters ‘\0’

1 #i n c l u d e < s t d i o . h> 1 #i n c l u d e < s t d i o . h>


2 #i n c l u d e < s t r i n g . h> 2 #i n c l u d e < s t r i n g . h>
3 i n t main ( ) 3 i n t main ( )
4 { 4 {
5 char a [ 2 0 ] = ” h e l l o world ” ; 5 c h a r a [ 2 0 ] = ” h e l l o \0 w o r l d ” ;
6 int l = strlen (a) ; 6 int l = strlen (a) ;
7 p r i n t f ( ” l e n g t h i s : %d” , l ) ; 7 p r i n t f ( ” l e n g t h i s : %d” , l ) ;
8 return 0; 8 return 0;
9 } 9 }

[length is: 11] [length is: 5]

Introduction to C Programming 48 / 50
String Operation: strcat

• Concatenate two strings into one

1 #i n c l u d e < s t d i o . h>
2 #i n c l u d e < s t r i n g . h>
3 i n t main ( )
4 {
5 char a [ 2 0 ] = ” h e l l o ” ;
1 hello
6 char b [ 1 0 ] = ” world ” ;
2 world
7 p r i n t f ( ” a=%s \n” , a ) ;
3 h e l l o world
8 p r i n t f ( ”b=%s \n” , b ) ;
9 strcat (a , b) ;
10 p r i n t f ( ” a=%s \n” , a ) ;
11 return 0;
12 }

Introduction to C Programming 49 / 50
Summary over string and char array

• Array of chars could be used as string, ‘\0’ should be appended at the


end
• One more byte should be reserved for ‘\0’
• String can be used as an array of chars
• Functions such as “strcpy”, “strlen”, “strcat” etc require string input

Usage Comments
Copy “str2” to “str1”, the content
strcpy(str1, str2)
of “str1” will be overwriten
strlen(str1) Calculate the number of characters before ‘\0’
strcat(str1, str2) Concatenate “str2” to “str1” and save to “str1”
Compare two strings,
strcmp(str1, str2)
returns -1, 1 or 0 if they are identical

Introduction to C Programming 50 / 50

You might also like