The document discusses arrays and strings in C programming. It defines arrays as groups of data items of the same type that share a common name. One-dimensional arrays are declared with a datatype and size, and can be initialized. Multi-dimensional arrays have multiple subscripts. Strings are arrays of characters that are null-terminated. Strings can be initialized and manipulated using functions like strcpy(), strlen(), strcmp(). Examples are provided to demonstrate array and string declarations, initialization, manipulation and differences between the two data structures.
Dr. S. &S.S. Gandhy Government College of Engineering and Technology SEM:- 1ST SUBJECT:- COMPUTER PROGRAMMING AND UTILIZATION TOPIC:- ARRAY AND STRING STUDENT’S NAME:- PRATIK B. PATEL PRASHANT A. CHELANI VIJAY D. VADHER NIKHIL R. PATIL
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.
4.
ARRAY CONCEPT:- Anarray 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.
5.
Elements ofarray 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)
6.
DECLARATION OF ONE- DIMENTIONALARRAY:- 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]
7.
INITIALIZATIONOF ONE-DIMENTIONAL ARRAY Intmark[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’
8.
MULTYDIMENSIONAL ARRAY Ifan 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
INTRODUCTION :- Stringsare 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).
13.
A stringvariable 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
14.
Initializing String Variables Stringsare initialized in either of the following two forms: char name[4]={‘R’,‘A’,‘M’, ‘0’}; char name[]={‘R’,‘A’,‘M’, ‘0’}; OR char 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 0 name[0] name[1] name[2] name[3]
15.
Reading and displayingStrings It can be done manually. Using printf() and scanf(). Using gets() and puts().
String handling functions strcpy( ) Copies str2 into str1 strlen ( ) Gives the length of str1 strcmp ( ) 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 string strlwr ( ) Converts string to lowercase strupr ( ) Converts string to uppercase strrev ( ) Reverses the given string
19.
Program to findstring length using function SOURCE CODE OUTPUT
DIFFERECE BETWEEN ARRAYAND STRING ARRAY STRING An array can hold any data type. String can hold only char data An array size can not be 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).