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

Similar documents
CSC209H Lecture 4. Dan Zingaro. January 28, 2015

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

Computer Programming: Skills & Concepts (CP) Strings

Array Initialization

8. Characters, Strings and Files

Engineering Problem Solving with C++, Etter

Computers Programming Course 11. Iulian Năstac

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

Procedural Programming

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

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

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

Computers Programming Course 10. Iulian Năstac

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

LECTURE 15. String I/O and cstring library

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

Pointers, Arrays, and Strings. CS449 Spring 2016

Memory, Arrays & Pointers

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

Create a Program in C (Last Class)

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

Fundamentals of Programming & Procedural Programming

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

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


(9-3) Strings II H&K Chapter 8. Instructor - Andrew S. O Fallon CptS 121 (October 19, 2018) Washington State University

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

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

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

Object Oriented Programming COP3330 / CGS5409

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

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

Data Structures and Algorithms(4)

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

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

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

Procedural Programming & Fundamentals of Programming

ONE DIMENSIONAL ARRAYS

C Characters and Strings

Arrays and Strings. CS449 Fall 2017

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

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

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

CS1100 Introduction to Programming

EECS402 Lecture 08. Intro To The Standard string Class. Some string Functionality

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

Object-Oriented Programming

Week 5. Muhao Chen.

ARRAYS(II Unit Part II)

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

C Strings. Abdelghani Bellaachia, CSCI 1121 Page: 1

Scientific Programming in C V. Strings

Intermediate Programming, Spring 2017*

C Programming Language Review and Dissection III

Introduction to string

Characters and Strings

BITG 1113: Array (Part 2) LECTURE 9

UNIT 6. STRUCTURED DATA TYPES PART 1: ARRAYS

CSC 270 Survey of Programming Languages. C-String Values

Strings and Stream I/O

Pointers, Dynamic Data, and Reference Types

Exercise 1.1 Hello world

C-String Library Functions

1 Pointer Concepts. 1.1 Pointer Examples

CS106L Handout #05 Fall 2007 October 3, C Strings

Principles of C and Memory Management

Introduction to Algorithms and Data Structures. Lecture 6 - Stringing Along - Character and String Manipulation

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

For example, let s say we define an array of char of size six:

Fundamentals of Programming

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES II

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

Chapter 13. Strings. Introduction

CSC 2400: Computer Systems. Arrays and Strings in C

MORE STRINGS AND RECURSION

Character Array. C Programming. String. A sequence of characters The last character should be \0 that indicates the end of string 5 \0

Chapter 8 Character Arrays and Strings

CS 31 Discussion: Week 6. Taylor Caulfield

Arrays and Pointers. Overview. Arrays Introducing Pointers C-Style Character Strings Multidimensioned Arrays

Fundamental of Programming (C)

C programming language continued: Recursion, strings and pointer

Dynamic Allocation in C

C for C++ Programmers

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

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

CSC 2400: Computer Systems. Arrays and Strings in C

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

Slide 1 CS 170 Java Programming 1 More on Strings Duration: 00:00:47 Advance mode: Auto

Administrative Stuff. More on Strings, File I/O. Last Time. Java API

More on Strings, File I/O. September 8, 2016

DAY 3. CS3600, Northeastern University. Alan Mislove

Midterm Examination # 2 Wednesday, March 18, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

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

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

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

Strings. Steven R. Bagley

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

Kurt Schmidt. October 30, 2018

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

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

Transcription:

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 Language, Second Edition, Kernighan & Ritchie, Prentice Hall, 1988

