Chapter 8 Character Arrays and Strings

Size: px
Start display at page:

Download "Chapter 8 Character Arrays and Strings"

Transcription

1 Chapter 8 Character Arrays and Strings

2 INTRODUCTION A string is a sequence of characters that is treated as a single data item. String constant: String constant example. \ String constant example.\ \ includes double quote in the string. o printf( \ Well Done!\ ); output: Well Done! 2

3 DECLARING AND INITIALIZING STRING VARIABLES: C does not support strings as a data type. Strings are represented as character arrays. The general form of declaration of a string variable is: char string_name[size]; Example: char city[10]; char name[30]; Size = maximum number of characters in the string plus one (for NULL character \0 ) 3

4 DECLARING AND INITIALIZING STRING VARIABLES: Initialization: Example: 1. char city[18] = Ahmedabad Gujarat ; 2. char city[18] = { A, h, m, e, d, a, b, a, d,, G, u, j, a, r, a, t, \0 }; 3. char string[]={ G, o, o, d, \0 }; size is determined from list of values. 4. char str[10] = Good ; str G o o d \0 \0 \0 \0 \0 \0 4

5 DECLARING AND INITIALIZING STRING VARIABLES: Initialization: Example: 1. char str2[3]= Good ; //compile time error - size is smaller than specified value 2. char str3[5]; str3= Good ; we can not separate initialization from declaration. 4. char s1[4]= abc ; char s2[4]; s2=s1; //Error Array name can not be used as the left operand of an assignment operator. 5

6 TERMINATING NULL CHARACTER The string is a variable-length structure and is stored in a fixed-length array. The array size is not always the size of string and most often it is much larger than the string stored in it. Null character end-of-string 6

7 READING STRINGS FROM TERMINAL 7

8 Reading strings from terminal Using scanf function Format specification: %s Example: char name[11]; scanf( %s,name); The problem with the scanf function is that it terminates its input on the first white space it finds. 8

9 Reading strings from terminal Using scanf function char name[11]; scanf( %s,name); Example: Ajay Patel given as input then scanf takes only Ajay as input because it breaks in blank space (white space). name A j a y \0?????? 9

10 Reading strings from terminal Using scanf function Example: char adr1[25], adr2[25]; scanf( %s%s,adr1,adr2); 10

11 Reading strings from terminal Using scanf function Format specification: %ws scanf( %ws,name); Example: char name[10]; scanf( %5s,name); The input string RAM will be stored as: name R A M \0??????

12 Reading strings from terminal Using scanf function Format specification: %ws scanf( %ws,name); Example: char name[10]; scanf( %5s,name); The input string KRISHNA will be stored as: name K R I S H \0????

13 Reading strings from terminal Using a Line of Text Format specification: %[..] also known as edit set conversion code. Example, char line[20]; scanf( %[^\n],line); line A j a y P a t e l \0?????????

14 Reading strings from terminal Using getchar and gets function Using getchar() function char ch; ch=getchar(); Example next slide 14

15 Reading strings from terminal Using getchar and gets function #include<stdio.h> void main() { char line[81],ch; int c=0; printf( Enter text. Press <Return> at end.\n ); do { ch=getchar(); line[c]=ch; c++; }while(ch!= \n ); c=c-1; line[c]= \0 ; } printf( line=%s,line); Output: Enter text. Press <Return> at end. String is interesting chapter. line=string is interesting chapter. 15

16 Reading strings from terminal Using getchar and gets function Using gets() function Header file stdio.h General format gets(string_variable_name); g e t s ( ) r e a d c h a r a c t e r s i n t o string_variable_name from keyboard until a new-line character is encountered and then appends a null character to the string. 16

17 Reading strings from terminal Using getchar and gets function Example, char line[81]; gets(line); printf( %s,line); 17

18 Reading strings from terminal Using getchar and gets function Difference between gets() and scanf() function gets() Format: gets(string_variable_name); It can consider white space until new line character encountered. Example, char ch[10]; gets(ch); scanf() Format: scanf( %ws,name); It can not consider white space while reading a string. Example, char ch[10]; scanf( %s,ch); It is used to read line of text. It is not used to read line of text but word. 18

19 Writing strings to screen 19

20 WRITING STRINGS TO SCREEN USING PRINTF() FUNCTION Format specification: %w.p s Here w width and p first p characters of the string. Example char name = Ajay Patel ; printf( %s,name); A j a y P a t e l printf( %10s,name); A j a y P a t e l 20

21 WRITING STRINGS TO SCREEN USING PRINTF() FUNCTION When the field width is less than size of string, entire string is printed char name = Ajay Patel ; printf( %5s,name); A j a y P a t e l The integer on right side of decimal indicates number of characters to be printed printf( %15.6s,name); printf( %-15.6s,name); A j a y P A j a y P 21

22 WRITING STRINGS TO SCREEN USING PRINTF() FUNCTION char name = Ajay Patel ; printf( %15.0s,name); 0 characters are to be printed. So nothing is printed on the screen. printf( %.3s,name); A j a printf( %*.*s,w,p,string); printf( %-*.*s,15,6,name); A j a y P 22

23 23

24 (Contd.) 24

25 Writing strings to screen using putchar() and puts() function Using putchar() function: char ch= A ; putchar(ch); Example, char name[5]= Ajay ; i=0; while(name[i]!= \0 ) { putchar(name[i]); i++; } putchar( \n ); A j a y \0 25

