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

Similar documents
Introduction to C/C++ Lecture 3 - Program Flow Control

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

Functions: call by reference vs. call by value

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

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

Introduction to C/C++ Lecture 1 - Introduction to Programming

ONE DIMENSIONAL ARRAYS

Scientific Programming in C V. Strings

Introduction to string

Computers Programming Course 11. Iulian Năstac

C mini reference. 5 Binary numbers 12

Create a Program in C (Last Class)

Converting a Lowercase Letter Character to Uppercase (Or Vice Versa)

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

Strings and Library Functions

BITG 1113: Array (Part 2) LECTURE 9

Course organization. Course introduction ( Week 1)

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

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

Fundamentals of Programming

C Characters and Strings

Chapter 8 Character Arrays and Strings

Fundamental of Programming (C)

Chapter 5. Section 5.1 Introduction to Strings. CS 50 Hathairat Rattanasook

by Pearson Education, Inc. All Rights Reserved.

Long Questions. 7. How does union help in storing the values? How it differs from structure?

Week 8 Lecture 3. Finishing up C

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

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

9/9/2017. Prof. Vinod Mahajan

C: Arrays, and strings. Department of Computer Science College of Engineering Boise State University. September 11, /16

Using Character Arrays. What is a String? Using Character Arrays. Using Strings Life is simpler with strings. #include <stdio.

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

CCE1111 Programming for Engineers [C Programming Language]

Chapter 8 C Characters and Strings

Fundamentals of Programming & Procedural Programming

Characters, c-strings, and the string Class. CS 1: Problem Solving & Program Design Using C++

Strings in C++ Dr. Ferdin Joe John Joseph Kamnoetvidya Science Academy

C strings. (Reek, Ch. 9) 1 CS 3090: Safety Critical Programming in C

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

C-LANGUAGE CURRICULAM

CS107 Spring 2019, Lecture 4 C Strings

COMP1917: 09 Arrays and Strings

C Style Strings. Lecture 11 COP 3014 Spring March 19, 2018

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

LECTURE 15. String I/O and cstring library

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

Arrays, Strings, & Pointers

Built-in Functions for NTCAs.

CS107, Lecture 4 C Strings

C programming basics T3-1 -

Computer Programming: Skills & Concepts (CP) Strings

1 Pointer Concepts. 1.1 Pointer Examples

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

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

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

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

Characters, Character Strings, and string-manipulation functions in C

8. Characters, Strings and Files

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

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

Computer Science & Engineering 150A Problem Solving Using Computers

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.

Strings. Steven R. Bagley

Announcements. Strings and Pointers. Strings. Initializing Strings. Character I/O. Lab 4. Quiz. July 18, Special character arrays

Chapter 8 - Characters and Strings

C++ Arrays. Arrays: The Basics. Areas for Discussion. Arrays: The Basics Strings and Arrays of Characters Array Parameters

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

Characters and Strings

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

There are functions to handle strings, so we will see the notion of functions itself in little a detail later. (Refer Slide Time: 00:12)

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

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

C Programming Language

Chapter 11 Strings and Files. Starting Out with Games & Graphics in C++ Tony Gaddis

C: How to Program. Week /May/28

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

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

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Programmer s Points to Remember:

Module: Safe Programming. Professor Trent Jaeger. CSE543 - Introduction to Computer and Network Security

C Programming Language Review and Dissection III

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Fundamentals of Programming. Lecture 11: C Characters and Strings

CMPSC 311- Introduction to Systems Programming Module: Strings

LAB7 : Characters and Strings

Euclid s algorithm, 133

CSC 2400: Computer Systems. Arrays and Strings in C

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Computers Programming Course 10. Iulian Năstac

CSC 2400: Computer Systems. Arrays and Strings in C

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

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

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

This exam is to be taken by yourself with closed books, closed notes, no calculators.

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 11, FALL 2012

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

a) (5 points) What is the output of the following code sequence? int *ptr = 0x1050; printf ("%x\n", ptr--); printf ("%x\n", ptr);

ELEC / COMP 177 Fall Some slides from Kurose and Ross, Computer Networking, 5 th Edition

ARRAYS(II Unit Part II)

Transcription:

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 May 30, 2016 Rohit Sehgal Nishit Majithia (IITK) ACA May 30, 2016 1 / 11

Strings A sequence of characters is often referred to as a character string. A string is stored in an array of type char ending with the null character \0. Rohit Sehgal Nishit Majithia (IITK) ACA May 30, 2016 2 / 11

Strings A sequence of characters is often referred to as a character string. A string is stored in an array of type char ending with the null character \0. A string containing a single character takes up 2 bytes of storage. Rohit Sehgal Nishit Majithia (IITK) ACA May 30, 2016 2 / 11

