Top Banner
Dr. S. & S.S. Gandhy Government College of Engineering and Technology SEM:- 1 ST SUBJECT:- COMPUTER PROGRAMMING AND UTILIZATION TOPIC:- ARRAY AND STRING STUDENT’S NAME:- PRATIK B. PATEL PRASHANT A. CHELANI VIJAY D. VADHER NIKHIL R. PATIL
22

Array and string

Jan 11, 2017

Download

Education

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Array and string

Dr. S. & S.S. Gandhy Government College of Engineering and Technology

SEM:- 1ST

SUBJECT:- COMPUTER PROGRAMMING AND UTILIZATIONTOPIC:- ARRAY AND STRINGSTUDENT’S NAME:-PRATIK B. PATEL PRASHANT A. CHELANIVIJAY D. VADHERNIKHIL R. PATIL

Page 2: Array and string

ARRAY :- NEED OF ARRAY ARRAY CONCEPT DECLARATION OF ONE-

DIMENTIONAL ARRAY INITIALIZATION OF ONE-

DIMENTIONAL ARRAY MULTYDIMENSIONAL ARRAY

Page 3: Array and string

NEED OF ARRAY: It is very difficult to write a program

in ‘C’ which consisting large data items like addition of 50 integers, marks of student in University, etc.

‘C’ program provides a solution called ‘ARRAY’.

By using array, it is easy to include large numbers of data items.

Page 4: Array and string

ARRAY CONCEPT:- An array is a group of data items of the

same data type that share a common name.

An array should be of same datatype and consists of integers or strings and so on.

An array is linear and homogeneous. An array stores the data elements in

sequential order. Homogeneous means all data items are of

same datatype.

Page 5: Array and string

Elements of array are specifying a subscript.

A subscript is also called index. Subscipt is start from 0 and cannot

negative. There are two types of array. 1). One-dimentional arrays(also called

vectors) 2). Multi-dimentional arrays(also called

Matrix)

Page 6: Array and string

DECLARATION OF ONE-DIMENTIONAL ARRAY:- Syntex: datatype arrayname[size] Where, datatype:- The type of the data stored in

the array Arrayname:- Name of the array Size:- Maximum number of elements an

array can hold Example:- int marks[10]

Mark[0] Mark[1] ………….. ……….....

Mark[9]

Page 7: Array and string

INITIALIZATION OF ONE-DIMENTIONAL ARRAY Int mark[6]={3,7,8,4,5,6} Is also equel to Int mark[]={3,7,8,4,5,6} It will occupy in memory like,

a[0] a[1] a[2] a[3] a[4] a[5] Char a[8]={‘L’,’E’,’A’,’R’,’N’,’’,’C’}

a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] When compiler sees a character array, it add a null

character.So, while declaring a character array, we must allow one extra element space for null character.

3 7 8 4 5 6

‘L’ ‘E’ ‘A’ ‘R’ ‘N’ ‘’ ‘C’ ‘\0’

Page 8: Array and string

MULTYDIMENSIONAL ARRAY If an array have more than one dimension, is called

multi-dimensional array. Two dimensional array have two subscript, three

dimensional array have three subscript. Declaration of two dimensional array:- Int a[2][3] It consist of two rows and three colomns. a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2]

1 2 3 4 5 6

Page 9: Array and string

PROGRAM OF SIMPLE ARRAY

SOURCE CODE OUTPUT

Page 10: Array and string

Program to reverse a array

SOURCE CODE OUTPUT

Page 11: Array and string

Strings INTRODUCTION INITIALIZING STRING VARIABLES READING AND DISPLAYING STRINGS STRING HANDLING FUNCTIONS PROGRAMS OF STRING

Page 12: Array and string

INTRODUCTION :- Strings are array of characters i.e. they are

characters arranged one after another in memory. Thus, a character array is called string.

Each character within the string is stored within one element of the array successively.

A string is always terminated by a null character (i.e. slash zero \0).

Page 13: Array and string

A string variable is declared as an array of characters.

Syntax:char string_name[size];

E.g. char name[20]; When the compiler assigns a character

string to a character array, it automatically supplies a null character (‘\0’) at the end of the string

Page 14: Array and string

Initializing String Variables

Strings are initialized in either of the following two forms:char name[4]={‘R’,‘A’,‘M’, ‘\0’};char name[]={‘R’,‘A’,‘M’, ‘\0’};

ORchar name[4]=“RAM”;char name[]=“RAM”;

When we initialize a character array by listing its elements, the null terminator or the size of the array must be provided explicitly.

R A M \0name[0

]name[1

]name[2] name[3

]

Page 15: Array and string

Reading and displaying Strings

It can be done manually.Using printf() and scanf().Using gets() and puts().

Page 16: Array and string

Using printf() and scanf()

SOURCE CODE OUTPUT

Page 17: Array and string

Using gets() and puts()

SOURCE CODE OUTPUT

Page 18: Array and string

String handling functions

strcpy ( ) Copies str2 into str1strlen ( ) Gives the length of str1strcmp ( ) Returns 0 if str1 is same as

str2. Returns <0 if strl < str2. Returns >0 if str1 > str2

strcmpi ( ) Same as strcmp() function. But, this function negotiates case.  “A” and “a” are treated as same.

strdup ( ) Duplicates the stringstrlwr ( ) Converts string to

lowercasestrupr ( ) Converts string to

uppercasestrrev ( ) Reverses the given string

Page 19: Array and string

Program to find string length using function

SOURCE CODE OUTPUT

Page 20: Array and string

Program to Reverse string using function

SOURCE CODE OUTPUT

Page 21: Array and string

DIFFERECE BETWEEN ARRAY AND STRINGARRAY STRINGAn array can hold any data type.

String can hold only char data

An array size can not be changed.

A string size can be changed if it is a char pointer

The last element of an array is an element of the specific type.

The last character of a string is a null – ‘\0’ character.

The length of an array is to specified in [] at the time of declaration (except char[]).

The length of the string is the number of characters + one (null character).     

Page 22: Array and string