26 Writing strings to screen using putchar() and puts() function Using puts() function: Format: puts(string_variable_name) It prints the string on the screen then move the cursor to the beginning of the next line on the screen. Example, char name[20]; gets(name); puts(name); 26

27 ARITHMETIC OPERATIONS ON CHARACTERS Whenever a character constant or character v a r i a b l e i s u s e d i n e x p r e s s i o n, i t i s automatically converted into an integer value by the system. The integer value depends on the local character set of the system. If the machine ASCII representation, char x= a ; printf( %d\n,x); output:- 97 Arithmetic operations can be performed on character constant or character variable. x= z -1; 122(ASCII of z) - 1 =

28 ARITHMETIC OPERATIONS ON CHARACTERS To check whether the character is upper-case letter or not. ch >= A && ch <= Z We can convert a character digit to its equivalent integer value using the following relationship. x = character 0 // Here x is interger. Example, x= ASCII value of 7 - ASCII value of 0 = = 7 28

29 29

30 ARITHMETIC OPERATIONS ON CHARACTERS The C library supports a function that converts a string of digits into their integer values. The function takes the form Example, integer_variable = atoi(string); char number[5] = 2013 ; int year; year = atoi(number); atoi() function is stored in the header file stdlib.h. 30

31 PUTTING STRINGS TOGETHER We can not join two strings together by the simple arithmetic addition. Like, string3 = string1 + string2; string2 = string1 + hello ; are not valid. The characters from string1 and string2 should be copied into the string3 one after the other. The size of array string3 should be large enough to hold the total characters. The process of combining two string together is called concatenation. 31

32 COMPARISON OF TWO STRINGS Similarly, C does not permit the comparison of two strings directly. Like, if(name1==name2) if(name1== Ajay ) //are invalid It is therefore necessary to compare the two strings to be tested, character by character. 32

33 33

34 STRING HANDLING FUNCTIONS Most commonly used string handling functions: Function strcat() strcmp() Action Concatenates two strings Compares two strings strcpy() strlen() Copies one string over another Finds the length of a string 34

35 STRING HANDLING FUNCTIONS STRCAT() FUNCTION It joins two strings together. It takes the following form: strcat(string1, string2); string2 is appended to string1. Example refer next slide 35

36 STRING HANDLING FUNCTIONS STRCAT() FUNCTION Example char name[15] = Ajay ; char sname[6] = Patel ; strcat(name,sname); A j a y \0 name sname P a t e l \0 name A j a y P a t e l \0 36

37 STRING HANDLING FUNCTIONS STRCAT() FUNCTION Example char name[15] = Ajay ; strcat(name, Patel ); name A j a y \0 name A j a y P a t e l \0 37

38 STRING HANDLING FUNCTIONS STRCAT() FUNCTION Example char name[15]; char fname[6]= Ajay,lname[6]= Patel ; strcat(strcat(name,fname),lname); A j a y \0 fname lname P a t e l \0 name A j a y P a t e l \0 38

39 STRING HANDLING FUNCTIONS STRCMP() FUNCTION It compares two strings and has a value 0 if they are equal. If they are not, it has numeric difference between the first nonmatching characters in the strings. It takes the following form: strcmp(string1, string2); Example refer next slide 39

40 STRING HANDLING FUNCTIONS STRCMP() FUNCTION Example strcmp( their, there ); It returns a value -9 (Ascii of (i) - Ascii of (r) ). 40

41 STRING HANDLING FUNCTIONS STRCMP() FUNCTION Example char name1[15]= Ajay ; char name2[15]= Ajay ; int x; x = strcmp(name1,name2); if(x!= 0) { printf( strings are not equal. ); } else { printf( strings are equal. ); } 41

42 42

43 Sorting of strings in alphabetical order

44 STRING HANDLING FUNCTIONS STRCPY() FUNCTION It copies one string over another. It takes the following form: strcpy(string1, string2); it assigns the contents of string2 to string1. string2 may be a character array variable or a string constant. Example, strcpy(city, AHMEDABAD ); strcpy(city1,city2); size of the array city1 should be large enough to receive the contents of city2. 44

45 STRING HANDLING FUNCTIONS STRLEN() FUNCTION It counts and returns the number of characters in a string. The general form of strlen is: n = strlen(string); Where n is an integer variable, which receives the value of the length of the string. The argument may be a string constant. The counting ends at the first null character. Example char city[15]= Ahmedabad ; int n=strlen(city); n=9 int x=strlen( Baroda Gujarat ); x=14 45

46 Other String handling functions strncpy() function The header file string.h contains many more string manipulation functions. It copies only left-most n characters of the source string to the target string variable. strncpy(s1,s2,5); This statement copies the first 5 characters of the source string s2 into the target string s1. Since the first 5 characters may not include the terminating null character, we have to place it explicitly in the 6 th position of s2 as shown below: s1[6]= \0 ; 46

47 Other String handling functions strncpy() function Example, char str1= Nirma Uni ; char str2= IDS ; strncpy(str1,str2,2); IDrma Uni 47

48 Other String handling functions strncmp() function The general form is: strncmp(s1,s2,n); this compares the left-most n characters of s1 and s2 and returns 0 if they are equal. Negative number, if s1 sub-string is less than s2 Positive number, otherwise. 48

