0% found this document useful (0 votes)
11 views27 pages

CH 4

string and array s gtu

Uploaded by

23ae9
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)
11 views27 pages

CH 4

string and array s gtu

Uploaded by

23ae9
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/ 27

CH -4 ARRAY & STRING

Need of Array Variable


◻ Suppose we need to store rollno of the student in the
integer variable.
Declaration
int rollno;

◻ Now we need to store rollno of 100 students.


Declaration
int rollno101, rollno102, rollno103, rollno104...;

◻ This is not appropriate to declare these many integer


variables.
e.g. 100 integer variables for rollno.
◻ Solution to declare and store multiple variables of similar
type is an array.
◻ An array is a variable that can store multiple values.
Definition: Array
◻ An array is a fixed size sequential collection of
elements of same data type grouped under single
variable name. [99
[0] [1] [2] … ]
int rollno[100]; [0] [1] [2] … [99]

Fixed Size Sequential Same Data type Single Name


Here, the size of It is indexed to 0 All the elements All the elements
an array is 100 to 99 in (0-99) will be (0-99) will be
(fixed) to store sequence integer variables referred as a
rollno common name
rollno
Declaring an array
◻ By default array index
Syntax
starts with 0.
data-type variable-name[size];◻ If we declare an array of
Integer Array size 5 then its index
int mark[5]; ranges from 0 to 4.
◻ First element will be store
[0] [1] [2] [3] [4]
at mark[0] and last
element will be stored at
integer mark[4] not mark[5].
Float Array ◻ Like integer and float
float avg[5]; array we can declare array
of type char.
[0] [1] [2] [3] [4]
float
Initialing and Accessing an Array

Declaring, initializing and accessing single integer


variable
int mark=90; //variable mark is initialized with value 90
printf("%d",mark); //mark value printed

Declaring, initializing and accessing integer array


variable
int mark[5]={85,75,76,55,45};//mark is initialized with 5 values
printf("%d",mark[0]); //prints 85
printf("%d",mark[1]); //prints 75
printf("%d",mark[2]); //prints 65
printf("%d",mark[3]); //prints 55
printf("%d",mark[4]); //prints 45

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


mark[5] 85 75 65 55 45
Read(Scan) Array Elements
Reading array without loop
1 void main()
2 {
3 int mark[5];
printf("Enter array element=");
4
scanf("%d",&mark[0]);
5 printf("Enter array element=");
6 scanf("%d",&mark[1]);
7 printf("Enter array element=");
8 scanf("%d",&mark[2]);
9 printf("Enter array element=");
10 scanf("%d",&mark[3]);
11 printf("Enter array element=");
scanf("%d",&mark[4]);
12
printf("%d",mark[0]);
13 printf("%d",mark[1]);
14 printf("%d",mark[2]);
15 printf("%d",mark[3]);
16 printf("%d",mark[4]); [0] [1] [2] [3] [4]
17 } mark[5] 85 75 65 55 45
18
Read(Scan) Array Elements
Reading array using loop
1 void main()
2 {
3 int mark[5],i;
for(i=0;i<5;i++)
4
{
5 printf("Enter array element=");
6 scanf("%d",&mark[i]);
7 }
8 for(i=0;i<5;i++)
9 {
10 printf("%d",mark[i]);
11 }
}
12

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


mark[5] 85 75 65 55 45
WAP to count number of positive or negative number from an
array of 10 numbers.
Program
1 void main(){
2 int num[10],i,pos,neg; Output
3 pos = 0; Enter array element=1
4 neg = 0; Enter array element=2
5 for(i=0;i<10;i++) Enter array element=3
6 { Enter array element=4
7 printf("Enter array element="); Enter array element=5
8 scanf("%d",&num[i]); Enter array element=-1
9 } Enter array element=-2
10 for(i=0;i<10;i++) Enter array element=3
11 { Enter array element=4
12 if(num[i]>0) Enter array element=5
13 pos=pos+1; Positive=8,Negative=2
14 else
15 neg=neg+1;
16 }
17 printf("Positive=%d,Negative=%d",pos,neg);
18 }
WAP to read n numbers in an array and print them in reverse order.

