Strings. We ve used strings. Array with base type char. One character per element One extra character: '\0' Literal "Hello" stored as string

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

The C Programming Language course syllabus associate level

Stacks. Linear data structures

1 Abstract Data Types Information Hiding

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

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

Passing 1D arrays to functions.

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

System Calls and Standard I/O

File Handling. What is a file?

5 Arrays and Pointers

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Lecture 5: Java Fundamentals III

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

Documentum Developer Program

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

Variables, Constants, and Data Types

Chapter 13 - The Preprocessor

Simple C Programs. Goals for this Lecture. Help you learn about:

Chapter 2: Elements of Java

Introduction to Programming II Winter, 2014 Assignment 2

Programming languages C

C Strings and Pointers

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

/* File: blkcopy.c. size_t n

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

Basics of I/O Streams and File I/O

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

System Calls Related to File Manipulation

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

Package HadoopStreaming

How To Write Portable Programs In C

Lab 4: Socket Programming: netcat part

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

Lecture 24 Systems Programming in C

C++ Language Tutorial

Embedded Systems Design Course Applying the mbed microcontroller

Illustration 1: Diagram of program function and data flow

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Secrets of printf. 1 Background. 2 Simple Printing. Professor Don Colton. Brigham Young University Hawaii. 2.1 Naturally Special Characters

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

CS 106 Introduction to Computer Science I

How To Write An Array In A Microsoft Zil

The programming language C. sws1 1

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

public static void main(string[] args) { System.out.println("hello, world"); } }

Answers to Review Questions Chapter 7

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

Advanced Bash Scripting. Joshua Malone

University of Hull Department of Computer Science. Wrestling with Python Week 01 Playing with Python

8.5. <summary> Cppcheck addons Using Cppcheck addons Where to find some Cppcheck addons

Java Basics: Data Types, Variables, and Loops

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

Informatica e Sistemi in Tempo Reale

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT COURSE CURRICULUM. Course Title: Advanced Computer Programming (Code: )

CS193D Handout 06 Winter 2004 January 26, 2004 Copy Constructor and operator=

C Interview Questions

Fondamenti di C++ - Cay Horstmann 1

CS106A, Stanford Handout #38. Strings and Chars

Introduction to Python

MPLAB TM C30 Managed PSV Pointers. Beta support included with MPLAB C30 V3.00

Comparing RTOS to Infinite Loop Designs

Displaying a Numerical Value on HyperTerminal (or equ.)

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Outline Basic concepts of Python language

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

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

C / C++ and Unix Programming. Materials adapted from Dan Hood and Dianna Xu

Lecture 22: C Programming 4 Embedded Systems

Python Lists and Loops

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

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

Java Interview Questions and Answers

XMOS Programming Guide

MATLAB: Strings and File IO

An Implementation of a Tool to Detect Vulnerabilities in Coding C and C++

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

El Dorado Union High School District Educational Services

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

Pemrograman Dasar. Basic Elements Of Java

C Examples! Jennifer Rexford!

Chapter 3. Input and output. 3.1 The System class

Member Functions of the istream Class

Binary storage of graphs and related data

Exceptions in MIPS. know the exception mechanism in MIPS be able to write a simple exception handler for a MIPS machine

CMSC 202H. ArrayList, Multidimensional Arrays

LOW LEVEL FILE PROCESSING

Introduction to Data Structures

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

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

Exercise 1: Python Language Basics

Install Java Development Kit (JDK) 1.8

Using C to Access Data Stored in Program Memory on the TMS320C54x DSP

Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies)

Tutorial on C Language Programming

Transcription:

1 Programming in C

Strings We ve used strings Hello is string literal constant Array with base type char One character per element One extra character: '\0' Called null character End marker Literal "Hello" stored as string 2

String Variable Declaration Array of characters: Declares a c-string variable to hold up to 9 characters plus one null character No initial value 3

String Variable Typically a partially filled array Declare large enough to hold max-size string, including the null character. Given a standard array: If s contains string Hi Mom!, then stored as: 4

String Variable Initialization Can initialize string: Need not fill entire array Initialization places '\0' at end 5

String Variable Initialization Can omit array-size: Automatically makes size one more than length of quoted string NOT same as: IS same as: 6

String Indexes A string IS an array Can access indexed variables of: hi[0] is H hi[1] is i hi[2] is \0 hi[3] is unknown hi[4] is unknown 7

String Index Manipulation Can manipulate array elements Be careful! Here, \0 (null) was overwritten by a! If null overwritten, string no longer acts like a string! Unpredictable results! 8

String Library Used for string manipulations Normally want to do fun things with strings Requires library string.h: http://en.wikipedia.org/wiki/string.h 9

String Length: strlen Often useful to know length of string strlen(string) Returns number of characters Does not include null Return type is size_t so type cast may be required 10

= with strings Strings are not like other variables, they are arrays Cannot assign: Must use string library function for assignment: strcpy(destination, source) NO checks for size up to programmer! Assign value of msg to Hello : Or strncpy(destination, source, limit) No ending null character if limit is reached 11

== with strings Cannot use operator == to compare Must use strcmp string library function to compare: strcmp(string1, string2) Returns zero int if string1 is equal to string 2 Returns <0 int if string1 is less than string2 Returns >0 int if string1 is greater than string2 12

String Concatenate: strcat Appends one string onto end of another strcat(destination, source) Be careful when concatenating words msg1 is missing space after Hello msg2 is correct 13

String Parameters to Functions A string is an array, so String parameter is an array parameter Strings passed to a function can be changed by the receiving function! Like all arrays, typical to send size as well Function could also use \0 to find end 14

String Input and Output Watch input size of string Must be large enough to hold entered string! + \n perhaps + \0 C gives no warnings of input size issues! Functions in stdio.h 15

Character Input: getchar Reads one character at a time from a text stream int getchar( ) Reads the next character from the standard input stream and returns its value Return type is int! Will convert if assigned to char 16

Character Output: %s and putchar Format string placeholder for string: %s putchar: Writes one character at a time int putchar (int outchar) Writes the parameter to standard output If successful, returns the character written 17

String variable String Input: gets char *gets (char *strptr) Inputs a line (terminated by a newline) from standard input Converts newline to \0 If successful, returns the string and also places it in argument Warning: Does not check length of input gcc may produce warning message 18

String Input: fgets String variable Use stdin for now char *fgets (char * strptr, int size, FILE *fp) Inputs characters from the specified file pointer through \n or until specifed size is reached Puts newline (\n) in the string if size not reached!!! Appends \0 at the end of the string If successful, returns the string & places in argument 19

String variable or constant String Output: puts int puts (const char *strptr) Takes a null-terminated string from memory and writes it to standard output Writes \n in place of \0 20

String variable String Output: fputs Use stdout for now int fputs (const char *strptr, FILE *fp) Takes a null-terminated string from memory and writes it to the specified file pointer Drops \0 Programmer's responsibility: Make sure the newline is present at the appropriate place(s) 21

Programming in C T H E E N D 22