49 Other String handling functions strncat() function The general form is: strncat(s1,s2,n); This call will concatenate the left-most n characters of s2 to the end of s1. S1: B A L A \0 S2: G U R U S A M Y \0 After strncat(s1,s2,4); excution: S1: B A L A G U R U \0 49

50 OTHER STRING HANDLING FUNCTIONS STRSTR() FUNCTION It is a two-parameter function that can be used to locate sub-string in a string. This takes the forms: strstr(s1,s2); strstr(s1, ABC ); The function strstr searches the string s1 to see whether the string s2 is contained in s1. If yes, the function returns the position of the first occurrence of the sub-string. Otherwise, it returns a NULL pointer. 50

51 OTHER STRING HANDLING FUNCTIONS STRSTR() FUNCTION Example, char s1[15]= Nirma University, s2[10]= Uni ; if(strstr(s1,s2)==null) printf( substring is not found. ); else printf( s2 is as substring of s1. ); 51

52 OTHER STRING HANDLING FUNCTIONS STRCHR() FUNCTION We also have functions to determine the existence of a character in a string. The function strchr(s1, m ); will locate the first occurrence of the character m. The function strrchr(s1, m ); will locate the last occurrence of the character m in the string s1. 52

53 TABLE OF STRINGS A list of names can be treated as a table of string and a two-dimensional character array can be used to store the entire list. Example, char student[30][20]; It is used store 30 student names each of length not more than 20 characters. 53

54 Write a C program to copy one string into another without using string handling function. #include<stdio.h> #include<conio.h> void main() { char str1[50],str2[20]; int i; clrscr(); printf( Enter the string2: ); gets(str2); for(i=0;str2[i]!= \0 ;i++) str1[i]=str2[i]; str1[i]= \0 ; printf( String1 = %s,str1); getch(); } Output: Enter the string2: Nirma University String1 = Nirma University 54

55 Write a C program to compare two strings without using string handling function. #include<stdio.h> #include<conio.h> void main() { char str1[50],str2[20]; int i; clrscr(); printf( Enter the string1 and string2: ); gets(str1); gets(str2); i=0; while(str1[i]==str2[i] && str1[i]!= \0 && str2[i]!= \0 ) i++; if(str1[i]== \0 && str2[i]== \0 ) printf( Strings are equal. ); else printf( Strings are not equal. ); getch(); } 55

56 Write a C program that counts the number of words from the given string #include<stdio.h> #include<conio.h> void main() { char str[100]; int count=1,i=0; while(str[i]!='\0') { if(str[i]==' ') { count++; } i++; clrscr(); printf("enter the string:"); gets(str); } printf("\nno. of words in string are: %d",count); getch(); } 56

57 WRITE A PROGRAM TO PRINT FOLLOWING USING FOR LOOP: 57

58 #include<stdio.h> #include<string.h> void main() { char str[20]; int i,j; printf("enter the string:"); scanf("%s",str); for(i=0;i<strlen(str);i++) { for(j=0;j<=i;j++) printf("%c",str[j]); printf("\n"); } getch(); } 58

59 Thank You

ONE DIMENSIONAL ARRAYS

ONE DIMENSIONAL ARRAYS LECTURE 14 ONE DIMENSIONAL ARRAYS Array : An array is a fixed sized sequenced collection of related data items of same data type. In its simplest form an array can be used to represent a list of numbers

More information

Introduction to string

Introduction to string 1 Introduction to string String is a sequence of characters enclosed in double quotes. Normally, it is used for storing data like name, address, city etc. ASCII code is internally used to represent string

More information

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University Unit 1 Programming Language and Overview of C 1. State whether the following statements are true or false. a. Every line in a C program should end with a semicolon. b. In C language lowercase letters are

More information

Chapter 8: Character & String. In this chapter, you ll learn about;

Chapter 8: Character & String. In this chapter, you ll learn about; Chapter 8: Character & String Principles of Programming In this chapter, you ll learn about; Fundamentals of Strings and Characters The difference between an integer digit and a character digit Character

More information

Chapter 8 C Characters and Strings

Chapter 8 C Characters and Strings Chapter 8 C Characters and Strings Objectives of This Chapter To use the functions of the character handling library (). To use the string conversion functions of the general utilities library

More information

Strings and Library Functions

Strings and Library Functions Unit 4 String String is an array of character. Strings and Library Functions A string variable is a variable declared as array of character. The general format of declaring string is: char string_name

More information

Computers Programming Course 11. Iulian Năstac

Computers Programming Course 11. Iulian Năstac Computers Programming Course 11 Iulian Năstac Recap from previous course Cap. Matrices (Arrays) Matrix representation is a method used by a computer language to store matrices of different dimension in

More information

String can be represented as a single-dimensional character type array. Declaration of strings

String can be represented as a single-dimensional character type array. Declaration of strings String String is the collection of characters. An array of characters. String can be represented as a single-dimensional character type array. Declaration of strings char string-name[size]; char address[25];

More information

https://www.eskimo.com/~scs/cclass/notes/sx8.html

https://www.eskimo.com/~scs/cclass/notes/sx8.html 1 de 6 20-10-2015 10:41 Chapter 8: Strings Strings in C are represented by arrays of characters. The end of the string is marked with a special character, the null character, which is simply the character

More information

Vidyalankar. F.E. Sem. II Structured Programming Approach Prelim Question Paper Solution

Vidyalankar. F.E. Sem. II Structured Programming Approach Prelim Question Paper Solution 1. (a) 1. (b) 1. (c) F.E. Sem. II Structured Programming Approach C provides a variety of stroage class specifiers that can be used to declare explicitly the scope and lifetime of variables. The concepts

