KEMBAR78
Introduction to array and string | PPTX
INTRODUCTION TO ARRAY AND STRING
Team Members:
Sajid Anowar
ID: 191-35-390
Muntasir Muhit
ID: 191-35-399
Array
CONTENT:
• Array
• Types of Array
Array
An array is a collection of one or more values of the
same type. Each value is called an element of
the array. The elements of the array share the same
variable name but each element has its own unique
index number (also known as a subscript). An
array can be of any type:
For example: int , float , char etc.
Syntax
data-type name[size1][size2]………[sizen];
Example: int a[6];
Array
Advantage of Array
• Huge amount of data can be stored under single variable name.
• Searching of data item is faster.
• 2 dimension arrays are used to represent the matrices.
• It is helpful in implementing other data structure like linked list,
queue, stack.
Types of Array
One Dimensional Array
A one-dimensional
array (or single dimension
array) is a type of
linear array. Accessing its
elements involves
a single subscript which can
either represent a row or
column index.
SYNTAX: data-type name[index];
EXAMPLE: int num[10];
Initialization
• int num[6]={2,4,6,7,8,12};
• Individual elements can also be initialize as:
num[0]=2;
num[1]=4;
num[2]=6;
num[3]=7;
num[4]=8;
num[5]=12;
Reading Data from User
• for loop is used to read data from the user.
for(i=0;i<10;i++)
{
scanf(“%d”,&num[i]);
}
MULTI DIMENTIONAL ARRAY
The two dimensional (2D) array
in C programming is also
known as matrix. A matrix
can be represented as a
table of rows and columns
SYNTAX: data-type name[row-size][column-size];
EXAMPLE: int a[3][4];
Initialization
• int odd[3][2]={1,3,5,7,9,11};
Individual element can also be assigned as:
Odd[0][0]=1;
Odd[0][1]=3;
Odd[1][0]=5;
Odd[1][1]=7;
Odd[2][0]=9;
Odd[2][1]=11;
Reading Data from User
• Nested for Loop is used.
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
scanf(“%d”,&odd[i] [j]);
}
STRING
WHAT IS STRING ?
A group of characters A string constant is a one-
dimensional array
of characters terminated by a null ( ‘0’ ).
declaration of Character:
• char mychar;
declaration of String:
• char myString[10];
The characters after the null character are ignored.
WHAT IS NULL char ?
• Null ‘0’ is a terminator which terminates the string in an array
WHY NULL char ?
When declaring a string don’t forget to leave a space for the null
character which is also known as the string terminator character
only way the functions that work with a string can know where the
string ends.
INPUT FUNCTION
• Input Function :
The scanf() Function
• header file stdio.h
• Syntax:
• char mystring[100];
• scanf(“%s”, mystring);
The name of a string is a pointer constant to the first character in the character
array.
Problem:
terminates its input on the first white space it finds.
white space includes blanks, tabs, carriage returns(CR), form feeds & new line.
EXAMPLE
PROGRAM OUTPUT
INPUT FUNCTION
The gets() Function:
Header file stdio.h
takes a string from standard input and assigns it to a character array.
It replaces the n with 0.
Syntax:
• char mystring[100];
• gets(myString);
• fgets() it keeps the n and includes it as part of the string.
EXAMPLE
PROGRAM OUTPUT
OUTPUT FUNCTION
The printf () function
header file: stdio.h
The puts() function
header file: stdio.h
PROGRAM
OUTPUT
STRING OPERATION(string.h)
Four main library function which is define in
• string.h header file
• strcpy() - copy one string into another
• strcat() - append one string onto the right side of
• the other
• strcmp() – compare alphabetic order of two
• strings
• strlen() – return the length of a string
Strcpy()
PROGRAM OUTPUT
strcat()
PROGRAM OUTPUT
Strcmp()
PROGRAM OUTPUT
Strlen()
PROGRAM OUTPUT
MORE FUNCTIONS
• strlwr() : converts a string to lowercase
• Strupr() : converts a string to uppercase
• Strncat() : Appends first n characters of a string at the end of
another
• Strncmp() :Compares first n characters of two strings
• Strcmpi():Compares two strings without regard to case ("i" denotes
that this function ignores Case)
• [Note: There are more library functions…..]
EXAMPLE
PROGRAM OUTPUT
Introduction to array and string

