SlideShare a Scribd company logo
D.E.I. TECHNICAL COLLEGE
COURSE TITLE: Software Development-VI
SLIDE-2
COURSE CODE: VIT351
CLASS: DIPLOMA IN INFORMATION TECHNOLOGY VOCATIONAL (1ST YEAR)
SESSION: 2019-20
TOPICS COVERED
 ARRAYS IN C
 MULTIDIMENSIONAL ARRAYS
 CHARACTER HANDLING IN C
 STRING HANDLING IN C
 QUIZ SET 2
VIT351 Software Development VI Unit2
What is an Array?
 Arrays are the collection of a finite number of homogeneous
data elements.
 Elements of the array are referenced respectively by an
index set consisting of n consecutive numbers and are
stored respectively in successive memory locations.
 The number n of elements is called the length or size of the
array.
GO TO TOPICS
Continue….
 Array is a collection of similar elements. These similar elements
could be all ints, floats, doubles, chars, etc. Array is also known as
subscript variable.
 Array can store a fixed-size sequential collection of elements of
the same type.
 Array elements are stored in contiguous memory block.
 Instead of declaring individual variables, such as number0,
number1, ..., and number99, you can declare one array variable.
 Creating array is creating a group of variables and all its elements
are accessed via single name and different subscript values.
Properties of an Array
1) Indexing of an array begins from zero (0).
2) The variable name of array contains the base address of
the memory block.
3) The array variable are created at the time of
compilation.
4) The size of the array cannot be altered at runtime.
How to declare an Array?
Syntax:
dataType arrayName[arraySize];
For example,
float mark[5];
This is called single dimensional array.
Here, we declared an array, mark, of floating-point type.
And its size is 5. Meaning, it can hold 5 floating-point values.
It's important to note that the size and type of an array cannot
be changed once it is declared.
 Array declaration by specifying size
// Array declaration by specifying size
int arr1[10];
// With recent C/C++ versions, we can also
// declare an array of user specified size
int n = 10;
int arr2[n];
 Array declaration by initializing elements
// Array declaration by initializing elements
int arr[] = { 10, 20, 30, 40 }
// Compiler creates an array of size 4.
// above is same as "int arr[4] = {10, 20, 30, 40}"
 Array declaration by specifying size and
initializing elements
// Array declaration by specifying size and
initializing elements
int arr[6] = { 10, 20, 30, 40 }
// Compiler creates an array of size 6,
initializes first
// 4 elements as specified by user and rest
two elements as 0.
// above is same as "int arr[] = {10, 20, 30, 40,
0, 0}"
How to access an Array?
 Array elements are accessed by using an integer index.
 Index of an array starts from 0 to size-1 i.e first element of arr array will be stored at arr[0]
address.
Example:
#include <stdio.h>
int main()
{
int arr[5];
arr[0] = 5;
arr[2] = -10;
arr[3 / 2] = 2; // this is same as arr[1] = 2
arr[3] = arr[0];
printf("%d %d %d %d", arr[0], arr[1], arr[2], arr[3]);
return 0;
}
Initialization of an Array
 After an array is declared it must be initialized. Otherwise, it will
contain garbage value(any random value). An array can be initialized at either compile
time or at runtime.
data-type array-name[size] = { list of values };
/* Here are a few examples */
int marks[4]={ 67, 87, 56, 77 }; // integer array initialization
float area[5]={ 23.4, 6.8, 5.5 }; // float array initialization
int marks[4]={ 67, 87, 56, 77, 59 }; // Compile time error
In such case, there is no requirement to define the size. So it may also be written as the
following code.
int marks[]={20,30,40,50,60};
Example:
#include <stdio.h>
int main()
{
int marks[10], i, n, sum = 0, avg;
printf("Enter number of elements: ");
scanf("%d", &n);
for(i=0; i<n; i++)
{
printf("Enter number");
scanf("%d", &marks[i]);
// adding integers entered by the user to the sum variable
sum =sum+marks[i];
}
avg = sum/n;
printf("Average = %d", avg);
return 0;
}
Multi-dimensional Array
 C language supports multidimensional arrays also.
 The simplest form of a multidimensional array is the two-dimensional array.
 Both the row's and column's index begins from 0.
Declaration:
data-type array-name[row-size][column-size]
/* Example */
int a[3][4];
GO TO TOPICS
Initialization of 2-D Array
 int b[2] [3] = {12,65,78,45,33,21}; //valid
 int b[ ] [3] = {12,65,78,45,33,21}; // valid
 int b[2] [ ] = {12,65,78,45,33,21}; //invalid
 int b[ ] [ ] = {12,65,78,45,33,21}; // invalid