More information

Characters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters,

Characters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters, Strings Characters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters, decimal digits, special characters and escape

More information

C does not support string data type. Strings in C are represented by arrays of characters.

C does not support string data type. Strings in C are represented by arrays of characters. Chapter 8 STRINGS LEARNING OBJECTIVES After going this chapter the reader will be able to Use different input/output functions for string Learn the usage of important string manipulation functions available

More information

Reading Assignment. Strings. K.N. King Chapter 13. K.N. King Sections 23.4, Supplementary reading. Harbison & Steele Chapter 12, 13, 14

Reading Assignment. Strings. K.N. King Chapter 13. K.N. King Sections 23.4, Supplementary reading. Harbison & Steele Chapter 12, 13, 14 Reading Assignment Strings char identifier [ size ] ; char * identifier ; K.N. King Chapter 13 K.N. King Sections 23.4, 23.5 Supplementary reading Harbison & Steele Chapter 12, 13, 14 Strings are ultimately

More information

Chapter 8 - Characters and Strings

Chapter 8 - Characters and Strings 1 Chapter 8 - Characters and Strings Outline 8.1 Introduction 8.2 Fundamentals of Strings and Characters 8.3 Character Handling Library 8.4 String Conversion Functions 8.5 Standard Input/Output Library

More information

Chapter 9 Strings. With this array declaration: char s[10];

Chapter 9 Strings. With this array declaration: char s[10]; Chapter 9 Strings 9.1 Chapter Overview There is no data type in C called ʻstringʼ; instead, strings are represented by an array of characters. There is an assortment of useful functions for strings that

More information

Strings. Arrays of characters. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY

Strings. Arrays of characters. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY Strings Arrays of characters Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY 1 Basics Strings A string is a sequence of characters treated as a group We have already

More information

by Pearson Education, Inc. All Rights Reserved.

by Pearson Education, Inc. All Rights Reserved. The string-handling library () provides many useful functions for manipulating string data (copying strings and concatenating strings), comparing strings, searching strings for characters and

More information

String Class in C++ When the above code is compiled and executed, it produces result something as follows: cin and strings

String Class in C++ When the above code is compiled and executed, it produces result something as follows: cin and strings String Class in C++ The standard C++ library provides a string class type that supports all the operations mentioned above, additionally much more functionality. We will study this class in C++ Standard

More information

Iosif Ignat, Marius Joldoș Laboratory Guide 9. Character strings CHARACTER STRINGS

Iosif Ignat, Marius Joldoș Laboratory Guide 9. Character strings CHARACTER STRINGS CHARACTER STRINGS 1. Overview The learning objective of this lab session is to: Understand the internal representation of character strings Acquire skills in manipulating character strings with standard

More information

UNIT-I Input/ Output functions and other library functions

UNIT-I Input/ Output functions and other library functions Input and Output functions UNIT-I Input/ Output functions and other library functions All the input/output operations are carried out through function calls. There exists several functions that become

More information

Unit 1 - Arrays. 1 What is an array? Explain with Example. What are the advantages of using an array?

Unit 1 - Arrays. 1 What is an array? Explain with Example. What are the advantages of using an array? 1 What is an array? Explain with Example. What are the advantages of using an array? An array is a fixed-size sequenced collection of elements of the same data type. An array is derived data type. The

More information

C: How to Program. Week /May/28

C: How to Program. Week /May/28 C: How to Program Week 14 2007/May/28 1 Chapter 8 - Characters and Strings Outline 8.1 Introduction 8.2 Fundamentals of Strings and Characters 8.3 Character Handling Library 8.4 String Conversion Functions

More information

ARRAYS(II Unit Part II)

ARRAYS(II Unit Part II) ARRAYS(II Unit Part II) Array: An array is a collection of two or more adjacent cells of similar type. Each cell in an array is called as array element. Each array should be identified with a meaningful

More information

Eastern Mediterranean University School of Computing and Technology Information Technology Lecture7 Strings and Characters

Eastern Mediterranean University School of Computing and Technology Information Technology Lecture7 Strings and Characters Eastern Mediterranean University School of Computing and Technology Information Technology Lecture7 Strings and Characters Using Strings The string in C programming language is actually a one-dimensional

More information

8. Characters, Strings and Files

8. Characters, Strings and Files REGZ9280: Global Education Short Course - Engineering 8. Characters, Strings and Files Reading: Moffat, Chapter 7, 11 REGZ9280 14s2 8. Characters and Arrays 1 ASCII The ASCII table gives a correspondence

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

Eastern Mediterranean University School of Computing and Technology Information Technology Lecture5 C Characters and Strings

Eastern Mediterranean University School of Computing and Technology Information Technology Lecture5 C Characters and Strings Eastern Mediterranean University School of Computing and Technology Information Technology Lecture5 C Characters and Strings Using Strings The string in C programming language is actually a one-dimensional

More information

9/9/2017. Prof. Vinod Mahajan

9/9/2017. Prof. Vinod Mahajan In the last chapter you learnt how to define arrays of different sizes & sizes& dimensions, how to initialize arrays, how to pass arrays to a functions etc. What are strings:- The way a group of integer

More information

Fundamentals of Programming