Introduction to array and string

  • 1.
    INTRODUCTION TO ARRAYAND STRING Team Members: Sajid Anowar ID: 191-35-390 Muntasir Muhit ID: 191-35-399
  • 2.
  • 3.
    Array An array isa collection of one or more values of the same type. Each value is called an element of the array. The elements of the array share the same variable name but each element has its own unique index number (also known as a subscript). An array can be of any type: For example: int , float , char etc.
  • 4.
  • 5.
  • 6.
    Advantage of Array •Huge amount of data can be stored under single variable name. • Searching of data item is faster. • 2 dimension arrays are used to represent the matrices. • It is helpful in implementing other data structure like linked list, queue, stack.
  • 7.
  • 8.
    One Dimensional Array Aone-dimensional array (or single dimension array) is a type of linear array. Accessing its elements involves a single subscript which can either represent a row or column index. SYNTAX: data-type name[index]; EXAMPLE: int num[10];
  • 9.
    Initialization • int num[6]={2,4,6,7,8,12}; •Individual elements can also be initialize as: num[0]=2; num[1]=4; num[2]=6; num[3]=7; num[4]=8; num[5]=12;
  • 10.
    Reading Data fromUser • for loop is used to read data from the user. for(i=0;i<10;i++) { scanf(“%d”,&num[i]); }
  • 11.
    MULTI DIMENTIONAL ARRAY Thetwo dimensional (2D) array in C programming is also known as matrix. A matrix can be represented as a table of rows and columns SYNTAX: data-type name[row-size][column-size]; EXAMPLE: int a[3][4];
  • 12.
    Initialization • int odd[3][2]={1,3,5,7,9,11}; Individualelement can also be assigned as: Odd[0][0]=1; Odd[0][1]=3; Odd[1][0]=5; Odd[1][1]=7; Odd[2][0]=9; Odd[2][1]=11;
  • 13.
    Reading Data fromUser • Nested for Loop is used. for(i=0;i<3;i++) { for(j=0;j<2;j++) scanf(“%d”,&odd[i] [j]); }
  • 14.
  • 15.
    WHAT IS STRING? A group of characters A string constant is a one- dimensional array of characters terminated by a null ( ‘0’ ). declaration of Character: • char mychar; declaration of String: • char myString[10]; The characters after the null character are ignored.
  • 16.
    WHAT IS NULLchar ? • Null ‘0’ is a terminator which terminates the string in an array
  • 17.
    WHY NULL char? When declaring a string don’t forget to leave a space for the null character which is also known as the string terminator character only way the functions that work with a string can know where the string ends.
  • 18.
    INPUT FUNCTION • InputFunction : The scanf() Function • header file stdio.h • Syntax: • char mystring[100]; • scanf(“%s”, mystring); The name of a string is a pointer constant to the first character in the character array. Problem: terminates its input on the first white space it finds. white space includes blanks, tabs, carriage returns(CR), form feeds & new line.
  • 19.
  • 20.
    INPUT FUNCTION The gets()Function: Header file stdio.h takes a string from standard input and assigns it to a character array. It replaces the n with 0. Syntax: • char mystring[100]; • gets(myString); • fgets() it keeps the n and includes it as part of the string.
  • 21.
  • 22.
    OUTPUT FUNCTION The printf() function header file: stdio.h The puts() function header file: stdio.h PROGRAM OUTPUT
  • 23.
    STRING OPERATION(string.h) Four mainlibrary function which is define in • string.h header file • strcpy() - copy one string into another • strcat() - append one string onto the right side of • the other • strcmp() – compare alphabetic order of two • strings • strlen() – return the length of a string
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
    MORE FUNCTIONS • strlwr(): converts a string to lowercase • Strupr() : converts a string to uppercase • Strncat() : Appends first n characters of a string at the end of another • Strncmp() :Compares first n characters of two strings • Strcmpi():Compares two strings without regard to case ("i" denotes that this function ignores Case) • [Note: There are more library functions…..]
  • 29.