Objectives Understand how C implements character strings Look at a few string functions from the C standard library (caller's view) Illustrate string processing algorithms by reimplementing some of the standard library functions

String Types Unlike C++, Java and Python, C does not have a named string type C++: type string Java: type String Python: type str C strings are implemented using arrays of characters

String Constants A sequence of characters in double quotes is a string constant or string literal Example: "SYSC" Stored as an array of characters, terminated by '\0' (the null character) C compiler creates the array and initializes it

String Constants Note 1: '\0' is not the same as '0' (the character zero) Note 2: number of elements in the array is 1 more than the number of chars between the quotes Adjacent string constants are concatenated at compile time "Hello, " "world!" and "Hello, world!" are equivalent

String Variables This declaration: char dept[] = "SYSC"; allocates an array called dept, initialized with 5 chars: 'S', 'Y', 'S', 'C', '\0'

String Variables char dept[] = "SYSC"; is equivalent to: char dept[5]; dept[0] = 'S'; dept[1] = 'Y'; dept[2] = 'S'; dept[3] = 'C'; dept[4] = '\0';

String Variables We don't need to initialize all the elements in a character array char dept[5]; dept[0] = 'I'; dept[1] = 'M'; dept[2] = 'D'; dept[3] = '\0'; dept[4] is uninitialized (that's o.k., because the string is properly terminated with '\0')

String Variables Can't assign a string literal to a character array This isn't permitted: char dept[5];... dept = "SYSC"; // Error!

String Variables const qualifier tells the compiler that the array elements should never be altered (compiler should flag any attempt to do so) const char dept[] = "SYSC";

String Operations C's operators are not overloaded to support string operations Example: in C++, Java and Python, + is the string concatenation operator In C, + cannot be used to concatenate two character strings

<string.h> C standard library provides several functions that provide common string operations Prototypes are found in <string.h>

strlen int strlen(const char s[]); Returns the length of its character string argument, excluding '\0' #include <string.h>... char greeting[] = "Hello"; int len; len = strlen(greeting); // returns 5 (not 6)

strcmp int strcmp(const char s[], const char t[]); Returns negative value if s < t 0 if s == t positive value if s > t

strcmp Example: char name1[30]; char name2[30]; // Initialization of name1 and name2 // not shown if (strcmp(name1, name2)!= 0) { // strings are different }

strstr char *strstr(const char s[], const char t[]); Returns the location of substring t in string s as a character pointer (we'll study pointers later) If substring t isn't found in s, returns the value NULL NULL is defined in several header files If all you need to know is whether or not t is in s, but you don't care where, just check if the function returns NULL

strstr Example: char phrase[] = "quick brown fox jumped"; if (strstr(phrase, "fox") == NULL) { printf("fox is not in the string"); } else { printf("fox is in the string"); } Output is: fox is in the string

strcpy char *strcpy(char s[], const char t[]); ignore char * return type for now Copies all chars in t to s, including '\0' Programmer is responsible for ensuring that s is big enough to hold all chars copied from t

strcat char *strcat(char s[], const char t[]); ignore char * return type for now Concatenates t to end of s, including '\0' Programmer is responsible for ensuring that s is big enough to hold all chars copied from t

A strlen Implementation Loop over the string, counting characters until we reach null int CU_strlen(const char s[]) { int i = 0; while (s[i]!= '\0') { i = i + 1; } return i; }

A strcmp Implementation Loop, comparing the two strings on a characterby-character basis, until we find two characters that differ Calculate the difference of those two characters to determine if 1 string is > or < than the other If while looping we reach the end of both strings before finding chars that differ, the two strings are equal

A strcmp Implementation int CU_strcmp(const char s[], const char t[]) { int i; for (i = 0; s[i] == t[i]; i = i + 1) { if (s[i] == '\0') return 0; } // i is first pos'n where s and t differ return s[i] - t[i]; }

A strcpy Implementation Loop over the source string, copying chracters into the destination string, until we reach the end of the source string Null terminate the destination string

A strcpy Implementation void CU_strcpy(char s[], const char t[]) { int i = 0; while (t[i]!= '\0') { s[i] = t[i]; i = i + 1; } // Terminate s s[i] = '\0'; }

A strcat Implementation Loop over the destination string, until we find null Loop over the source string, copying chracters into the destination string, until we reach the end of the source string 1st character copied from source overwrites null in destination Null terminate the destination string

A strcat Implementation void CU_strcat(char s[], const char t[]) { int i, j; for (i = 0; s[i]!= '\0'; i = i + 1) ; // find end of s // Copy t to s, except for null for (j = 0; t[j]!= '\0'; j = j + 1) { s[i] = t[j]; i = i + 1; } s[i] = '\0'; // Terminate s }