String constants. /* Demo: string constant */ #include <stdio.h> int main() {

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


Computer Programming: Skills & Concepts (CP) Strings

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

CS 33. Introduction to C. Part 5. CS33 Intro to Computer Systems V 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

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

Sums. Here's a (poor) program to add three numbers

This is CS50. Harvard University Fall Quiz 0 Answer Key

CSE 230 Intermediate Programming in C and C++ Arrays, Pointers and Strings

Intermediate Programming, Spring 2017*

CS 137 Part 6. ASCII, Characters, Strings and Unicode. November 3rd, 2017

IO = Input & Output 2

C Characters and Strings

Computers Programming Course 10. Iulian Năstac

Programs in memory. The layout of memory is roughly:

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

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

Arrays, Strings, & Pointers

arrays and strings week 3 Ritsumeikan University College of Information Science and Engineering Ian Piumarta 1 / 22 imperative programming review

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

Arithmetic Expressions in C

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

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

CYSE 411/AIT681 Secure Software Engineering Topic #12. Secure Coding: Formatted Output

2/9/18. CYSE 411/AIT681 Secure Software Engineering. Readings. Secure Coding. This lecture: String management Pointer Subterfuge

8. Characters, Strings and Files

Problem 2 Add the two 2 s complement signed 8-bit values given below, and express your answer in decimal.

Pointers, Arrays, and Strings. CS449 Spring 2016

Contents of Lecture 3

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

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

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

Fundamentals of Programming

1/25/2018. ECE 220: Computer Systems & Programming. Write Output Using printf. Use Backslash to Include Special ASCII Characters

CS 31 Discussion: Week 6. Taylor Caulfield

Lecture07: Strings, Variable Scope, Memory Model 4/8/2013

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

Computers Programming Course 5. Iulian Năstac

Fundamental of Programming (C)

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

CSC 2400: Computer Systems. Arrays and Strings in C

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

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

ONE DIMENSIONAL ARRAYS

CSC 2400: Computer Systems. Arrays and Strings in C

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

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7

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

Armide Documentation. Release Kyle Mayes

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

Midterm Exam Nov 8th, COMS W3157 Advanced Programming Columbia University Fall Instructor: Jae Woo Lee.

20 Dynamic allocation of memory: malloc and calloc

ARRAYS(II Unit Part II)

Slide Set 3. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

CS1100 Introduction to Programming

Should you know scanf and printf?

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)

Chapter 8 C Characters and Strings

Memory, Arrays & Pointers

Array Initialization

C programming basics T3-1 -

Arrays and Strings. CS449 Fall 2017

return return else return

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

Chapter 13. Strings. Introduction

Strings. Steven R. Bagley

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

CSE 303: Concepts and Tools for Software Development

M.CS201 Programming language

211: Computer Architecture Summer 2016

Computers Programming Course 11. Iulian Năstac

Pointers (continued), arrays and strings

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

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

C for C++ Programmers

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

Slide Set 3. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

by Pearson Education, Inc. All Rights Reserved.

Characters and Strings

Library Functions. General Questions

Programming for Engineers Arrays

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

Stream Model of I/O. Basic I/O in C

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

Computer Systems Programming. Practice Midterm. Name:

Midterm #2 With Solutions solutions in blue marking notes in red

Memory Corruption 101 From Primitives to Exploit

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

Pointers (continued), arrays and strings

Fundamentals of Programming & Procedural Programming

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

Arrays and Pointers in C. Alan L. Cox

THE FUNDAMENTAL DATA TYPES

Fundamentals of Programming. Lecture 11: C Characters and Strings

Here's how you declare a function that returns a pointer to a character:

COMP1917: 09 Arrays and Strings

Do not start the test until instructed to do so!

Transcription:

Strings 1

String constants 2 /* Demo: string constant */ #include <stdio.h> s1.c int main() { printf("hi\n"); } String constants are in double quotes A backslash \ is used to include 'special' characters, with \\ used to include a backslash, e.g. "H\\i" In printf only, % is used to print special values, with %% to print a percent sign, e.g. printf("100%%\n")

Strings as arrays 3... int main() { char hi[] = "Hi\n"; printf("%s", hi); } s2.c (hi already has a newline) A string is an array of characters - it can be initialised with a string constant, and printed with %s This version fails, though! char hi[3] = "Hi\n";

Marker character 4... int main() { char hi[] = {'H', 'i', '\n', '\0'}; printf("%s", hi); } s3.c The characters in a string must have a marker character \0 (= character with code 0 = null character) at the end This version works! char hi[4] = "Hi\n";

Marker not counted 5 /* Demo: length of string */ #include <stdio.h> #include <string.h> s4.c int main() { char hi[] = {'H', 'i', '\n', '\0'}; int n = strlen(hi); printf("%d\n", n); } This prints 3, not 4 So the \0 marker isn't counted in the length

First marker taken 6 /* Demo: length of string */ #include <stdio.h> #include <string.h> s5.c int main() { char hi[] = {'H','i','\n','\0','L','o','\n','\0'}; int n = strlen(hi); printf("%d\n", n); } This prints 3, not 7 or 8 So a string isn't an array of characters, a string is contained in an array of characters

Random length 7 /* Demo: random length of string */ #include <stdio.h> #include <string.h> s6.c int main() { char hi[] = {'H', 'i', '\n'}; int n = strlen(hi); printf("%d\n", n); } This prints 3, or 42, or a very big number, or crashes, depending on what is in memory after the array So a string is not valid or reliable without a \0 marker