Strings A sequence of characters is often referred to as a character string. A string is stored in an array of type char ending with the null character \0. A string containing a single character takes up 2 bytes of storage. char a string a takes one byte takes 2 bytes Here a is single character value while a is string with two characters, the first one is a and second one is \0. Rohit Sehgal Nishit Majithia (IITK) ACA May 30, 2016 2 / 11

Strings A sequence of characters is often referred to as a character string. A string is stored in an array of type char ending with the null character \0. A string containing a single character takes up 2 bytes of storage. char a string a takes one byte takes 2 bytes Here a is single character value while a is string with two characters, the first one is a and second one is \0. Declaration of string: char string name[string size]; Rohit Sehgal Nishit Majithia (IITK) ACA May 30, 2016 2 / 11

String Input Use %s field specification in scanf to read string. Rohit Sehgal Nishit Majithia (IITK) ACA May 30, 2016 3 / 11

String Input Use %s field specification in scanf to read string. Ignores leading white spaces. reads characters until next white space encountered. Can use the width value in the field specification to limit the number of characters read: example: scanf( %10s,string); \\It only reads 10 characters Here width of string array should be one more than the defined size in scanf ( i.e char string[11] ) Rohit Sehgal Nishit Majithia (IITK) ACA May 30, 2016 3 / 11

String Input Use %s field specification in scanf to read string. Ignores leading white spaces. reads characters until next white space encountered. Can use the width value in the field specification to limit the number of characters read: example: scanf( %10s,string); \\It only reads 10 characters Here width of string array should be one more than the defined size in scanf ( i.e char string[11] ) scanf requires address of the variable, identifier (pointer) is address itself. Rohit Sehgal Nishit Majithia (IITK) ACA May 30, 2016 3 / 11

Contd.. scan set restrict the scanning input string. Rohit Sehgal Nishit Majithia (IITK) ACA May 30, 2016 4 / 11

Contd.. scan set restrict the scanning input string. Doesn t ignores leading white spaces. Characters read as long as character falls in scan set. example: scanf( %[0123456789],number); \\It only reads numbers Rohit Sehgal Nishit Majithia (IITK) ACA May 30, 2016 4 / 11

Contd.. scan set restrict the scanning input string. Doesn t ignores leading white spaces. Characters read as long as character falls in scan set. example: scanf( %[0123456789],number); \\It only reads numbers Putting at the start to negate the set example: scanf( %[ \n],string); \\read until new line character encountered. Rohit Sehgal Nishit Majithia (IITK) ACA May 30, 2016 4 / 11

String Output Use %s field specification in printf. Characters in string printed until \0 encountered. Rohit Sehgal Nishit Majithia (IITK) ACA May 30, 2016 5 / 11

Main operations on String strcpy strcat strcmp strlen copy one string into another append one string onto the right side of the other compare alphabetic order of two strings return the length of a string Rohit Sehgal Nishit Majithia (IITK) ACA May 30, 2016 6 / 11

strcpy and strncpy Syntax of strcpy strcpy ( destination string, source string ) Copies the characters (including the \0) from the source string to the destination string. Rohit Sehgal Nishit Majithia (IITK) ACA May 30, 2016 7 / 11

strcpy and strncpy Syntax of strcpy strcpy ( destination string, source string ) Copies the characters (including the \0) from the source string to the destination string. Syntax of strncpy strncpy ( destination string, source string, n ) Same as the strcpy, but the copy stops after n characters. Rohit Sehgal Nishit Majithia (IITK) ACA May 30, 2016 7 / 11

strcat Syntax strcat ( destination string, source string ) Appends source string to right hand side of destination string. Rohit Sehgal Nishit Majithia (IITK) ACA May 30, 2016 8 / 11

strcmp Syntax strcmp ( string1, string2 ) Compares string1 and string2 alphabetically. Return a value <0 if string1 precedes string2 ( i.e string1 <string2 ) Return a value >0 if string2 precedes string1 ( i.e string1 >string2 ) Return 0 if string1 equals to string2 Rohit Sehgal Nishit Majithia (IITK) ACA May 30, 2016 9 / 11

strcmp Syntax strcmp ( string1, string2 ) Compares string1 and string2 alphabetically. Return a value <0 if string1 precedes string2 ( i.e string1 <string2 ) Return a value >0 if string2 precedes string1 ( i.e string1 >string2 ) Return 0 if string1 equals to string2 Note: lowercase characters are greater than Uppercase Rohit Sehgal Nishit Majithia (IITK) ACA May 30, 2016 9 / 11

strlen Syntax strlen ( string ) Returns length of the string excluding null character. Rohit Sehgal Nishit Majithia (IITK) ACA May 30, 2016 10 / 11

The End Rohit Sehgal Nishit Majithia (IITK) ACA May 30, 2016 11 / 11