Home Explore E-LESSON-3

# E-LESSON-3

Published by , 2020-10-24 03:25:06

## Description: E-LESSON-3

### Read the Text Version

IDOL Institute of Distance and Online Learning ENHANCE YOUR QUALIFICATION, ADVANCE YOUR CAREER.

BCA 2 All right are reserved with CU-IDOL Computer Programming Course Code: BCA 112 Semester: First SLM Unit: e-Lesson No.: 5 3 www.cuidol.in Units-5 (BCA112)

Arrays & Strings 33 OBJECTIVES INTRODUCTION • Describe the concepts of arrays In this session we are going to learn about • Differentiate between one and two dimensional • The concepts of arrays, and using one and two arrays dimension arrays • Describe the concepts of strings • Difference between one and two dimensional arrays • Apply string handling functions • The concepts of strings, difference between array and string • Using string handling functions www.cuidol.in Units-5 (BCCA111122)) INASlTl ITriUgThEt OarFeDrIeSsTeArNvCeEd AwNitDh OCNUL-IIDNOE LLEARNING

Arrays & Strings 43 In this session we are going to learn about INTRODUCTION • The concepts of arrays, and using one and two dimension arrays • Difference between one and two dimensional arrays • The concepts of strings, difference between array and string • Using string handling functions www.cuidol.in Units-5 (BCA112) INASllTIrTiUgThEt aOrFeDreISsTeArNveCdE wANitDh COUN-LIIDNOELLEARNING

TOPICS TO BE COVERED 53 • Concepts of Array INTRODUCTION • One Dimensional Arrays • Two Dimensional Arrays • String Basics • String Handling Function www.cuidol.in Units-5 (BCA112) IANlSlTIrTigUhTtEaOreF DreIsSTeArvNeCdEwAiNthDCOUN-LIDINOELLEARNING

Concepts of Array 6 • Array is a collection of same type of data items. • Array items stored at contiguous memory locations and elements can be accessed randomly using indices of an array. • By using single variable, we can implement n data items of same type. • Array Structure is shown in below figure: www.cuidol.in Units-5 (BCA112) All right are reserved with CU-IDOL

One Dimensional Array 7 • An array of one dimension is known as a one-dimensional array or 1-D array. • Declaration syntax - data_type varname[size]; • For Example - int student[3] = {99, 76, 66}; // declaration with initialization #include <stdio.h> void main() {int s[5] = {99, 76, 66}, i; printf(\"\\n---Students marks details--- \"); for(i = 0; i < 3; i++) { printf(\"\\ns%d = %d \", i + 1, s[i]); } } Output - s1 = 99 s2 = 76 s3 = 66 www.cuidol.in Units-5 (BCA112) All right are reserved with CU-IDOL

Two Dimensional Array 8 • An array of two dimensions is known as a two-dimensional array or 2-D array. • Declaration syntax - data_type varname [size1] [size2]; • For Example - int item[4][2] = {34, 56, 12, 33, 34, 80, 12, 78}; // declaration with initialization www.cuidol.in Units-5 (BCA112) All right are reserved with CU-IDOL

Two Dimensional Array 9 #include <stdio.h> Input / Output: void main() { Enter 2x2 matrix elements: 1 int a[2][2], b[2][2],i, j; 2 3 printf(\"\\nEnter 2x2 matrix elements:\\n\"); 4 for (i = 0; i < 1; i++) Transpose of matrix is: for (j = 0; j < 1; j++) { 13 scanf(\"%d\", &a[i][j]); } 24 // Finding and displaying the transpose of matrix a printf(“\\nTranspose of matrix is:\\n”); for (i = 0; i < 1; i++){ for (j = 0; j < 1; j++) { b[i][j] = a[j][i]; printf (\"%d\",b[i][j]) } printf(\"\\n\"); } } www.cuidol.in Units-5 (BCA112) All right are reserved with CU-IDOL

String Basics 10 • A string is a sequence of characters terminated with a null character ’\\0’. • Declaration syntax – char string_name[size]; • String elements are stored at contiguous memory locations. • Example - char word[] = \"Hello\"; www.cuidol.in Units-5 (BCA112) All right are reserved with CU-IDOL

String Basics #include<stdio.h> 11 #include<string.h> void main() Input/Output: { Enter you first name: char name[20], full_name[30]; Param printf(\"\\nEnter your first name:\\n\"); scanf(\"%s\",name); Hello Param printf(\"\\n Hello %s \", name); Enter your full name: Param Prakash Welcome Param Prakash puts(“\\n Enter your full name:”); Units-5 (BCA112) All right are reserved with CU-IDOL gets(full_name); printf(“Welcome “); puts(full_name); } www.cuidol.in

String Handling Functions 12 • There are many string handling functions in C. • Each function performs a specific string related task. • Most commonly string handling functions are:  strlen()  strcpy()  strcat()  strcmp()  strrev()  strlwr()  strupr() www.cuidol.in Units-5 (BCA112) All right are reserved with CU-IDOL