Fundamentals of Programming Fundamentals of Programming Lecture 6 - Array and String Lecturer : Ebrahim Jahandar Borrowed from lecturer notes by Omid Jafarinezhad Array Generic declaration: typename variablename[size]; typename is

More information

Computers Programming Course 12. Iulian Năstac

Computers Programming Course 12. Iulian Năstac Computers Programming Course 12 Iulian Năstac Recap from previous course Strings in C The character string is one of the most widely used applications that involves vectors. A string in C is an array of

More information

LAB7 : Characters and Strings

LAB7 : Characters and Strings 1 LAB7 : Characters and Strings Task1: Write a C Program to Copy One String into Other Without Using Library Function. (use pointer) char s1[100], s2[100]; printf("\nenter the string :"); gets(s1); i =

More information

Fundamental of Programming (C)

Fundamental of Programming (C) Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 7 Array and String Department of Computer Engineering Outline Array String Department

More information

A function is a named group of statements developed to solve a sub-problem and returns a value to other functions when it is called.

A function is a named group of statements developed to solve a sub-problem and returns a value to other functions when it is called. Chapter-12 FUNCTIONS Introduction A function is a named group of statements developed to solve a sub-problem and returns a value to other functions when it is called. Types of functions There are two types

More information

Grade Distribution. Exam 1 Exam 2. Exams 1 & 2. # of Students. Total: 17. Total: 17. Total: 17

Grade Distribution. Exam 1 Exam 2. Exams 1 & 2. # of Students. Total: 17. Total: 17. Total: 17 Grade Distribution Exam 1 Exam 2 Score # of Students Score # of Students 16 4 14 6 12 4 10 2 8 1 Total: 17 Exams 1 & 2 14 2 12 4 10 5 8 5 4 1 Total: 17 Score # of Students 28 2 26 5 24 1 22 4 20 3 18 2

More information

Strings. Chuan-Ming Liu. Computer Science & Information Engineering National Taipei University of Technology Taiwan

Strings. Chuan-Ming Liu. Computer Science & Information Engineering National Taipei University of Technology Taiwan Strings Chuan-Ming Liu Computer Science & Information Engineering National Taipei University of Technology Taiwan 1 Outline String Basic String Library Functions Longer Strings: Concatenation and Whole-Line

More information

Characters and Strings

Characters and Strings Characters and Strings 60-141: Introduction to Algorithms and Programming II School of Computer Science Term: Summer 2013 Instructor: Dr. Asish Mukhopadhyay Character constants A character in single quotes,

More information

Strings. Daily Puzzle

Strings. Daily Puzzle Lecture 20 Strings Daily Puzzle German mathematician Gauss (1777-1855) was nine when he was asked to add all the integers from 1 to 100 = (1+100)+(2+99)+... = 5050. Sum all the digits in the integers from

More information

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size]; Arrays An array is a collection of two or more adjacent memory cells, called array elements. Array is derived data type that is used to represent collection of data items. C Array is a collection of data

More information

Course organization. Course introduction ( Week 1)

Course organization. Course introduction ( Week 1) Course organization Course introduction ( Week 1) Code editor: Emacs Part I: Introduction to C programming language (Week 2-9) Chapter 1: Overall Introduction (Week 1-3) Chapter 2: Types, operators and

More information

Strings(2) CS 201 String. String Constants. Characters. Strings(1) Initializing and Declaring String. Debzani Deb

Strings(2) CS 201 String. String Constants. Characters. Strings(1) Initializing and Declaring String. Debzani Deb CS 201 String Debzani Deb Strings(2) Two interpretations of String Arrays whose elements are characters. Pointer pointing to characters. Strings are always terminated with a NULL characters( \0 ). C needs

More information

Introduction to C/C++ Lecture 5 - String & its Manipulation

Introduction to C/C++ Lecture 5 - String & its Manipulation Introduction to C/C++ Lecture 5 - String & its Manipulation Rohit Sehgal Nishit Majithia Association of Computing Activities, Indian Institute of Technology,Kanpur rsehgal@cse.iitk.ac.in nishitm@cse.iitk.ac.in

More information

C Characters and Strings

C Characters and Strings CS 2060 Character handling The C Standard Library provides many functions for testing characters in ctype.h. int isdigit(int c); // is c a digit (0-9)? int isalpha(int c); // is c a letter? int isalnum(int

More information

C-String Library Functions

C-String Library Functions Strings Class 34 C-String Library Functions there are several useful functions in the cstring library strlen: the number of characters before the \0 strncat: concatenate two strings together strncpy: overwrite

More information

Lecture 10 Arrays (2) and Strings. UniMAP SEM II - 11/12 DKT121 1

Lecture 10 Arrays (2) and Strings. UniMAP SEM II - 11/12 DKT121 1 Lecture 10 Arrays (2) and Strings UniMAP SEM II - 11/12 DKT121 1 Outline 8.1 Passing Arrays to Function 8.2 Displaying Array in a Function 8.3 How Arrays are passed in a function call 8.4 Introduction

More information

CS107 Spring 2019, Lecture 4 C Strings

CS107 Spring 2019, Lecture 4 C Strings CS107 Spring 2019, Lecture 4 C Strings Reading: K&R (1.9, 5.5, Appendix B3) or Essential C section 3 This document is copyright (C) Stanford Computer Science and Nick Troccoli, licensed under Creative

More information

mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut

mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut dthiebaut@smithedu Outline A Few Words about HW 8 Finish the Input Port Lab! Revisiting Homework

