Reading and Writing String

Similar documents
ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

Sources: On the Web: Slides will be available on:

So far we have considered only numeric processing, i.e. processing of numeric data represented

File Handling. What is a file?

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

Stacks. Linear data structures

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

Basics of I/O Streams and File I/O

C Programming. for Embedded Microcontrollers. Warwick A. Smith. Postbus 11. Elektor International Media BV. 6114ZG Susteren The Netherlands

System Calls and Standard I/O

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 3: Input/Output

C Strings and Pointers

Introduction to Data Structures

5 Arrays and Pointers

Education: P.h.D. Candidate (Santa Clara University, California) M.S. in Computer Engineering (Santa Clara University, California)

1. Define: (a) Variable, (b) Constant, (c) Type, (d) Enumerated Type, (e) Identifier.

The C Programming Language course syllabus associate level

Module 816. File Management in C. M. Campbell 1993 Deakin University

Tutorial on C Language Programming

Lab Experience 17. Programming Language Translation

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Passing 1D arrays to functions.

Oracle Database: SQL and PL/SQL Fundamentals

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

An Incomplete C++ Primer. University of Wyoming MA 5310

C PROGRAMMING FOR MATHEMATICAL COMPUTING

How To Write Portable Programs In C

TCP/IP Networking, Part 2: Web-Based Control

3.GETTING STARTED WITH ORACLE8i

Exercise 1: Python Language Basics

arrays C Programming Language - Arrays

SQL is capable in manipulating relational data SQL is not good for many other tasks

Introduction to Programming II Winter, 2014 Assignment 2

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Oracle SQL. Course Summary. Duration. Objectives

ISIS Application Program Interface ISIS_DLL User s Manual. Preliminary Version

J a v a Quiz (Unit 3, Test 0 Practice)

1 Abstract Data Types Information Hiding

Chapter 7 Assembly Language

Crash Dive into Python

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Computer Programming I

Oracle 10g PL/SQL Training

CS 464/564 Introduction to Database Management System Instructor: Abdullah Mueen

Environnements et Outils de Développement Cours 6 Building with make

Management Information Systems 260 Web Programming Fall 2006 (CRN: 42459)

C++ Input/Output: Streams

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Project 2: Bejeweled

Conditionals (with solutions)

A single register, called the accumulator, stores the. operand before the operation, and stores the result. Add y # add y from memory to the acc

LAB #3 VHDL RECOGNITION AND GAL IC PROGRAMMING USING ALL-11 UNIVERSAL PROGRAMMER

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Organization of Records in Blocks

Chapter 2: Elements of Java

Preparing your data for analysis using SAS. Landon Sego 24 April 2003 Department of Statistics UW-Madison

C Interview Questions

Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators

WESTMORELAND COUNTY PUBLIC SCHOOLS Integrated Instructional Pacing Guide and Checklist Computer Math

風 水. Heap Feng Shui in JavaScript. Alexander Sotirov.

Chapter 3 Writing Simple Programs. What Is Programming? Internet. Witin the web server we set lots and lots of requests which we need to respond to

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Chapter 13 Storage classes

COMPUTER SCIENCE 1999 (Delhi Board)

El Dorado Union High School District Educational Services

Logistics. Software Testing. Logistics. Logistics. Plan for this week. Before we begin. Project. Final exam. Questions?

Java Basics: Data Types, Variables, and Loops

The Payroll Program. Payroll

One positive experience I've had in the last 24 hours: Exercise today:

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Unit Write iterative and recursive C functions to find the greatest common divisor of two integers. [6]

MATLAB: Strings and File IO

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

CS107L Handout 02 Autumn 2007 October 5, 2007 Copy Constructor and operator=

C Dynamic Data Structures. University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell

Oracle Database: SQL and PL/SQL Fundamentals

Database 2 Lecture I. Alessandro Artale

Character Translation Methods

Software security. Buffer overflow attacks SQL injections. Lecture 11 EIT060 Computer Security

Lecture 11 Array of Linked Lists

(hours worked,rateofpay, and regular and over time pay) for a list of employees. We have

3/13/2012. Writing Simple C Programs. ESc101: Decision making using if-else and switch statements. Writing Simple C Programs

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Introduction to: Computers & Programming: Review for Midterm 2

EXEC SQL CONNECT :userid IDENTIFIED BY :passwd;

MarshallSoft Client Mailer. Tutorial

Documentum Developer Program

VHDL Test Bench Tutorial

USART and Asynchronous Communication

C++ Programming Language

As previously noted, a byte can contain a numeric value in the range Computers don't understand Latin, Cyrillic, Hindi, Arabic character sets!

Reshaping & Combining Tables Unit of analysis Combining. Assignment 4. Assignment 4 continued PHPM 672/677 2/21/2016. Kum 1

