UNIT - 3 ARRAYS AND STRINGS. Array An Array is a collection of similar data items, that are stored under a common name. Types –One-Dimensional array –Two-Dimensional.

Slides:



Advertisements
Similar presentations
 A string is an array of characters.  Strings must have a 0 or null character after the last character to show where the string ends.  The null character.
Advertisements

Strings.
Lecture 09 Strings, IDEs. METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 29, 2002.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Character String Manipulation. Overview Character string functions sscanf() function snprintf() function.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Strings CS240 Dick Steflik. What is a string A null terminated array of characters: char thisIsAString[10]; \0 The “\0” (null character)
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Chapter Fourteen Strings Revisited. Strings A string is an array of characters A string is a pointer to a sequence of characters A string is a complete.
Pointers in C Rohit Khokher
Chapter 9 Character Strings
To remind us We finished the last day by introducing If statements Their structure was:::::::::
Chapter 9: Arrays and Strings
Strings String - a string is a series of characters treated as a unit. A C string is a variable-length array of characters that is delimited by the null.
Introduction of Arrays. Arrays Array form an important part of almost all programming language. It provides a powerful feature and can be used as such.
Chapter 8 Arrays and Strings
C-strings Array with base type char One character per indexed variable
Strings. Sentences in English are implemented as strings in the C language. Computations involving strings are very common. E.g. – Is string_1 the same.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Arrays and Strings Gabriel Hugh Elkaim Spring 2013.
Data Structure and C Part-6. Naming a Function Any valid variable-name can be given to the user-defined function. The main program itself is considered.
More Questions 1) Write a C program to read a matrix A of size nXn and two arrays X and Y of size n and calculate XA-Y?
1. An array is a collection of a fixed number of components wherein all of the components are of the same type Example: Suppose that there is a list of.
While Loop Structure while (condition) { …. // This line will process when while condition is true }
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
Chapter 8 Arrays and Strings
Write a C program to pass an array containing age of person to a function. This function should find average age and display the average age in main function.
Topics to be covered  Introduction to array Introduction to array  Types of array Types of array  One dimensional array One dimensional array  Declaration.
CS 162 Introduction to Computer Science Chapter 17 C++ String Objects Herbert G. Mayer, PSU (Copied from Prof. Phillip Wong at PSU) Status 11/30/2014.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.
Chapter 8: Character and String CMPD144: Programming 1.
Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
Structured Programming Approach Module VIII - Additional C Data Types Arrays Prof: Muhammed Salman Shamsi.
1 Arrays 1090CS, Computer Programming 1 Manesh T
Strings program. C Program to Check if a given String is Palindrome #include void main() { char string[25], reverse_string[25] = {'\0'}; int i, length.
Strings program. C Program to Check if a given String is Palindrome #include void main() { char string[25], reverse_string[25] = {'\0'}; int i, length.
String Array (Multidimensional Arrays) 1. A string array is a multidimensional array of strings. It is declared in the following syntax: char variable_name[No_of_strings][size_of_each_string];
UNIT-4 1. Arrays: Definition and declaration, Initialization, Accessing elements of arrays, Storing values in arrays, Inter-function Communication: Passing.
UniMAP SEM I - 09/10EKT 120 Computer Programming1 Lecture 8 – Arrays (2) & Strings.
UniMAP SEM I - 10/11EKT 120 Computer Programming1 Lecture 8 – Arrays (2) and Strings.
Write a C program to pass an array containing age of person to a function. This function should find average age and display the average age in main function.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
In C programming, one of the frequently arising problem is to handle similar types of data. For example: If the user want to store marks of 100 students.
Arrays C provides the option to the user to combine similar data types into a single entity It followed contiguous memory allocation.
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal may.
ECE 103 Engineering Programming Chapter 29 C Strings, Part 2 Herbert G. Mayer, PSU CS Status 7/30/2014 Initial content copied verbatim from ECE 103 material.
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Explain Declaration,Initialization of Array Explain Types of Array One Dimensional,Two Dimensional and Multi Dimensional Array Explain Arrays.
MAHENDRAN. Session Objectives Explain Declaration,Initialization of Array Explain Types of Array One Dimensional,Two Dimensional and Multi Dimensional.
UNIT - 5 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.
Chapter 2 Array and String. Array Definition of Array : An array is a sequence or collection of the related data items that share a common name. Purpose.
C ARRAYS.
C Scope Rules and Arrays
MULTI-DIMENSIONAL ARRAY
Module 2 Arrays and strings – example programs.
ARRAYS An array is a sequence of data item of homogeneous value(same type). Arrays are of two types: 1. One-dimensional arrays 2. Multi-Dimensional arrays.
Arrays C provides the option to the user to combine similar data types into a single entity It followed contiguous.
FUNCTIONS AND POINTERS
Chapter 2 Array and String Visit to more Learning Resources.
UNIT - 3 Noornilo Nafees.
Arrays C provides the option to the user to combine similar data types into a single entity It followed contiguous.
Visit for more Learning Resources
Presentation transcript:

UNIT - 3 ARRAYS AND STRINGS

Array An Array is a collection of similar data items, that are stored under a common name. Types –One-Dimensional array –Two-Dimensional array – Multi-Dimensional array

One-Dimensional array Array Declaration Syntax: data_type array_name[size]; Example: int x[3]; X[0] X[1] X[2] x

Array initialization At compile time At run time

At compile time Syntax: data_type array_name[size]={variables}; Example: int x[3]={5,3,7}; X[0] X[1] X[2] x

At Run time Array can also initialize at the run time. Example: while(i<10) { if(i<5) sum[i]=0; else sum[i]=sum[i]+i; }

Example: scanf(“%d%d”,&a[0],&a[1]);

Example #include void main() { int x[2],i; printf("\nEnter the inputs:"); for(i=0;i<2;i++) scanf("%d",&x[i]); for(i=0;i<2;i++) printf("\nThe value in x[%d] is %d",i,x[i]); getch(); }

Output Enter the inputs:3 6 The value in x[0] is 3 The value in x[1] is 6

Example #include void main() { int i; char x[5]={'a','b','c','d','e'}; clrscr(); for(i=0;i<5;i++) printf("\nThe value in x[%d] is %c",i,x[i]); getch(); }

Output The value in x[0] is a The value in x[1] is b The value in x[2] is c The value in x[3] is d The value in x[4] is e

Two-Dimensional array Array Declaration Syntax: data_type array_name[row_size] [col_size]; Example: int x[3][2]; X[0][0] X[1][0] X[2][0] Col 0 Col 1 row 0 row 1 row 2 X[0][1] X[1][1] X[2][1]

Array Initialization Syntax: data_type array_name[row_size] [col_size];={variables}; Example: int x[2][2]={1,50,2,75};

int x[2][2]={ {1,50}, {2,75} }; (or) int x[ ][2]={ {1,50}, {2,75} };

row 0 row 1 Col 0 Col 1

Example #include void main() { int i,j; int x[2][2]={{1,50}, {2,75} }; clrscr(); for(i=0;i<2;i++) for(j=0;j<2;j++) printf("\nThe value in x[%d][%d] is %d",i,j,x[i][j]); getch(); }

Output The value in x[0][0] is 1 The value in x[0][1] is 50 The value in x[1][0] is 2 The value in x[1][1] is 75

Example #include void main() { int i,j; int x[][2]={ {1,50},{2,75},{3,65}}; clrscr(); for(i=0;i<=2;i++) for(j=0;j<2;j++) printf("\nThe value in x[%d][%d] is %d",i,j,x[i][j]); getch(); }

Output The value in x[0][0] is 1 The value in x[0][1] is 50 The value in x[1][0] is 2 The value in x[1][1] is 75 The value in x[2][0] is 3 The value in x[2][1] is 65

Matrix Addition #include void main() { int i,j,k,r1,r2,c1,c2; int a[5][5],b[5][5],c[5][5]; clrscr(); step1: printf("\n Enter the size of matrix A:"); scanf("%d%d",&r1,&c1); printf("\n Enter the size of matrix B: "); scanf("%d%d",&r2,&c2); if((c1==c2)&&(r1==r2)) goto step2; else goto step1;

step2: printf("\n Enter the elements of matrix A \n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { scanf("%d",&a[i][j]); } printf("\n Enter the elements of matrix B \n"); for(i=0;i<r2;i++) { for(j=0;j<c2;j++) { scanf("\t%d",&b[i][j]); }

for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { c[i][j]=0; c[i][j]=c[i][j]+a[i][j]+b[i][j]; } printf("\n The resultant matrix after addition of A & B is\n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) printf("%d\t",c[i][j]); printf("\n"); } getch(); }

Output Enter the size of matrix A: 2 2 Enter the size of matrix B: 2 2 Enter the elements of matrix A 2 Enter the elements of matrix B 3 The resultant matrix after addition of A&B is 5

Matrix Multiplication #include void main() { int i,j,k,r1,r2,c1,c2; int a[5][5],b[5][5],c[5][5]; clrscr(); step1: printf("\n Enter the size of matrix A \n"); scanf("%d%d",&r1,&c1); printf("\n Enter the size of matrix B \n"); scanf("%d%d",&r2,&c2); if(c1==r2) goto step2; else goto step1;

step2: printf("\n Enter the elements of matrix A \n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { scanf("%d",&a[i][j]); } printf("\n Enter the elements of matrix B \n"); for(i=0;i<r2;i++) { for(j=0;j<c2;j++) { scanf("\t%d",&b[i][j]); }

for(i=0;i<r1;i++) { for(j=0;j<c2;j++) { c[i][j]=0; for(k=0;k<c1;k++) { c[i][j]=c[i][j]+a[i][k]*b[k][j]; } for(i=0;i<r1;i++) { for(j=0;j<c2;j++) printf("%d\t",c[i][j]); printf("\n"); } getch(); }

Output Enter the size of matrix A:2 2 Enter the size of matrix B:2 2 Enter the elements of matrix A 4 Enter the elements of matrix B 4 The resultant matrix is 32

Enter the size of matrix A:2 3 Enter the size of matrix B:3 2 Enter the elements of matrix A Enter the elements of matrix B

Passing array to Function Here an array is transferred as parameter to a function. void main() void fun(n,b[]) { void fun(int,int);int x,b[5]; int a[5],n;………….. ……………………….. fun(n,a); …………… } }

Example #include void add(int,int b[]); void main() { int a[5],i,n; clrscr(); printf("\n Enter the Number: "); scanf("%d",&n); printf("\n Enter the Values: "); for(i=0;i<n;i++) scanf("%d",&a[i]); add(n,a); }

void add(int x,int b[]) { int sum=0,i; for(i=0;i<x;i++) sum=sum+b[i]; printf("\nThe sum is: %d",sum); }

Output Enter the Number: 5 Enter the Values: The sum is: 15

Array of Characters In array the characters are terminated by the null (‘\0’) character. Example: char a[]={a,b,c}; abc\0

Example #include void main() { int i=0; char a[]="abcd"; clrscr(); while(a[i]!='\0') { printf("\t%c",a[i]); i++; }

Output a b c d

Multi Dimensional Array Syntax datatype array_name [size1][size2]….[size n] datatype- type of the data. array_name-name of the array. size-size of the array.

Example: int a[3][3][3]; Col 0 Col 1 Col 2 row 0 row 1 row 2 X[0][0] X[1][0] X[2][0] X[0][1] X[1][1] X[2][1] X[0][2] X[1][2] X[2][2]

String Functions strlen() It is used to find the length of the string. syntax: strlen(string) strcpy() It is used to copy one string to another. syntax: strcpy(string1,string2) strcat() It is used to combine two strings. syntax: strcat(string1,string2)

strcmp() It is used to compare two strings. syntax: strcmp(string1,string2) –Returns 0 if two strings are equal. –Return value <0 if s1 is less than s2. –Return value >0 if s1 is greater than s2. strrev() It used to reverse a string. syntax: strrev(string) strlwr(), strupr() It used to change the case of a string. syntax: strlwr(string) strupr(string)

strncpy() It used to copy ‘n’ characters of one string to another. strstr() –It is used to determine the first occurrence of a given string in another string. strncat() –It Appends source string to destination string upto specified length. strspn() –It is used t find upto what length two strings are identical.

strncmp() – It is used to compare ‘n’ character of two strings. strcmpi() – It is used to compare two strings without regarding the case. strnicmp() – It is used to compare first ‘n’ characters of two strings without regarding the case. strchr() – It is used to determine the first occurrence of a given character in a string. strrchr() – It is used to determine the last occurrence of a given character in a string.

Example #include void main() { char a[]="college"; int b; clrscr(); b=strlen(a); printf("\nThe length of the string is %d",b); getch(); } Output: The length of the string is 7

Example #include void main() { char a[]="IT"; char b[]="Dept"; clrscr(); strcpy(a,b); printf("\nThe string is %s",a); getch(); } Output: The string is Dept

Example #include void main() { char a[]="IT"; char b[]="Dept"; clrscr(); strcat(a,b); printf("\nThe string is %s",a); getch(); } Output: The string is ITDept

Example #include void main() { char a[]="itdept"; char b[]="it"; int i; clrscr(); i=strcmp(a,b); if(i==0) printf("\nstrings are equal:%d",i); else if(i<0) printf("\nstring1 is less than string2:%d",i);

else printf("\nstring1 is greater than string2:%d",i); getch(); } Output: string1 is greater than string2:100

Example #include void main() { char a[]="itdept"; clrscr(); printf("\nThe string is :%s",a); strupr(a); printf("\nThe string after conversion to uppercase :%s",a); strlwr(a); printf("\nThe string after conversion to lowercase :%s",a); getch(); }

Output The string is :itdept The string after conversion to uppercase :ITDEPT The string after conversion to lowercase :itdept

Example #include void main() { char a[]="Dept"; clrscr(); printf("\nThe string is %s",strrev(a)); getch(); } Output: The string is tpeD

Example #include void main() { char a[]="itdept"; char b[15]; int i=0; clrscr(); strncpy(b,a,2); b[2]='\0'; printf("\nThe string is :%s",b); getch(); }

Output: The string is :it

String Palindrome #include void main() { int len,i,j; char str[15]; clrscr(); printf("\n Enter the string:"); scanf("%s",str); len=strlen(str);

for(i=0,j=len-1;i<len/2;i++,j--) { if(str[i]!=str[j]) { printf("\nThe String is not a palindrome"); getch(); exit(0); } printf("\nThe String is a palindrome"); getch(); } Output: Enter the string:abcba The String is a palindrome

Enumerated Data Type It is user defined data type. The user can create their own data type and define some values to the variables. Syntax: enum tag_name { enum1; enum2; ………. };