Example: Adding two matrices
#include <stdio.h>
int main()
{ int i,j;
int a[2][2], b[2][2], result[2][2];
// Taking input using nested for loop
printf("Enter elements of 1st matrixn");
for (i = 0; i < 2; i++)
for (j = 0; j < 2; j++){
printf("Enter a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]); }
// Taking input using nested for loop
printf("Enter elements of 2nd matrixn");
for (i = 0; i < 2; i++)
for (j = 0; j < 2; j++){
printf("Enter b%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}
// adding corresponding elements of two arrays
for (i = 0; i < 2; i++)
for (j = 0; j < 2; j++)
{
result[i][j] = a[i][j] + b[i][j];
}
// Displaying the sum
printf("nSum Of Matrix:n");
for (i = 0; i < 2; i++)
for (j = 0; j < 2; j++)
{
printf("%dt", result[i][j]);
if (j == 1)
printf("n");
}
return 0;
}
Important Note:
 The array elements can be accessed in a constant time by using the index of the
particular element.
 To access an array element, address of an element is computed as an offset
from the base address of the array and one multiplication is needed to compute
what is supposed to be added to the base address to get the memory address
of the element
 First the size of an element of that data type is calculated and then it is multiplied
with the index of the element to get the value to be added to the base address.
 This process takes one multiplication and one addition. Since these two
operations take constant time, we can say the array access can be performed
in constant time.
VIT351 Software Development VI Unit2
Dealing with Characters
ctype.h file includes functions that are useful for testing and
mapping characters.
All the functions defined in ctype.h accept int as a
parameter, whose value must be EOF or representable as
an unsigned char.
These functions return non-zero (true) if the argument
satisfies the condition described else the functions return
zero (false).
GO TO TOPICS
isalnum() Function
 Description
The C library function void isalnum(int c) checks if the passed
character is alphanumeric.
 Declaration
int isalnum(int c);
 Parameters
c − This is the character to be checked.
 Return Value
This function returns non-zero value if c is a digit or a letter, else it
returns 0.
Example:1#include <ctype.h>
#include<stdio.h>
int main () {
int var1 = 'a';
int var2 = '4';
int var3 = 't';
if( isalnum(var1) ) {
printf("var1 = |%c| is alphanumericn", var1 );
} else {
printf("var1 = |%c| is not alphanumericn", var1 ); }
if( isalnum(var2) ) {
printf("var2 = |%c| is alphanumericn", var2 );
} else {
printf("var2 = |%c| is not alphanumericn", var2 ); }
if( isalnum(var3) ) {
printf("var3 = |%c| is alphanumericn", var3 );
} else {
printf("var3 = |%c| is not alphanumericn", var3 ); }
return(0); }
isalpha() Function
 Description
The C library function void isalpha(int c) checks if the passed
character is alphabet.
 Declaration
int isalpha(int c);
 Parameters
c − This is the character to be checked.
 Return Value
This function returns non-zero value if c is a letter, else it returns 0.
Example:2#include <stdio.h>
#include <ctype.h>
int main () {
int var1 = 'a';
int var2 = '4';
int var3 = 't';
if( isalpha(var1) ) {
printf("var1 = |%c| is an alphabetn", var1 );
} else {
printf("var1 = |%c| is not an alphabetn", var1 );}
if( isalpha(var2) ) {
printf("var2 = |%c| is an alphabetn", var2 );
} else {
printf("var2 = |%c| is not an alphabetn", var2 );}
if( isalpha(var3) ) {
printf("var3 = |%c| is an alphabetn", var3 );
} else {
printf("var3 = |%c| is not an alphabetn", var3 );}
return(0); }
iscntrl() Function
 Description
The C library function void iscntrl(int c) checks if the passed character
is a control character.
 Declaration
int iscntrl(int c);
 Parameters
c − This is the character to be checked.
 Return Value
This function returns non-zero value if c is a control character, else it
returns 0.
Example:3#include <stdio.h>
#include <ctype.h>
int main ()
{
int i = 0, j = 0;
char str1[] = "We like to learn a about t programming";
char str2[] = "these tutorials n are nice";
/* Prints string till control character a */
while( !iscntrl(str1[i]) )
{
putchar(str1[i]);
i++;
}
/* Prints string till control character n */
while( !iscntrl(str2[j]) )
{
putchar(str2[j]);
j++;
}
return(0);
}
isdigit() Function
 Description
The C library function void isdigit(int c) checks if the passed
character is a decimal digit character.
Decimal digits are (numbers) − 0 1 2 3 4 5 6 7 8 9.
 Declaration
int isdigit(int c);
 Parameters
c − This is the character to be checked.
 Return Value
This function returns non-zero value if c is a digit, else it returns 0.
ispunct() Function
 Description
The C library function int ispunct(int c) checks whether the passed
character is a punctuation character. A punctuation character is
any graphic character (as in isgraph) that is not alphanumeric (as
in isalnum).
 Declaration
int ispunct(int c);
 Parameters
c − This is the character to be checked.
 Return Value
This function returns a non-zero value(true) if c is a punctuation
character else, zero (false).
isspace() Function
 Description
The C library function int isspace(int c) checks whether the passed character is white-
space.Standard white-space characters are:
 ' ' (0x20) space (SPC)
 't' (0x09) horizontal tab (TAB)
 'n' (0x0a) newline (LF)
 'v' (0x0b) vertical tab (VT)
 'f' (0x0c) feed (FF)
 'r' (0x0d) carriage return (CR)
Declaration
int isspace(int c);
Parameters
c − This is the character to be checked.
Return Value
This function returns a non-zero value(true) if c is a white-space character else, zero (false).
String Handling in C
 String is a sequence of characters that is treated as a single data item and terminated by
null character '0'.
 Remember that C language does not support strings as a data type.
 A string is actually one-dimensional array of characters in C language.
 These are often used to create meaningful and readable programs.
 C language supports a large number of string handling functions that can be used to
carry out many of the string manipulations. These functions are packaged
in string.h library.
char name[12] = “DataScience"; // valid character array initialization
char name[12] = {‘d',‘a',‘t',‘a',‘s',‘c',‘i',’e’,’n’,’c’,’e’,'0'}; // valid initialization
char ch[3] = “data"; // Illegal
char str[4];
str = “data"; // Illegal
GO TO TOPICS
 strlen()
o prototype: int strlen(char *);
o It calculates the length of the given string
 strrev()
o prototype: char* strrev(char*);
o It reverses the given string
 strlwr()
o prototype: char* strlwr(char*);
o It converts the given string into its corresponding lower case
 strupr()
o prototype: char* strupr(char*);
o It converts the given string into its corresponding upper case
 strcpy()
o prototype: char* strcpy(char*, char*);
o It copies the second string into first
 strcat()
o prototype: char* strcat(char*, char*);
o It simply appends or concatenates the two strings.
 strcmp()
o prototype: int strcmp(char*, char*);
o It compares two strings and return -1, 0 or 1, indicative values.
Return 0 if strings are identical.
Returns -1 if first string appears before the second string in the dictionary.
Returns 1 if first string appears after the second string in the dictionary.
OR
 if Return value < 0 then it indicates str1 is less than str2.
 if Return value > 0 then it indicates str2 is less than str1.
 if Return value = 0 then it indicates str1 is equal to str2.
Example:1&2#include <stdio.h>
#include <string.h>
int main()
{
char str1[12] = "Hello";
char str2[12] = "Team";
char str3[12];
int len ;
/*copy str1 into str3 */
strcpy(str3, str1);
printf("strcpy(str3, str1) : %sn", str3 );
/*concatenates str1 and str2 */
strcat (str1, str2);
printf("strcat(str1, str2) : %sn", str1 );
/*total lenghth of str1 after concatenation */
len = strlen (str1);
printf("strlen(str1) : %dn", len );
return 0;
}
#include <stdio.h>
#include <string.h>
int main ()
{
char src[50], dest[50];
strcpy(src, "This is source");
strcpy(dest, "This is destination");
strcat(dest, src);
printf("Final destination string : |%s|", dest);
return(0);
}
Example:3
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[15];
char str2[15];
int ret;
strcpy(str1, "abcdef");
strcpy(str2, "ABCDEF");
ret = strcmp(str1, str2);
if(ret < 0)
{
printf("str1 is less than str2");
}
else if(ret > 0)
{
printf("str2 is less than str1");
}
else
{
printf("str1 is equal to str2");
}
return(0);
}
2.What does the following statement imply?
int arr[]={1,2,3,4,5,6,7,8,9,10};
A. Array uses only data type in its declaration
B. Array uses continuous memory addresses
C. Array is expandable
D. Array is derived data type
1. What is not true for an array?
A. The integer array does not contain any element.
B. The array contains 10 elements and the values are
natural numbers between 1 to 10.
C. The statement is not valid.
D. Array should have a dimension which is not given here.
GO TO TOPICS
4. A matrix is a
A. We may use a loop to feed values into array.
B. An array can contain values belonging to different data type
categories.
C. An array can have only one dimension
D. An array cannot be copied to another variable of fundamental
data type
3. Which of the following statements is false?
A. Single dimensional array
B. Two dimensional array
C. Three dimensional array
D. Four dimensional array
6. How many elements may be accommodated in
an array of dimension m and n? int arr[m][n]
A. Nested brackets are not allowed
B. There should not be any comma among between the numbers
C. There must be commas between the rows
D. The matrix is not having any dimensions mentioned for it.
5. What is wrong in this expression?
int mat[][]={{3,4},{7,8},{4,5},{20,10}}
A. Total number of elements that can be accommodated will
be m x n.
B. Total of m number of elements can be accommodated.
C. Total of n number of elements can be accommodated.
D. The declaration is defective
A. mat[0][0],mat[0][y],mat[x][0],mat[x][y]
B. mat[y][0],mat[0][y],mat[0][x],mat[x][y]
C. mat[0][x],mat[0][y],mat[x][0],mat[y][0]
D. mat[x][0],mat[x][y],mat[y][0],mat[y][y]
7. What are the four corner points of a matrix declared as int
mat[x][y]?
9. Which function is used for checking if a
character is a control character?
A. isletter()
B. checkalpha()
C. isalpha()
D. checkletter()
8. Which function is used for checking if a character is
alphabet?
A. isspecial()
B. iscntrl()
C. isgraphic()
D. checkspl()
11. Which function is used for checking if a character is a
punctuation?
A. isnum()
B. isnumber()
C. isdigit()
D. checkdigit()
10. Which function is used for checking if a character is a
digit?
A. ispunct()
B. ispnct()
C. isgraphic()
D. isdigit()
13. A length of the String may be calculated with
one of the following function. Identify the same.
A. Array of Characters
B. Array of Characters terminated with a ‘0’ null character
C. Is an object
D. Same as characters
12. String ar
A. strlength()
B. strlen()
C. strln()
D. length()
15. If you have to write a function for finding the length
of string. What will be the logic?
A. Joining two String
B. Merging two String
C. Cutting one string with another
D. None of the above
14. strcat() is used for
A. Read the character in an array and count them until you reach the and of the
array
B. Read the character in an array and count them until you encounter a ‘0’.
C. The dimension of the declared character is the length of the string
D. None of the Above
Continue……
In Slide 3

More Related Content

PPT
PDF
Programming Fundamentals Arrays and Strings
PPTX
COM1407: Arrays
PPT
Arrays
PPT
Functions and pointers_unit_4
PDF
Control statements-Computer programming
PDF
Array&amp;string
PPTX
C Programming Unit-3
Programming Fundamentals Arrays and Strings
COM1407: Arrays
Arrays
Functions and pointers_unit_4
Control statements-Computer programming
Array&amp;string
C Programming Unit-3

What's hot (20)

PPTX
Library functions in c++
PPTX
Unit 6. Arrays
PPTX
DATA TYPE IN PYTHON
PPTX
4 Pointers.pptx
PPTX
function, storage class and array and strings
PPTX
Bsc cs i pic u-4 function, storage class and array and strings
PPTX
Templates in C++
PPTX
Memory management of datatypes
PPTX
OPERATOR IN PYTHON-PART1
PDF
Generic programming and concepts that should be in C++
DOCX
Data Structure Project File
PPTX
Mcai pic u 4 function, storage class and array and strings
PDF
C Prog - Pointers
PDF
Functions torage class and array and strings-
PPTX
Btech i pic u-4 function, storage class and array and strings
PPTX
Decision making and branching
Library functions in c++
Unit 6. Arrays
DATA TYPE IN PYTHON
4 Pointers.pptx
function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and strings
Templates in C++
Memory management of datatypes
OPERATOR IN PYTHON-PART1
Generic programming and concepts that should be in C++
Data Structure Project File
Mcai pic u 4 function, storage class and array and strings
C Prog - Pointers
Functions torage class and array and strings-
Btech i pic u-4 function, storage class and array and strings
Decision making and branching
Ad

Similar to VIT351 Software Development VI Unit2 (20)

PPTX
Array
PPTX
arrays in c programming - example programs
PPT
presentation_arrays_1443553113_140676.ppt
PDF
SlideSet_4_Arraysnew.pdf
PPT
Arrays in c programing. practicals and .ppt
PDF
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
PDF
Arrays and function basic c programming notes
PPTX
CHAPTER 5
PDF
02 arrays
PPTX
Arrays in C language
PDF
Arrays-Computer programming
PPTX
PDF
Arrays and strings in c++
PPTX
Arrays
PPTX
one dimentional array on programming with C
PDF
[ITP - Lecture 15] Arrays & its Types
PPT
Module 5 PPS.pptnwnekdnndkdkdnnsksosmsna
DOC
Basic c programs updated on 31.8.2020
PPTX
Unit 3
PDF
Arrays and library functions
Array
arrays in c programming - example programs
presentation_arrays_1443553113_140676.ppt
SlideSet_4_Arraysnew.pdf
Arrays in c programing. practicals and .ppt
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
Arrays and function basic c programming notes
CHAPTER 5
02 arrays
Arrays in C language
Arrays-Computer programming
Arrays and strings in c++
Arrays
one dimentional array on programming with C
[ITP - Lecture 15] Arrays & its Types
Module 5 PPS.pptnwnekdnndkdkdnnsksosmsna
Basic c programs updated on 31.8.2020
Unit 3
Arrays and library functions
Ad

More from YOGESH SINGH (8)

PDF
VIT351 Software Development VI Unit5
PDF
VIT351 Software Development VI Unit4
PDF
VIT351 Software Development VI Unit3
PDF
VIT351 Software Development VI Unit1
PDF
DEE 431 Introduction to MySql Slide 6
PPTX
DEE 431 Introduction to Mysql Slide 3
PPTX
DEE 431 Database keys and Normalisation Slide 2
PPTX
DEE 431 Introduction to DBMS Slide 1
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit4
VIT351 Software Development VI Unit3
VIT351 Software Development VI Unit1
DEE 431 Introduction to MySql Slide 6
DEE 431 Introduction to Mysql Slide 3
DEE 431 Database keys and Normalisation Slide 2
DEE 431 Introduction to DBMS Slide 1

Recently uploaded (20)

PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PPTX
additive manufacturing of ss316l using mig welding
PDF
PPT on Performance Review to get promotions
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PPTX
Lecture Notes Electrical Wiring System Components
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
Well-logging-methods_new................
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPT
Mechanical Engineering MATERIALS Selection
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PDF
Digital Logic Computer Design lecture notes
Arduino robotics embedded978-1-4302-3184-4.pdf
additive manufacturing of ss316l using mig welding
PPT on Performance Review to get promotions
bas. eng. economics group 4 presentation 1.pptx
CYBER-CRIMES AND SECURITY A guide to understanding
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Strings in CPP - Strings in C++ are sequences of characters used to store and...
Lecture Notes Electrical Wiring System Components
Embodied AI: Ushering in the Next Era of Intelligent Systems
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Foundation to blockchain - A guide to Blockchain Tech
Well-logging-methods_new................
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Mechanical Engineering MATERIALS Selection
CH1 Production IntroductoryConcepts.pptx
Model Code of Practice - Construction Work - 21102022 .pdf
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Lesson 3_Tessellation.pptx finite Mathematics
Digital Logic Computer Design lecture notes

VIT351 Software Development VI Unit2

  • 1. D.E.I. TECHNICAL COLLEGE COURSE TITLE: Software Development-VI SLIDE-2 COURSE CODE: VIT351 CLASS: DIPLOMA IN INFORMATION TECHNOLOGY VOCATIONAL (1ST YEAR) SESSION: 2019-20
  • 2. TOPICS COVERED  ARRAYS IN C  MULTIDIMENSIONAL ARRAYS  CHARACTER HANDLING IN C  STRING HANDLING IN C  QUIZ SET 2
  • 4. What is an Array?  Arrays are the collection of a finite number of homogeneous data elements.  Elements of the array are referenced respectively by an index set consisting of n consecutive numbers and are stored respectively in successive memory locations.  The number n of elements is called the length or size of the array. GO TO TOPICS
  • 5. Continue….  Array is a collection of similar elements. These similar elements could be all ints, floats, doubles, chars, etc. Array is also known as subscript variable.  Array can store a fixed-size sequential collection of elements of the same type.  Array elements are stored in contiguous memory block.  Instead of declaring individual variables, such as number0, number1, ..., and number99, you can declare one array variable.  Creating array is creating a group of variables and all its elements are accessed via single name and different subscript values.
  • 6. Properties of an Array 1) Indexing of an array begins from zero (0). 2) The variable name of array contains the base address of the memory block. 3) The array variable are created at the time of compilation. 4) The size of the array cannot be altered at runtime.
  • 7. How to declare an Array? Syntax: dataType arrayName[arraySize]; For example, float mark[5]; This is called single dimensional array. Here, we declared an array, mark, of floating-point type. And its size is 5. Meaning, it can hold 5 floating-point values. It's important to note that the size and type of an array cannot be changed once it is declared.
  • 8.  Array declaration by specifying size // Array declaration by specifying size int arr1[10]; // With recent C/C++ versions, we can also // declare an array of user specified size int n = 10; int arr2[n];  Array declaration by initializing elements // Array declaration by initializing elements int arr[] = { 10, 20, 30, 40 } // Compiler creates an array of size 4. // above is same as "int arr[4] = {10, 20, 30, 40}"  Array declaration by specifying size and initializing elements // Array declaration by specifying size and initializing elements int arr[6] = { 10, 20, 30, 40 } // Compiler creates an array of size 6, initializes first // 4 elements as specified by user and rest two elements as 0. // above is same as "int arr[] = {10, 20, 30, 40, 0, 0}"
  • 9. How to access an Array?  Array elements are accessed by using an integer index.  Index of an array starts from 0 to size-1 i.e first element of arr array will be stored at arr[0] address. Example: #include <stdio.h> int main() { int arr[5]; arr[0] = 5; arr[2] = -10; arr[3 / 2] = 2; // this is same as arr[1] = 2 arr[3] = arr[0]; printf("%d %d %d %d", arr[0], arr[1], arr[2], arr[3]); return 0; }
  • 10. Initialization of an Array  After an array is declared it must be initialized. Otherwise, it will contain garbage value(any random value). An array can be initialized at either compile time or at runtime. data-type array-name[size] = { list of values }; /* Here are a few examples */ int marks[4]={ 67, 87, 56, 77 }; // integer array initialization float area[5]={ 23.4, 6.8, 5.5 }; // float array initialization int marks[4]={ 67, 87, 56, 77, 59 }; // Compile time error In such case, there is no requirement to define the size. So it may also be written as the following code. int marks[]={20,30,40,50,60};
  • 11. Example: #include <stdio.h> int main() { int marks[10], i, n, sum = 0, avg; printf("Enter number of elements: "); scanf("%d", &n); for(i=0; i<n; i++) { printf("Enter number"); scanf("%d", &marks[i]); // adding integers entered by the user to the sum variable sum =sum+marks[i]; } avg = sum/n; printf("Average = %d", avg); return 0; }
  • 12. Multi-dimensional Array  C language supports multidimensional arrays also.  The simplest form of a multidimensional array is the two-dimensional array.  Both the row's and column's index begins from 0. Declaration: data-type array-name[row-size][column-size] /* Example */ int a[3][4]; GO TO TOPICS
  • 13. Initialization of 2-D Array  int b[2] [3] = {12,65,78,45,33,21}; //valid  int b[ ] [3] = {12,65,78,45,33,21}; // valid  int b[2] [ ] = {12,65,78,45,33,21}; //invalid  int b[ ] [ ] = {12,65,78,45,33,21}; // invalid
  • 14. Example: Adding two matrices #include <stdio.h> int main() { int i,j; int a[2][2], b[2][2], result[2][2]; // Taking input using nested for loop printf("Enter elements of 1st matrixn"); for (i = 0; i < 2; i++) for (j = 0; j < 2; j++){ printf("Enter a%d%d: ", i + 1, j + 1); scanf("%d", &a[i][j]); } // Taking input using nested for loop printf("Enter elements of 2nd matrixn"); for (i = 0; i < 2; i++) for (j = 0; j < 2; j++){ printf("Enter b%d%d: ", i + 1, j + 1); scanf("%d", &b[i][j]); } // adding corresponding elements of two arrays for (i = 0; i < 2; i++) for (j = 0; j < 2; j++) { result[i][j] = a[i][j] + b[i][j]; } // Displaying the sum printf("nSum Of Matrix:n"); for (i = 0; i < 2; i++) for (j = 0; j < 2; j++) { printf("%dt", result[i][j]); if (j == 1) printf("n"); } return 0; }
  • 15. Important Note:  The array elements can be accessed in a constant time by using the index of the particular element.  To access an array element, address of an element is computed as an offset from the base address of the array and one multiplication is needed to compute what is supposed to be added to the base address to get the memory address of the element  First the size of an element of that data type is calculated and then it is multiplied with the index of the element to get the value to be added to the base address.  This process takes one multiplication and one addition. Since these two operations take constant time, we can say the array access can be performed in constant time.
  • 17. Dealing with Characters ctype.h file includes functions that are useful for testing and mapping characters. All the functions defined in ctype.h accept int as a parameter, whose value must be EOF or representable as an unsigned char. These functions return non-zero (true) if the argument satisfies the condition described else the functions return zero (false). GO TO TOPICS
  • 18. isalnum() Function  Description The C library function void isalnum(int c) checks if the passed character is alphanumeric.  Declaration int isalnum(int c);  Parameters c − This is the character to be checked.  Return Value This function returns non-zero value if c is a digit or a letter, else it returns 0.
  • 19. Example:1#include <ctype.h> #include<stdio.h> int main () { int var1 = 'a'; int var2 = '4'; int var3 = 't'; if( isalnum(var1) ) { printf("var1 = |%c| is alphanumericn", var1 ); } else { printf("var1 = |%c| is not alphanumericn", var1 ); } if( isalnum(var2) ) { printf("var2 = |%c| is alphanumericn", var2 ); } else { printf("var2 = |%c| is not alphanumericn", var2 ); } if( isalnum(var3) ) { printf("var3 = |%c| is alphanumericn", var3 ); } else { printf("var3 = |%c| is not alphanumericn", var3 ); } return(0); }
  • 20. isalpha() Function  Description The C library function void isalpha(int c) checks if the passed character is alphabet.  Declaration int isalpha(int c);  Parameters c − This is the character to be checked.  Return Value This function returns non-zero value if c is a letter, else it returns 0.
  • 21. Example:2#include <stdio.h> #include <ctype.h> int main () { int var1 = 'a'; int var2 = '4'; int var3 = 't'; if( isalpha(var1) ) { printf("var1 = |%c| is an alphabetn", var1 ); } else { printf("var1 = |%c| is not an alphabetn", var1 );} if( isalpha(var2) ) { printf("var2 = |%c| is an alphabetn", var2 ); } else { printf("var2 = |%c| is not an alphabetn", var2 );} if( isalpha(var3) ) { printf("var3 = |%c| is an alphabetn", var3 ); } else { printf("var3 = |%c| is not an alphabetn", var3 );} return(0); }
  • 22. iscntrl() Function  Description The C library function void iscntrl(int c) checks if the passed character is a control character.  Declaration int iscntrl(int c);  Parameters c − This is the character to be checked.  Return Value This function returns non-zero value if c is a control character, else it returns 0.
  • 23. Example:3#include <stdio.h> #include <ctype.h> int main () { int i = 0, j = 0; char str1[] = "We like to learn a about t programming"; char str2[] = "these tutorials n are nice"; /* Prints string till control character a */ while( !iscntrl(str1[i]) ) { putchar(str1[i]); i++; } /* Prints string till control character n */ while( !iscntrl(str2[j]) ) { putchar(str2[j]); j++; } return(0); }
  • 24. isdigit() Function  Description The C library function void isdigit(int c) checks if the passed character is a decimal digit character. Decimal digits are (numbers) − 0 1 2 3 4 5 6 7 8 9.  Declaration int isdigit(int c);  Parameters c − This is the character to be checked.  Return Value This function returns non-zero value if c is a digit, else it returns 0.
  • 25. ispunct() Function  Description The C library function int ispunct(int c) checks whether the passed character is a punctuation character. A punctuation character is any graphic character (as in isgraph) that is not alphanumeric (as in isalnum).  Declaration int ispunct(int c);  Parameters c − This is the character to be checked.  Return Value This function returns a non-zero value(true) if c is a punctuation character else, zero (false).
  • 26. isspace() Function  Description The C library function int isspace(int c) checks whether the passed character is white- space.Standard white-space characters are:  ' ' (0x20) space (SPC)  't' (0x09) horizontal tab (TAB)  'n' (0x0a) newline (LF)  'v' (0x0b) vertical tab (VT)  'f' (0x0c) feed (FF)  'r' (0x0d) carriage return (CR) Declaration int isspace(int c); Parameters c − This is the character to be checked. Return Value This function returns a non-zero value(true) if c is a white-space character else, zero (false).
  • 27. String Handling in C  String is a sequence of characters that is treated as a single data item and terminated by null character '0'.  Remember that C language does not support strings as a data type.  A string is actually one-dimensional array of characters in C language.  These are often used to create meaningful and readable programs.  C language supports a large number of string handling functions that can be used to carry out many of the string manipulations. These functions are packaged in string.h library. char name[12] = “DataScience"; // valid character array initialization char name[12] = {‘d',‘a',‘t',‘a',‘s',‘c',‘i',’e’,’n’,’c’,’e’,'0'}; // valid initialization char ch[3] = “data"; // Illegal char str[4]; str = “data"; // Illegal GO TO TOPICS
  • 28.  strlen() o prototype: int strlen(char *); o It calculates the length of the given string  strrev() o prototype: char* strrev(char*); o It reverses the given string  strlwr() o prototype: char* strlwr(char*); o It converts the given string into its corresponding lower case  strupr() o prototype: char* strupr(char*); o It converts the given string into its corresponding upper case  strcpy() o prototype: char* strcpy(char*, char*); o It copies the second string into first
  • 29.  strcat() o prototype: char* strcat(char*, char*); o It simply appends or concatenates the two strings.  strcmp() o prototype: int strcmp(char*, char*); o It compares two strings and return -1, 0 or 1, indicative values. Return 0 if strings are identical. Returns -1 if first string appears before the second string in the dictionary. Returns 1 if first string appears after the second string in the dictionary. OR  if Return value < 0 then it indicates str1 is less than str2.  if Return value > 0 then it indicates str2 is less than str1.  if Return value = 0 then it indicates str1 is equal to str2.
  • 30. Example:1&2#include <stdio.h> #include <string.h> int main() { char str1[12] = "Hello"; char str2[12] = "Team"; char str3[12]; int len ; /*copy str1 into str3 */ strcpy(str3, str1); printf("strcpy(str3, str1) : %sn", str3 ); /*concatenates str1 and str2 */ strcat (str1, str2); printf("strcat(str1, str2) : %sn", str1 ); /*total lenghth of str1 after concatenation */ len = strlen (str1); printf("strlen(str1) : %dn", len ); return 0; } #include <stdio.h> #include <string.h> int main () { char src[50], dest[50]; strcpy(src, "This is source"); strcpy(dest, "This is destination"); strcat(dest, src); printf("Final destination string : |%s|", dest); return(0); }
  • 31. Example:3 #include <stdio.h> #include <string.h> int main () { char str1[15]; char str2[15]; int ret; strcpy(str1, "abcdef"); strcpy(str2, "ABCDEF"); ret = strcmp(str1, str2); if(ret < 0) { printf("str1 is less than str2"); } else if(ret > 0) { printf("str2 is less than str1"); } else { printf("str1 is equal to str2"); } return(0); }
  • 32. 2.What does the following statement imply? int arr[]={1,2,3,4,5,6,7,8,9,10}; A. Array uses only data type in its declaration B. Array uses continuous memory addresses C. Array is expandable D. Array is derived data type 1. What is not true for an array? A. The integer array does not contain any element. B. The array contains 10 elements and the values are natural numbers between 1 to 10. C. The statement is not valid. D. Array should have a dimension which is not given here. GO TO TOPICS
  • 33. 4. A matrix is a A. We may use a loop to feed values into array. B. An array can contain values belonging to different data type categories. C. An array can have only one dimension D. An array cannot be copied to another variable of fundamental data type 3. Which of the following statements is false? A. Single dimensional array B. Two dimensional array C. Three dimensional array D. Four dimensional array
  • 34. 6. How many elements may be accommodated in an array of dimension m and n? int arr[m][n] A. Nested brackets are not allowed B. There should not be any comma among between the numbers C. There must be commas between the rows D. The matrix is not having any dimensions mentioned for it. 5. What is wrong in this expression? int mat[][]={{3,4},{7,8},{4,5},{20,10}} A. Total number of elements that can be accommodated will be m x n. B. Total of m number of elements can be accommodated. C. Total of n number of elements can be accommodated. D. The declaration is defective
  • 35. A. mat[0][0],mat[0][y],mat[x][0],mat[x][y] B. mat[y][0],mat[0][y],mat[0][x],mat[x][y] C. mat[0][x],mat[0][y],mat[x][0],mat[y][0] D. mat[x][0],mat[x][y],mat[y][0],mat[y][y] 7. What are the four corner points of a matrix declared as int mat[x][y]?
  • 36. 9. Which function is used for checking if a character is a control character? A. isletter() B. checkalpha() C. isalpha() D. checkletter() 8. Which function is used for checking if a character is alphabet? A. isspecial() B. iscntrl() C. isgraphic() D. checkspl()
  • 37. 11. Which function is used for checking if a character is a punctuation? A. isnum() B. isnumber() C. isdigit() D. checkdigit() 10. Which function is used for checking if a character is a digit? A. ispunct() B. ispnct() C. isgraphic() D. isdigit()
  • 38. 13. A length of the String may be calculated with one of the following function. Identify the same. A. Array of Characters B. Array of Characters terminated with a ‘0’ null character C. Is an object D. Same as characters 12. String ar A. strlength() B. strlen() C. strln() D. length()
  • 39. 15. If you have to write a function for finding the length of string. What will be the logic? A. Joining two String B. Merging two String C. Cutting one string with another D. None of the above 14. strcat() is used for A. Read the character in an array and count them until you reach the and of the array B. Read the character in an array and count them until you encounter a ‘0’. C. The dimension of the declared character is the length of the string D. None of the Above