Characters 8 A char is an ASCII character, i.e. one byte (0..127) It is just a small integer, e.g. 'a' is a synonym for the number 97, and '\0' is a synonym for the number 0 Note that '0' is a synonym for 48, not 0 Use digit-'0' to convert a digit into a number, and letter-'a'+'a' to convert a letter to upper case

International characters 9 Any character which is not in the American ASCII set is 'international': in English, that's,,... UTF-8 is the 'magic' you need (see aside: characters) Code printf("cost: 10...") works (with being two characters from C's point of view) This works if your editor and terminal window are set up properly

Historical notes 10 The marker idea (instead of a length at the beginning) was probably to save space when memories were tiny: it involves an 'expensive' loop to find the length, and it prevents strings from containing a \0 character strlen and many other functions have rubbish names because there used to be a 6-letter limit (now its 31) C has a 'rule' that anything built-in takes one processor instruction (ish) and anything longer needs a function, so strlen is a function because it needs a loop

Documentation 11 To find the documentation for the strlen function, type C strlen into Google With luck, you find something like this: #include <string.h>... size_t strlen(const char *str); Sometimes the library to use (in this case string) is not very explicit

Sizes 12 The declaration/signature/prototype for strlen is size_t strlen(const char *str); size_t is a synonym for a suitable integer type on your computer for holding sizes, probably long This does not work, because %d expects an int printf("%d\n", strlen(hi));

Coercion 13 This is recommended int n = strlen(hi); printf("%d\n", n); Technically, the definition of n involves a conversion ('coercion') from size_t to int You are telling the compiler that you know the length will be small enough to fit in an int

Const 14 The declaration for strlen is... strlen(const char *str); const means that the strlen function promises not to change the string that you give it

Char pointer 15 The declaration for strlen, leaving out const is... strlen(char *str); char *str means that the argument variable str has type char * which is 'pointer to character' For most purposes, C treats 'array of characters' and 'pointer to the first character' the same, so read this as:... strlen(char str[]);

Warning 16 In this unit, before getting to pointers, you can almost always define strings as character arrays (char s[]) However, in documentation, books, tutorials, blogs, on stack overflow, etc., pointer notation is used (char *s) We will clear this up when we get to pointers There is one place where you have to use pointer notation, not array notation, even now, that is when reading command line arguments in the main function

Main 17... args.c int main(int n, char *args[n]) { for (int i=0; i<n; i++) { printf("arg %d is %s\n", i, args[i]); } } Run this with./args,./args a,./args a b The args array holds the words you typed on the command line, with args[0] being the program name, probably expanded into a full file path The explanation will come in a later chapter

Returning strings 18 For the time being, you can't write functions which return strings That's because we are using array notation, and C doesn't allow raw arrays to be returned from functions Later, using pointer notation, we will effectively be able to return strings and arrays from functions

Comparing strings badly 19 Try this:... int main() { char s1[] = "x"; char s2[] = "x"; if (s1 == s2) printf("same\n"); else printf("different\n"); } compare.c This prints out Different, because == means 'identical' not 'same contents' Comparing contents involves a loop, so it's a function

Comparing strings well 20 This works:... compare2.c int main() { char s1[] = "x"; char s2[] = "x"; if (strcmp(s1,s2) == 0) printf("same\n"); else printf("different\n"); } strcmp returns 0 if the strings are the same, or a negative or positive number if the first is earlier or later in lexicographic order than the second

Copying a string 21 /* Demo: copy a string */ #include <stdio.h> #include <string.h> copy.c int main() { char s1[] = "Hi\n"; char s2[4]; strcpy(s2, s1); printf("%s", s2); } strcpy(to,from) copies a string from one array to another

Joining strings 22... int main() { char s1[] = "Hi\n"; char s2[] = "Lo\n"; char s3[7]; strcpy(s3, s1); strcat(s3, s2); printf("%s", s3); } join.c strcat(to,from) joins from onto the end of to The length of s3 could be calculated: char s3[strlen(s1) + strlen(s2) + 1];

Printing strings 23... sprint.c int main() { char s1[25]; sprintf(s1, "The square of %d is %d", 37, 37*37); printf("%s\n", s1); } sprintf(s,...) prints into s, with other arguments the same as printf You can use snprintf(null,0,...) (C11) to precalculate the length

Challenges 24 One of the main challenges of C, compared to other languages, is that it has: undefined behaviour unspecified behaviour implementation-defined behaviour locale-specific behaviour See appendix sections J.2, J.1, J.3, J.4 in the C11 standard for details

Undefined behaviour 25 The program is technically incorrect, but the platform (compiler plus run time system plus libraries etc.) may not detect it, and the result is unpredictable The program may do the right thing, or some random wrong thing, or crash Example: accessing a char past the end of a string

Unspecified behaviour 26 The program is technically incorrect, the standard says the platform has a limited choice of what to do, but the programmer has no official way of finding out what is chosen so must still avoid the situation Example: accessing a local variable before it has been initialised The standard says some value must be provided

Implementation-defined 27 The program is technically correct, the standard says the platform has a limited choice of what to do, and that choice is supposed to be documented Examples: handling of international UTF-8 text, whether stdout is buffered, whether char is signed or not, how many bits int and long have (beyond 16/32) Nobody knows where to find this documentation! So if you can't avoid these things, or test them in the program, you need to document the assumptions your program makes

Locale-specific 28 The program is technically correct, but the behaviour depends on where in the world you are Examples: the default human language, money, timezones The platform is required to provide information about the locale to your program But the user may need to set up the environment properly for this to work, so you need to provide user documentation