Program
1 void main()
2 { Output
3 int num[100],n,i; Enter number of array
4 printf("Enter number of array elements="); elements=5
5 scanf("%d",&n); Enter array element=1
6 //loop will scan n elements only Enter array element=2
7 for(i=0;i<n;i++) Enter array element=3
8 { Enter array element=4
9 printf("Enter array element="); Enter array element=5
10 scanf("%d",&num[i]); 5
11 } 4
12 //negative loop to print array in reverse order3
13 for(i=n-1;i>=0;i--) 2
14 { 1
15 printf("%d\n",num[i]);
16 }
17 }
Multi Dimensional Array
Declaring 2 Dimensional Array
Syntax ◻ A two
data-type variable-name[x][y];
dimensional array
Declaration can be seen as a
int data[3][3]; //This array can hold 9 elementstable with ‘x’
rows and ‘y’
columns.
int data[3][3];
◻ The row number
Column-0 Column-1 Column-2
ranges from 0 to
Row-0 data[0][0] data[0][1] data[0][2] (x-1) and
Row-1 data[1][0] data[1][1] data[1][2] column number
Row-2 data[2][0] data[2][1] data[2][2]
ranges from 0 to
(y-1).
Initialing and Accessing a 2D Array: Example-1
Program
1 int data[3][3] = {
2 {1,2,3}, //row 0 with 3 elements
3 {4,5,6}, //row 1 with 3 elements
4 {7,8,9} //row 2 with 3 elements
5 };
6 printf("%d",data[0][0]); //1 Column-0 Column-1 Column-2
7 printf("%d",data[0][1]); //2
8 printf("%d\n",data[0][2]); //3 Row-0 1 2 3
9
10 printf("%d",data[1][0]); //4 Row-1 4 5 6
11 printf("%d",data[1][1]); //5
12 printf("%d\n",data[1][2]); //6 Row-2 7 8 9
13
14 printf("%d",data[2][0]);//7
15 printf("%d",data[2][1]); //8
16 printf("%d",data[2][2]); //9
1 // data[3][3] can be initialized like this also
2 int data[3][3]={{1,2,3},{4,5,6},{7,8,9}};
Initialing and Accessing a 2D Array: Example-2

Program
1 int data[2][4] = {
2 {1,2,3,4}, //row 0 with 4 elements
3 {5,6,7,8}, //row 1 with 4 elements
4 };
5 printf("%d",data[0][0]); //1
6 printf("%d",data[0][1]); //2 Col-0 Col-1 Col-2 Col-3
7 printf("%d",data[0][2]); //3
8 printf("%d\n",data[0][3]); //4 Row-
9
1 2 3 4
0
10 printf("%d",data[1][0]); //5
11 printf("%d",data[1][1]); //6 Row-
5 6 7 8
12 printf("%d",data[1][2]); //7 1
13 printf("%d",data[1][3]); //8

1 // data[2][4] can be initialized like this also


2 int data[2][4]={{1,2,3,4},{5,6,7,8}};
Read(Scan) 2D Array Elements
Program
void main(){
1 int data[3][3],i,j;
2 for(i=0;i<3;i++)
3 {
4 for(j=0;j<3;j++)
5 {
6 printf("Enter array element=");
7 scanf("%d",&data[i][j]); Output
8 } Enter array element=1
9 } Enter array element=2
10 for(i=0;i<3;i++) Enter array element=3
11 { Enter array element=4
12 for(j=0;j<3;j++) Enter array element=5
13 { Enter array element=6
14 printf("%d",data[i][j]); Enter array element=7
15 } Enter array element=8
16 printf("\n"); Enter array element=9
17 } 123
18} 456
19 789
Develop a program to count number of positive, negative and zero
elements from 3 X 3 matrix
Program
1 void main(){
2 int data[3][3],i,j,pos=0,neg=0,zero=0; Output
3 for(i=0;i<3;i++) Enter array element=9
4 { Enter array element=5
5 for(j=0;j<3;j++) Enter array element=6
6 { Enter array element=-3
7 printf("Enter array element=");Enter array element=-7
8 scanf("%d",&data[i][j]); Enter array element=0
9 if(data[i][j]>0) Enter array element=11
10 pos=pos+1; Enter array element=13
11 else if(data[i][j]<0) Enter array element=8
12 neg=neg+1; positive=6,negative=2,z
13 else ero=1
14 zero=zero+1;
15 }
16 }
17 printf("positive=%d,negative=%d,zero=%d",pos,neg,zero);
18 }
String
(Character Array)
Definition: String
◻ A String is a one-dimensional array of characters
terminated by a null('\0').
char name[10]; [0] [1] [2] … [9]

◻ Each character in the array occupies one byte of


memory, and the last character must always be
null('\0').
◻ The termination character ('\0') is important in a
string to identify where the string ends.
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
name[10]
A J A Y S I N H \0
Declaring & Initializing String

Declaration
char name[10];