More information

Chapter 5. Section 5.4 The Common String Library Functions. CS 50 Hathairat Rattanasook

Chapter 5. Section 5.4 The Common String Library Functions. CS 50 Hathairat Rattanasook Chapter 5 Section 5.4 The Common String Library Functions CS 50 Hathairat Rattanasook Library Functions We already discussed the library function fgets() Library functions are available: to find the length

More information

Test Paper 3 Programming Language Solution Question 1: Briefly describe the structure of a C program. A C program consists of (i) functions and (ii) statements There should be at least one function called

More information

1. Simple if statement. 2. if else statement. 3. Nested if else statement. 4. else if ladder 1. Simple if statement

1. Simple if statement. 2. if else statement. 3. Nested if else statement. 4. else if ladder 1. Simple if statement UNIT- II: Control Flow: Statements and Blocks, if, switch statements, Loops: while, do-while, for, break and continue, go to and Labels. Arrays and Strings: Introduction, One- dimensional arrays, Declaring

More information

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

CSCI 6610: Intermediate Programming / C Chapter 12 Strings ... 1/26 CSCI 6610: Intermediate Programming / C Chapter 12 Alice E. Fischer February 10, 2016 ... 2/26 Outline The C String Library String Processing in C Compare and Search in C C++ String Functions

More information

PES INSTITUTE OF TECHNOLOGY (BSC) I MCA, First IA Test, November 2015 Programming Using C (13MCA11) Solution Set Faculty: Jeny Jijo

PES INSTITUTE OF TECHNOLOGY (BSC) I MCA, First IA Test, November 2015 Programming Using C (13MCA11) Solution Set Faculty: Jeny Jijo PES INSTITUTE OF TECHNOLOGY (BSC) I MCA, First IA Test, November 2015 Programming Using C (13MCA11) Solution Set Faculty: Jeny Jijo 1. (a)what is an algorithm? Draw a flowchart to print N terms of Fibonacci

More information

Multiple Choice Questions ( 1 mark)

Multiple Choice Questions ( 1 mark) Multiple Choice Questions ( 1 mark) Unit-1 1. is a step by step approach to solve any problem.. a) Process b) Programming Language c) Algorithm d) Compiler 2. The process of walking through a program s

More information

CS107, Lecture 4 C Strings

CS107, Lecture 4 C Strings CS107, Lecture 4 C Strings Reading: K&R (1.9, 5.5, Appendix B3) or Essential C section 3 This document is copyright (C) Stanford Computer Science and Nick Troccoli, licensed under Creative Commons Attribution

More information

Important Questions for Viva CPU

Important Questions for Viva CPU Important Questions for Viva CPU 1. List various components of a computer system. i. Input Unit ii. Output Unit iii. Central processing unit (Control Unit + Arithmetic and Logical Unit) iv. Storage Unit

More information

1 Pointer Concepts. 1.1 Pointer Examples

1 Pointer Concepts. 1.1 Pointer Examples 1 1 Pointer Concepts What are pointers? How are they used? Point to a memory location. Call by reference is based on pointers. Operators: & Address operator * Dereferencing operator Machine/compiler dependencies

More information

Computers Programming Course 5. Iulian Năstac

Computers Programming Course 5. Iulian Năstac Computers Programming Course 5 Iulian Năstac Recap from previous course Classification of the programming languages High level (Ada, Pascal, Fortran, etc.) programming languages with strong abstraction

More information

Computers Programming Course 10. Iulian Năstac

Computers Programming Course 10. Iulian Năstac Computers Programming Course 10 Iulian Năstac Recap from previous course 5. Values returned by a function A return statement causes execution to leave the current subroutine and resume at the point in

More information

C-LANGUAGE CURRICULAM

C-LANGUAGE CURRICULAM C-LANGUAGE CURRICULAM Duration: 2 Months. 1. Introducing C 1.1 History of C Origin Standardization C-Based Languages 1.2 Strengths and Weaknesses Of C Strengths Weaknesses Effective Use of C 2. C Fundamentals

More information

Write a C program using arrays and structure

Write a C program using arrays and structure 03 Arrays and Structutes 3.1 Arrays Declaration and initialization of one dimensional, two dimensional and character arrays, accessing array elements. (10M) 3.2 Declaration and initialization of string

More information

C Programming Language

C Programming Language C Programming Language Character Strings and String Functions Manar Mohaisen Office: F208 Email: manar.subhi@kut.ac.kr Department of EECE Review of the Precedent Lecture Explained dynamic data allocation

More information

Programming in C. Session 7. Seema Sirpal Delhi University Computer Centre

Programming in C. Session 7. Seema Sirpal Delhi University Computer Centre Programming in C Session 7 Seema Sirpal Delhi University Computer Centre Relationship between Pointers & Arrays In some cases, a pointer can be used as a convenient way to access or manipulate the data

More information

Structured programming

Structured programming Exercises 10 Version 1.0, 13 December, 2016 Table of Contents 1. Strings...................................................................... 1 1.1. Remainders from lectures................................................

More information

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

CSC209H Lecture 4. Dan Zingaro. January 28, 2015 CSC209H Lecture 4 Dan Zingaro January 28, 2015 Strings (King Ch 13) String literals are enclosed in double quotes A string literal of n characters is represented as a n+1-character char array C adds a

More information

C Programming Language Review and Dissection III