Pitfalls in Embedded Software

The stack and the stack pointer

Transcription:

Reading and writing strings Reading and Writing String Reading Format conversion %s can be used in scanf for reading strings not containing white spaces: scanf("%s", str) & not required before str as it is a pointer. C string library provides gets(str) to read a string. It does not skip white spaces like scanf does. But, scanf and gets have no way to know the size of the array in advance. So potentially, they could attempt to store character past the array. scanf can use "%.ns" conversion to avoid this problem. But gets is dangerous. R. K. Ghosh (IIT-Kanpur) C Programming April 6, 2011 2 / 4

Reading and writing strings Reading and Writing String Reading Character by Character Since both scanf and gets are potentially risky, character by character reading using getchar, and similarly writing using putchar are safer to use. Code for readline for example will be: i n t r e a d L i n e ( char s t r [ ], i n t n ) { i n t ch, i = 0 ; w h i l e ( ( ch = g e t c h a r ( ) )!= \n ) { i f ( i < n ) s t r [ i ++] = ch ; s t r [ i ] = \0 ; r e t u r n i ; R. K. Ghosh (IIT-Kanpur) C Programming April 6, 2011 2 / 4

Reading and writing strings Reading and Writing String Accessing Characters in a String i n t countwhitespace ( c o n s t s [ ] ) { i n t c n t = 0, i ; f o r ( i = 0 ; s [ i ]!= \0 ; i ++) { i f ( s [ i ] = ) c n t++; r e t u r n c n t ; i n t countwhitespaces ( c o n s t char s ) { f o r ( ; s!= \0, s++) i f ( s == ) c n t++; r e t u r n c n t ; R. K. Ghosh (IIT-Kanpur) C Programming April 6, 2011 2 / 4

Library functions Manipulating String Functions Direct copy or compare strings result in failures. // C o n s i d e r f o l l o w i n g code s n i p p e t char s t r A [ 1 0 ], s t r B [ 1 0 ] ; s t r A = abc ; s t r B = s t r A ; // WRONG ( a s s i g n m e n t o f i n c o m p a t i b l e t y p e s ) i f ( s t r A == s t r B ) // WRONG ( p o i n t e r s compared. ) r e t u r n s 0 R. K. Ghosh (IIT-Kanpur) C Programming April 6, 2011 3 / 4

Library functions Manipulating String Functions Then how do we do it? C provide rich set of string functions. #include <string.h> to access these string manipulation functions. strlen(s): returns length of the string. strcpy(dest,src): copies one string into another. strcat(dest,src): appends one string to another. strcmp(first,second): return 1 if equal 0 otherwise. R. K. Ghosh (IIT-Kanpur) C Programming April 6, 2011 3 / 4

Library functions Manipulating Length of a String #i n c l u d e <s t d i o. h> i n t r e a d L i n e ( char s t r [ ], i n t n ) ; i n t l e n ( c o n s t char s ) ; i n t main ( ) { char s t r [ 5 0 ] ; r e a d L i n e ( s t r, 4 9 ) ; p r i n t f ( Length = %d\n, l e n ( s t r ) ) ; i n t l e n ( c o n s t char s ) { i n t n = 0 ; f o r ( ; s ; s++) // when s = \ 0 l o o p e x i t s n++; r e t u r n n ; R. K. Ghosh (IIT-Kanpur) C Programming April 6, 2011 3 / 4

Example Example (1) Suppose a string stores a name in the form First/Last. We need a function to find / in the name. There is a C function strchr that can be used. But we want to write a function which takes a pointer to string and a charater and returns a pointer to the separator.

Example Example (1 contd) #i n c l u d e <s t d i o. h> #i n c l u d e < s t r i n g. h> char f i r s t L a s t ( char s, char ch ) { char p = s ; w h i l e ( p!= ch ) { i f ( p == \0 ) r e t u r n NULL ; p++; r e t u r n p ;

Example Example (1 contd.) i n t main ( ) { char s t r [ ] = Deigo / Maradona ; char ptr, q, c = / ; i n t l = s t r l e n ( s t r ) ; p t r = f i r s t L a s t ( s t r, c ) ; f o r ( q = s t r ; q < p t r ; q++) p r i n t f ( %c, q ) ; p r i n t f ( ) ; f o r ( q = p t r +1; q < s t r + l ; q++) p r i n t f ( %c, q ) ; p r i n t f ( \n ) ;

Example Example (2) Let us write the following program: After user enter words, program determines which words comes first and last if the words were listed in dictionary order. The programs stop accepting words once the user enters a four-letter word. 1 Initially read one word, define it both as the smallest and the largest. 2 If the word read has 4 letters stop, and print the smallest and the largest. 3 Otherwise, read next word, update the smallest and the largest.

Example (2 contd.) #i n c l u d e <s t d i o. h> #i n c l u d e < s t r i n g. h> i n t main ( ) { char s m a l l e s t [ 2 0 ] ; // s t o r e s minimum char l a r g e s t [ 2 0 ] ; // s t o r e s maximum char word [ 2 0 ] ; // s t o r e s c u r r e n t word char ch ; i n t i = 0 ; p r i n t f ( E n t e r t h e words \n ) ; / Rest o f t h e code /

Example (2 contd.) // Read t h e f i r s t word, s e t i t as t h e s m a l l e s t word w h i l e ( ( ch = g e t c h a r ( ) )!= \n && i < 19) s m a l l e s t [ i ++] = ch ; s m a l l e s t [ i ] = \0 ; // end s t r i n g i f ( s t r l e n ( s m a l l e s t ) == 4) { // no more i n p u t p r i n t f ( s m a l l e s t word : %s \n, s m a l l e s t ) ; p r i n t f ( l a r g e s t word : %s \n, s m a l l e s t ) ; r e t u r n ; // s t o p // I n i t i a l l y t h e s m a l l e s t i s a l s o t h e l a r g e s t s t r c p y ( l a r g e s t, s m a l l e s t ) ; / Rest o f t h e code f o r r e a d i n g more words /

Example (2 contd.) w h i l e ( 1 ) { i = 0 ; w h i l e ( ( ch = g e t c h a r ( ) )!= \n && i < 19) word [ i ++] = ch ; i f ( strcmp ( word, s m a l l e s t ) < 0) // compare w i t h s m a l l e s t s t r c p y ( s m a l l e s t, word ) ; // c u r r e n t i s s m a l l e s t i f ( strcmp ( word, l a r g e s t ) > 0) // compare w i t h l a r g e s t s t r c p y ( l a r g e s t, word ) ; // c u r r e n t i s l a r g e s t i f ( s t r l e n ( word ) == 4) // e x i t on a word w i t h 4 l e t t e r s break ; p r i n t f ( s m a l l e s t word : %s \n, s m a l l e s t ) ; // p r i n t s m a l l e s t p r i n t f ( l a r g e s t word : %s \n, l a r g e s t ) ; // p r i n t l a r g e s t

Example (3) Create a program for printing calender reminders for events. Programs takes each reminder in form of a date (a digit number) and a string. Each reminder can be a string of at most 60 characters. Upto 50 reminders can be stored. End of input is signalled by 0.

Example (3 input format) Enter day and reminder: 26 Republic day dinner at 7.00PM Enter day and reminder: 5 Movie - "A beautiful mind" Enter day and reminder: 12 Dental appointment Enter day and reminder: 5 Exta class Enter day and reminder: 16 Make up lab Enter day and reminder: 0

Example (3 output Format.) The program should output the following: Day Reminder 5 Exta class 5 Movie - "A beautiful mind" 12 Dental appointment 16 Make up lab 26 republic day dinner at 7.00PM

Example (3 code) The code is fragmented into 5 parts. First part is for reading a line of text and storing it as a string. Second part specifies declaration of all needed variables Third and fourth part implement main logic of reading a line storing it in sorted order. Final part is for printing out the stored reminders.

The code appears in slide No. 12 also. Example (3: reading a line of text ) i n t r e a d L i n e ( char s t r [ ], i n t n ) { i n t ch, i = 0 ; w h i l e ( ( ch = g e t c h a r ( ) )!= \n ) { i f ( i < n ) s t r [ i ++] = ch ; s t r [ i ] = \0 ; r e t u r n i ;

Example (3: variables) #i n c l u d e <s t d i o. h> #i n c l u d e < s t r i n g. h> #d e f i n e MAX REMIND 50 #d e f i n e MSG LEN 60 i n t r e a d L i n e ( char s t r [ ], i n t n ) ; i n t main ( ) { char r e m i n d e r s [ MAX REMIND ] [ MSG LEN + 3 ] ; char d a y s t r [ 3 ], m s g s t r [ MSG LEN + 1 ] ; i n t day, i, j, n r e m i n d e r s = 0 ; / Rest o f t h e program /

Example (3 code: main loop) f o r ( ; ; ) { i f ( n r e m i n d e r s == MAX REMIND) { p r i n t f ( No s p a c e f o r i n s e r t i o n \n ) ; break ; p r i n t f ( E n t e r day and r e m i n d e r : ) ; s c a n f ( %2d, &day ) ; // r e a d t h e day i f ( day == 0) // 0 i n p u t > no more r e m i n d e r s break ; // w r i t e day i n t o c h a r a c t e r a r r a y d a y s t r s p r i n t f ( d a y s t r, %2d, day ) ; / Other p a r t o f f o r l o o p on n e x t s l i d e /