Initialization method 1:
char name[10]={'D','A','R','S','H','A','N','\0'};

Initialization method 2:
char name[10]="DARSHAN";
//'\0' will be automatically inserted at the end in this type o
f declaration.

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
name[10] D A R S H A N \0
Read String: scanf()
Program
Output
1 void main()
2 { Enter name: Darshan
3 char name[10]; Name=Darshan
4 printf("Enter name:"); Output
5 scanf("%s",name);
Enter name: CE Darshan
6 printf("Name=%s",name);
Name=CE
7 }

◻ There is no need to use address of (&) operator in scanf to store a


string.
◻ As string name is an array of characters and the name of the array,
i.e., name indicates the base address of the string (character array).
◻ scanf() terminates its input on the first whitespace(space, tab,
newline etc.) encountered.
Read String: gets()
Progra
m
1 #include<stdio.h> Output
2 void main() Enter name:Darshan
3 { Institute
4 char name[10]; Name=Darshan
5 printf("Enter name:"); Institute
6 gets(name); //read string including
7 white spaces
8 printf("Name=%s",name);
}
◻ gets(): Reads characters from the standard input and stores them as a
string.
◻ puts(): Prints characters from the standard.
◻ scanf(): Reads input until it encounters whitespace, newline or End
Of File(EOF) whereas gets() reads input until it encounters newline
or End Of File(EOF).
◻ gets(): Does not stop reading input when it encounters whitespace
instead it takes whitespace as a string.
String Handling Functions : strlen()
◻ C has several inbuilt functions to operate on string.
These functions are known as string handling functions.
◻ strlen(s1): returns length of a string in integer
Program
1 #include <stdio.h>
2 #include <string.h> //header file for string functions
3 void main()
4 {
5 char s1[10];
6 printf("Enter string:");
7 gets(s1);
8 printf("%d",strlen(s1)); // returns length of s1 in integer
9 }
Output
Enter string: CE Darshan
10
String Handling Functions: strcmp()
◻ strcmp(s1,s2): Returns 0 if s1 and s2 are the same.
◻ Returns less than 0 if s1<s2.
◻ Returns greater than 0 if s1>s2.
Program
1 void main() Output
2 { Enter string-1:Computer
3 char s1[10],s2[10]; Enter string-2:Computer
4 printf("Enter string-1:"); Strings are same
5 gets(s1);
6 printf("Enter string-2:"); Output
7 gets(s2); Enter string-1:Computer
8 if(strcmp(s1,s2)==0) Enter string-2:Computer
9 printf("Strings are same"); Strings are same
10 else
11 printf("Strings are not same");
12 }
String Handling Functions
For examples consider: char s1[]="Their",s2[]="There";
Syntax Description
strcpy(s1,s2 Copies 2nd string to 1st string.
) strcpy(s1,s2) copies the string s2 in to string s1 so s1
is now “There”. s2 remains unchanged.

strcat(s1,s2 Appends 2nd string at the end of 1st string.


) strcat(s1,s2); a copy of string s2 is appended at the
end of string s1. Now s1 becomes “TheirThere”

strchr(s1,c) Returns a pointer to the first occurrence of a given


character in the string s1.
printf("%s",strchr(s1,'i'));
Output : ir
strstr(s1,s2 Returns a pointer to the first occurrence of a given string
) s2 in string s1. printf("%s",strstr(s1,"he"));
Output : heir
String Handling Functions (Cont…)
For examples consider: char s1[]="Their",s2[]="There";
Syntax Description

strrev(s1) Reverses given string.


strrev(s1); makes string s1 to “riehT”

strlwr(s1) Converts string s1 to lower case.


printf("%s",strlwr(s1)); Output : their

strupr(s1) Converts string s1 to upper case.


printf("%s",strupr(s1)); Output :
THEIR
strncpy(s1,s2,n) Copies first n character of string s2 to string s1
s1=""; s2="There";
strncpy(s1,s2,2);
printf("%s",s1); Output : Th

strncat(s1,s2,n) Appends first n character of string s2 at the end of string s1.


strncat(s1,s2,2);
printf("%s", s1); utput :
TheirTh
String Handling Functions (Cont…)

For examples consider: char s1[]="Their",s2[]="There";


Syntax Description
strncmp(s1,s2,n) Compares first n character of string s1 and s2 and
returns similar result as strcmp() function.
printf("%d",strcmp(s1,s2,3));
Output : 0

strrchr(s1,c) Returns the last occurrence of a given character in a


string s1. printf("%s",strrchr(s2,'e'));
Output : ere
Any Questions???
Thank you !!

You might also like