C Programming Language Review and Dissection III C Programming Language Review and Dissection III Lecture 5 Embedded Systems 5-1 Today Pointers Strings Formatted Text Output Reading Assignment: Patt & Patel Pointers and Arrays Chapter 16 in 2 nd edition

More information

C Programming Multiple. Choice

C Programming Multiple. Choice C Programming Multiple Choice Questions 1.) Developer of C language is. a.) Dennis Richie c.) Bill Gates b.) Ken Thompson d.) Peter Norton 2.) C language developed in. a.) 1970 c.) 1976 b.) 1972 d.) 1980

More information

It is necessary to have a single function main in every C program, along with other functions used/defined by the programmer.

It is necessary to have a single function main in every C program, along with other functions used/defined by the programmer. Functions A number of statements grouped into a single logical unit are called a function. The use of function makes programming easier since repeated statements can be grouped into functions. Splitting

More information

UNIT -5 FUNCTIONS AND POINTERS

UNIT -5 FUNCTIONS AND POINTERS 1. FUNCTIONS: UNIT -5 FUNCTIONS AND POINTERS C language programs are highly dependent on functions. Program always starts with user defined main () function. C supports two types of functions. They are,

More information

Computer Programming Unit v

Computer Programming Unit v READING AND WRITING CHARACTERS We can read and write a character on screen using printf() and scanf() function but this is not applicable in all situations. In C programming language some function are

More information

Chapter 13. Strings. Introduction

Chapter 13. Strings. Introduction Chapter 13 Strings 1 Introduction This chapter covers both string constants (or literals, as they re called in the C standard) and string variables. Strings are arrays of characters in which a special

More information

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University ITC213: STRUCTURED PROGRAMMING Bhaskar Shrestha National College of Computer Studies Tribhuvan University Lecture 10: Arrays Readings: Chapter 9 Introduction Group of same type of variables that have same

More information

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows Unti 4: C Arrays Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type An array is used to store a collection of data, but it is often more useful

More information

CSCE150A. Introduction. Basics. String Library. Substrings. Line Scanning. Sorting. Command Line Arguments. Misc CSCE150A. Introduction.

CSCE150A. Introduction. Basics. String Library. Substrings. Line Scanning. Sorting. Command Line Arguments. Misc CSCE150A. Introduction. Chapter 9 Scanning Computer Science & Engineering 150A Problem Solving Using Computers Lecture 07 - Strings Stephen Scott (Adapted from Christopher M. Bourke) Scanning 9.1 String 9.2 Functions: Assignment

More information

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 9. Strings. Notes. Notes. Notes. Lecture 07 - Strings

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 9. Strings. Notes. Notes. Notes. Lecture 07 - Strings Computer Science & Engineering 150A Problem Solving Using Computers Lecture 07 - Strings Scanning Stephen Scott (Adapted from Christopher M. Bourke) 1 / 51 Fall 2009 cbourke@cse.unl.edu Chapter 9 Scanning

More information

UNIT 2 STRINGS. Marks - 7

UNIT 2 STRINGS. Marks - 7 1 UNIT 2 STRINGS Marks - 7 SYLLABUS 2.1 String representation : Reading and Writing Strings 2.2 String operations : Finding length of a string, Converting Characters of a string into upper case and lower

More information

Strings. Part III. 1) Introduction. 2) Definition. 3) Using Strings. 4) Input/Output. 5) Using string.h Library. 6) Strings as Function Parameters

Strings. Part III. 1) Introduction. 2) Definition. 3) Using Strings. 4) Input/Output. 5) Using string.h Library. 6) Strings as Function Parameters EE105: Software Engineering II Part 3 Strings page 1 of 18 Part III Strings 1) Introduction 2) Definition 3) Using Strings 4) Input/Output 5) Using string.h Library 6) Strings as Function Parameters 7)

More information

Computer Science & Engineering 150A Problem Solving Using Computers

Computer Science & Engineering 150A Problem Solving Using Computers Computer Science & Engineering 150A Problem Solving Using Computers Lecture 07 - Strings Stephen Scott (Adapted from Christopher M. Bourke) 1 / 51 Fall 2009 Chapter 9 9.1 String 9.2 Functions: Assignment

More information

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world alun@debian:~$ gcc hello.c alun@debian:~$ a.out Hello, world alun@debian:~$ gcc -o hello hello.c alun@debian:~$ hello Hello, world alun@debian:~$ 1 A Quick guide to C for Networks and Operating Systems

More information

Overview. Concepts this lecture String constants Null-terminated array representation String library <strlib.h> String initializers Arrays of strings

Overview. Concepts this lecture String constants Null-terminated array representation String library <strlib.h> String initializers Arrays of strings CPE 101 slides based on UW course Lecture 19: Strings Overview Concepts this lecture String constants ull-terminated array representation String library String initializers Arrays of strings

More information

PDS Class Test 2. Room Sections No of students