String Handling Functions 13 • strlen(s1) : Returns the length of string s1. • strrev(s1): Generate reverse of string s1. • strlwr(s1): Convert string s1 to lowercase. • strupr(s1): Convert string s1 to uppercase. • strcpy(s1,s2): Copy string s1 to string s2. • strcat(s1,s2): Concatenates string s2 at the end of string s1. • strcmp(s1,s2): Compare two strings. It returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2. www.cuidol.in Units-5 (BCA112) All right are reserved with CU-IDOL

String Handling Functions 14 #include <stdio.h> #include <string.h> void main() { char str1[20] = \"WelcomeBack\"; printf(\"Length of string str1: %d\", strlen(str1)); printf(\"Reverse of string str1: %s\", strrev(str1)); printf(\"Lowercase of string str1: %d\", strlwr(str1)); printf(\"Upercase of string str1: %d\", strupr(str1)); } Output- Length of string str1: 11 Reverse of string str1: kcaBemocleW Lowercase of string str1: welcomeback Upercase of string str1: WELCOMEBACK www.cuidol.in Units-5 (BCA112) All right are reserved with CU-IDOL

String Handling Functions 15 #include <stdio.h> #include <string.h> void main() { char s1[10] = “Hello\"; char s2[10] = “World”; char s3[10]; strcpy(s1, s3); printf(“Output string s3 after copy: %s\", s3); printf(“Output string after concatenation: %s\", strcat(s1,s2)); printf(“Comparison result: %d\", strcmp(s1,s2)); } Output- Output string s3 after copy: : Hello Output string after concatenation: HelloWorld Comparison result: -1 www.cuidol.in Units-5 (BCA112) All right are reserved with CU-IDOL

Multiple Choice Questions 16 1. What is an Array in C language? a) A group of elements of same data type. b) An array contains more than one element c) Array elements are stored in memory in continuous or contiguous locations. d) All the above. 2. Elements in an array are accessed _____________ a) randomly b) sequentially c) exponentially d) logarithmically 3. Which function will you choose to join two words? a) strcpy() b) strcat() c) strncon() d) memcon() Answers:1. d) 2.a) 3.b) Units-5 (BCA112) All right are reserved with CU-IDOL www.cuidol.in

Multiple Choice Questions 17 4. What will strcmp() function do? a) compares the first n characters of the object b) compares the string c) undefined function d) copies the string 5. A String constant in C terminated by ___ a)'\\0' b)'\\\\0' c)’0\\’ d)’0’ Answers:4. b) 5.a) www.cuidol.in Units-5 (BCA112) All right are reserved with CU-IDOL

SUMMARY Let us recapitulate the important concepts discussed in this session: 18 • Array is a collection of same type of data items. • Array items stored at contiguous memory locations and elements can be accessed randomly using indices of an array. • An array of one dimension is known as a one-dimensional array or 1-D array. • An array of two dimensions is known as a two-dimensional array or 2-D array. • A string is a sequence of characters terminated with a null character ’\\0’. • String elements are stored at contiguous memory locations. • There are many string handling functions in C. Each function performs a specific string related task. • Most commonly string handling functions inclues: strlen(), strcpy(), strcat(), strcmp(), strrev(), strlwr(), strupr(). www.cuidol.in Units-5 (BCA112) All right are reserved with CU-IDOL

FREQUENTLY ASKED QUESTIONS 19 Q1. Define array. How it is beneficial? Ans. Array is a collection of same type of data items. For further details please refer to the subject SLM. Q2. Differentiate between 1-D and 2-D arrays. Ans: An array of one dimension is known as a one-dimensional array or 1-D array. While an array of two dimensions is known as a two-dimensional array or 2-D array. For further details please refer to the subject SLM. Q3. Distinguish between array and string. Ans: Array is a collection of same type of data items. While a string is a sequence of characters terminated with a null character ’\\0’. For further details please refer to the subject SLM. Q4: List and explain five string handling functions. Ans: The five string handling functions inclues: strlen(), strcpy(), strcat(), strcmp(), strrev(). For further details please refer to the subject SLM. www.cuidol.in Units-5 (BCA112) All right are reserved with CU-IDOL

REFERENCES 20 •CU-IDOL’s “Computer Programming” SLM • Balaguruswamy (2017).Programming in ANSI C. New Delhi: McGraw-Hill. • Kanetkar, Y. (2014), Programming in C ANSI standard.New Delhi: BPB Publications. • Gottfried (2005).Programming with C. New York: Tata McGraw Hill. • Harrow K., Jones J. (1996).Problem Solving with C. London: Pearson Education. • Jeri R., Hanly,KoffmanE.P. (2000).Problem Solving and Program Design in C. 3rd Ed. Boston: Addison Wesley. www.cuidol.in Units-5 (BCA112) All right are reserved with CU-IDOL

21 THANK YOU www.cuidol.in Units-5 (BCA112) All right are reserved with CU-IDOL

## Teamlease Edtech Ltd (Amita Chitroda)

Like this book? You can publish your book online for free in a few minutes!