PDS Class Test 2. Room Sections No of students PDS Class Test 2 Date: October 27, 2016 Time: 7pm to 8pm Marks: 20 (Weightage 50%) Room Sections No of students V1 Section 8 (All) Section 9 (AE,AG,BT,CE, CH,CS,CY,EC,EE,EX) V2 Section 9 (Rest, if not

More information

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs. 1 3. Functions 1. What are the merits and demerits of modular programming? Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

More information

Unit 6 Files. putchar(ch); ch = getc (fp); //Reads single character from file and advances position to next character

Unit 6 Files. putchar(ch); ch = getc (fp); //Reads single character from file and advances position to next character 1. What is File management? In real life, we want to store data permanently so that later on we can retrieve it and reuse it. A file is a collection of bytes stored on a secondary storage device like hard

More information

Unit 4 Preprocessor Directives

Unit 4 Preprocessor Directives 1 What is pre-processor? The job of C preprocessor is to process the source code before it is passed to the compiler. Source Code (test.c) Pre-Processor Intermediate Code (test.i) Compiler The pre-processor

More information

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University ITC213: STRUCTURED PROGRAMMING Bhaskar Shrestha National College of Computer Studies Tribhuvan University Lecture 07: Data Input and Output Readings: Chapter 4 Input /Output Operations A program needs

More information

CSC 2400: Computer Systems. Arrays and Strings in C

CSC 2400: Computer Systems. Arrays and Strings in C CSC 2400: Computer Systems Arrays and Strings in C Lecture Overview Arrays! List of elements of the same type Strings! Array of characters ending in \0! Functions for manipulating strings 1 Arrays: C vs.

More information

Department of Computer Applications

Department of Computer Applications Sheikh Ul Alam Memorial Degree College Mr. Ovass Shafi. (Assistant Professor) C Language An Overview (History of C) C programming languages is the only programming language which falls under the category

More information

Chapter 10. Arrays and Strings

Chapter 10. Arrays and Strings Christian Jacob Chapter 10 Arrays and Strings 10.1 Arrays 10.2 One-Dimensional Arrays 10.2.1 Accessing Array Elements 10.2.2 Representation of Arrays in Memory 10.2.3 Example: Finding the Maximum 10.2.4

More information

Contents. A Review of C language. Visual C Visual C++ 6.0

Contents. A Review of C language. Visual C Visual C++ 6.0 A Review of C language C++ Object Oriented Programming Pei-yih Ting NTOU CS Modified from www.cse.cuhk.edu.hk/~csc2520/tuto/csc2520_tuto01.ppt 1 2 3 4 5 6 7 8 9 10 Double click 11 12 Compile a single source

More information

SYSC 2006 C Winter String Processing in C. D.L. Bailey, Systems and Computer Engineering, Carleton University

SYSC 2006 C Winter String Processing in C. D.L. Bailey, Systems and Computer Engineering, Carleton University SYSC 2006 C Winter 2012 String Processing in C D.L. Bailey, Systems and Computer Engineering, Carleton University References Hanly & Koffman, Chapter 9 Some examples adapted from code in The C Programming

More information

C mini reference. 5 Binary numbers 12

C mini reference. 5 Binary numbers 12 C mini reference Contents 1 Input/Output: stdio.h 2 1.1 int printf ( const char * format,... );......................... 2 1.2 int scanf ( const char * format,... );.......................... 2 1.3 char

More information

EM108 Software Development for Engineers

EM108 Software Development for Engineers EE108 Section 3 Strings page 1 of 25 EM108 Software Development for Engineers Section 3 - Strings 1) Introduction 2) Definition 3) Using Strings 4) Input/Output 5) Using string.h Library 6) Strings as

More information

7.STRINGS. Data Structure Management (330701) 1

7.STRINGS. Data Structure Management (330701)   1 Strings and their representation String is defined as a sequence of characters. 7.STRINGS In terms of c language string is an array of characters. While storing the string in character array the size of

More information

Procedural Programming

Procedural Programming Exercise 5 (SS 2016) 28.06.2016 What will I learn in the 5. exercise Strings (and a little bit about pointer) String functions in strings.h Files Exercise(s) 1 Home exercise 4 (3 points) Write a program

More information

Procedural Programming & Fundamentals of Programming

Procedural Programming & Fundamentals of Programming Procedural Programming & Fundamentals of Programming Exercise 3 (SS 2018) 29.05.2018 What will I learn in the 4. exercise Pointer (and a little bit about memory allocation) Structure Strings and String

More information

Chapter 21: Introduction to C Programming Language

Chapter 21: Introduction to C Programming Language Ref. Page Slide 1/65 Learning Objectives In this chapter you will learn about: Features of C Various constructs and their syntax Data types and operators in C Control and Loop Structures in C Functions

More information

A First Book of ANSI C Fourth Edition. Chapter 9 Character Strings

A First Book of ANSI C Fourth Edition. Chapter 9 Character Strings A First Book of ANSI C Fourth Edition Chapter 9 Character Strings Objectives String Fundamentals Library Functions Input Data Validation Formatting Strings (Optional) Case Study: Character and Word Counting

More information

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath UNIT - I Introduction to C Programming Introduction to C C was originally developed in the year 1970s by Dennis Ritchie at Bell Laboratories, Inc. C is a general-purpose programming language. It has been

More information

CSC 2400: Computer Systems. Arrays and Strings in C

CSC 2400: Computer Systems. Arrays and Strings in C CSC 2400: Computer Systems Arrays and Strings in C Lecture Overview Arrays! List of elements of the same type Strings! Array of characters ending in \0! Functions for manipulating strings 1 Arrays in C

More information

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE NOTE: 1. There are TWO PARTS in this Module/Paper. PART ONE contains FOUR questions and PART TWO contains FIVE questions. 2. PART ONE is to be

More information

I2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub

I2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub Lebanese University Faculty of Science Computer Science BS Degree I2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub 2Computer Science BS Degree -Imperative Programming

More information