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

Similar documents
Dynamic Memory Allocation (and Multi-Dimensional Arrays)

Strings. Steven R. Bagley

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

Arrays and Pointers in C & C++

upper and lower case English letters: A-Z and a-z digits: 0-9 common punctuation symbols special non-printing characters: e.g newline and space.

C Characters and Strings

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

Linked Lists in C and C++

Array Initialization

ENEE 150: Intermediate Programming Concepts for Engineers Spring 2018 Handout #27. Midterm #2 Review

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

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

LECTURE 15. String I/O and cstring library

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

Unit 6 Files. putchar(ch); ch = getc (fp); //Reads single character from file and advances position to next character

Introduction to string

Chapter 8 - Characters and Strings

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

Accessing Files in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Other C materials before pointer Common library functions [Appendix of K&R] 2D array, string manipulations. <stdlib.

Structures, Unions, and Typedefs

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Object Oriented Programming COP3330 / CGS5409

The output: The address of i is 0xbf85416c. The address of main is 0x80483e4. arrays.c. 1 #include <stdio.h> 3 int main(int argc, char **argv) 4 {

211: Computer Architecture Summer 2016

C: How to Program. Week /May/28

Language Design COMS W4115. Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science

o Echo the input directly to the output o Put all lower-case letters in upper case o Put the first letter of each word in upper case

ch = argv[i][++j]; /* why does ++j but j++ does not? */

Kurt Schmidt. October 30, 2018

Your first C and C++ programs

Classes, Constructors, etc., in C++

Binary Trees (and Big O notation)

Characters and Strings

C-programming. Goal To give basic knowledge of the C language. Some previous experiences in programming are assumed. Litterature

Understanding Pointers

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

Pointers and File Handling

Work relative to other classes

Programming Assignment #4 Binary Trees in C++

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

Introduction to C. Sean Ogden. Cornell CS 4411, August 30, Geared toward programmers

CS 241 Data Organization Pointers and Arrays

File IO and command line input CSE 2451

Memory, Arrays & Pointers

CS 61c: Great Ideas in Computer Architecture

Language Design COMS W4115. Prof. Stephen A. Edwards Fall 2006 Columbia University Department of Computer Science

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

Strings and Stream I/O

Reminder. Sign up for ee209 mailing list. Precept. If you haven t received any from ee209 yet Follow the link from our class homepage

Introduction to C Language

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

Introduction to C. Ayush Dubey. Cornell CS 4411, August 31, Geared toward programmers

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Introduction to C. Zhiyuan Teo. Cornell CS 4411, August 26, Geared toward programmers

Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts

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

Chapter 16. Pointers and Arrays. Address vs. Value. Another Need for Addresses

by Pearson Education, Inc. All Rights Reserved.

Lecture 05 Pointers ctd..

PRINCIPLES OF OPERATING SYSTEMS

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Mid-term Exam. Fall Semester 2017 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

CS 0449 Sample Midterm

Arrays and Pointers. CSE 2031 Fall November 11, 2013

today cs3157-fall2002-sklar-lect05 1

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

ECE 15B COMPUTER ORGANIZATION

Tutorial 1 C Tutorial: Pointers, Strings, Exec

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

CSE 303: Concepts and Tools for Software Development

CS1003: Intro to CS, Summer 2008

C mini reference. 5 Binary numbers 12

EL2310 Scientific Programming

Princeton University Computer Science 217: Introduction to Programming Systems. Goals of this Lecture. A Taste of C. Agenda.

8. Characters, Strings and Files

(13-2) Dynamic Data Structures I H&K Chapter 13. Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

Discussion 3 Richard Guo Advanced C 01/28/09

C PROGRAMMING. Characters and Strings File Processing Exercise

C programming basics T3-1 -

Mid-term Exam. Fall Semester 2017 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

CS201 Some Important Definitions

EL2310 Scientific Programming

Today s Learning Objectives

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

C Libraries. Bart Childs Complementary to the text(s)

C Programming Language Review and Dissection III

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

Common Misunderstandings from Exam 1 Material

Goals of this Lecture

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Derived Classes in C++

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment.

C Introduction. Comparison w/ Java, Memory Model, and Pointers

THE C STANDARD LIBRARY & MAKING YOUR OWN LIBRARY. ISA 563: Fundamentals of Systems Programming

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

Chapter 8 C Characters and Strings

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

Transcription:

Strings in C Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, Absolute C++, by Walter Savitch, The C++ Programming Language, Special Edition, by Bjarne Stroustrup, and from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel) 1

Reading Assignment Kernighan & Ritchie, Chapter 5 All the way through! Study 5.5 in detail pages 104-107 Character pointer and functions You will use these all the time! Study 5.10 pages 114-118 Command line arguments You will use these a lot! Applicable also to C++ (Skip ahead) 2

Review Array a set of elements all of the same type stored contiguously in memory e.g., int A[25]; // 25 integers struct Str B[15]; /* 15 objects of type struct Str */ double C[]; /* indeterminate # of doubles */ Pointer a variable whose value is the location of some other object float *p; // pointer to a float 3

Review (continued) int A[25]; // 25 integers Type of A[i] is int (for all i). Type of A is int * const I.e., constant pointer to int 4

Summary Arrays and pointers are closely related Let int A[25]; int *p; int i, j; Let p = A; Then p points to A[0] p + i points to A[i] &A[j] == p+j *(p+j) is the same as A[j] 5

Summary (continued) If void f(int A[], int arraysize); Then f(&b[i], bsize-i) calls f with subarray of B as argument Starting at element i, continuing for bsize-i elements 6

Review (concluded) void f(int A[], int arraysize); and void f(int *A, int arraysize); are identical! Most C programmers use pointer notation rather than array notation In these kinds of situations 7

Additional Notes A pointer is not an integer Not necessarily the same size May not be assigned to each other except for value of zero! Called NULL in C; defined in <stdio.h> Means pointer to nowhere void * is a pointer to no type at all May be assigned to any pointer type Any pointer type may be assigned to void * CS-2303, A-Term 2012 Strings in C 8

Questions? 10

Characters in C Worcester Polytechnic Carnegie Institute Mellon char is a one-byte data type capable of holding a character (mostly printable) Treated as an arithmetic integer type (Usually) unsigned May be used in arithmetic expressions Add, subtract, multiply, divide, etc. Character constants C99 & C++ introduce a new data type called wchar for international text 'a', 'b', 'c', 'z', '0', '1', '9', '+', '-', '=', '!', '~', etc, '\n', '\t', '\0', etc. A-Z, a-z, 0-9 are in order, so that arithmetic can be done 11

Strings in C Definition: A string is a character array ending in the null character '\0' i.e., char s[256]; char t[] = "This is an initialized string!"; char *u = "This is another string!"; String constants are in double quotes "like this" May contain any characters Including \" and \' see p. 38, 193 of K&R String constants may not span lines in code However, they may be concatenated e.g., "Hello, " "World!\n" is the same as "Hello, World!\n" 12

Strings in C (continued) Let char *u = "This is another string!"; Then u[0] == 'T' u[1] == 'h' u[2] == 'i' u[20] == n' u[21] == 'g' u[22] == '!' u[23] == '\0' 13

Support for Strings in C Most string manipulation is done through functions in <string.h> String functions depend upon final '\0' So you don t have to count the characters! Examples: int strlen(char *s) returns length of string Excluding final '\0' char* strcpy(char *s, char *ct) Copies string ct to string s, return s s must be big enough to hold contents of ct ct may be smaller than s 14

Additional String Functions int strcmp(char *s, char *t) lexically compares s and t, returns <0 if s < t, >0 if s > t, and zero if s and t are identical K&R p. 106 for exact details expressed in code char* strcat(char *s, char *ct) Concatenates string ct to onto end of string s, returns s s must be big enough to hold contents of both strings! Other string functions strchr(), strrchr(), strspn(), strcspn() strpbrk(), strstr(), strtok(), See K&R pp. 105-106 for various implementations B.3 for complete list and specifications 15

Character functions in C See <ctype.h> These return either 0 (i.e., false) or 1 (i.e., true) int isdigit(int c) int isalpha(int c) int isalnum(int c) int isxdigit(int c) int islower(int c) int isupper(int c) int isspace(int c) int iscntrl(int c) int ispunct(int c) int isprint(int c) int isgraph(int c) These change case (if appropriate) and return characters int toupper(int c) int tolower(int c) 16

String Conversion Functions in C See <stdlib.h> K&R p. 251-252 double atof(const char *s) int atoi(const char *s) long atol(const char *s) double strtod(const char *s, char **endp) long strtol(const char *s, char **endp, int base) unsigned long strtoul(const char *s, char **endp, int base) 17

Dilemma Question: If strings are arrays of characters, and if arrays cannot be returned from functions, how can we manipulate variable length strings and pass them around our programs? Answer: Use storage allocated in The Heap! 18

Dynamic Data Allocation 0xFFFFFFFF stack (dynamically allocated) SP address space 0x00000000 heap (dynamically allocated) global and static data program code (text) PC CS-2303, A-Term 2012 Arrays and Pointers 19

Typical Usage with Strings char *gettextfromsomewhere( ); gettextfromsomewhere() cannot return pointer to automatic string variable within its own scope! int main(){ char *txt; ; txt = gettextfromsomewhere( ); ; printf("the text returned is %s.", txt); free(txt); } Why not? 20

Typical Usage (continued) char *gettextfromsomewhere ( ) { char *t;... t = malloc(stringlength);... /* fill in string t*/ return t; } Don t forget to free() the returned string! int main(){ char * txt; ; txt = gettextfromsomewhere ( ); ; printf("the text returned is %s.", txt); free(txt); } 21

Questions? 22

String Manipulation in C Almost all C programs that manipulate text do so with malloc ed and free d memory No limit on size of string in C You need to be aware of sizes of character arrays! You need to remember to free storage when it is no longer needed Before forgetting pointer to that storage! 23

Input-Output Functions printf(const char *format,...) Format string may contain %s inserts a string argument (i.e., char *) up to trailing '\0' scanf(const char *format,...) Format string may contain %s scans a string into argument (i.e., char *) up to next white space Adds '\0' Related functions fprintf(), fscanf() to/from a file sprintf(), sscanf() to/from a string 24

Hazard with scanf() char word[20]; ; scanf("%s", word); scanf will continue to scan characters from input until a space, tab, new-line, or EOF is detected An unbounded amount of input May overflow allocated character array Probable corruption of data! scanf adds trailing '\0' Solution: scanf("%19s", word); 25

Questions? 26

Command Line Arguments See 5.10 By convention, main takes two arguments:- int main(int argc, char *argv[]); argc is number of arguments argv[0] is string name of program itself argv[i] is argument i in string form i.e., i < argc argv[argc] contains a null pointer! Sometimes you will see (the equivalent) int main(int argc, char **argv); 27

Example PA #2 Instead of prompting user for game parameters, simply take them from command line % life 100 50 1000 would play the Game of Life on a grid of 100 50 squares for 1000 generations. 28

Example PA #2 (continued) int main(int argc, char *argv[])){ int xsize, ysize, gens, j; char grid[][]; if (argc <= 1) { printf("input parameters:- "); scanf("%d%d%d", &xsize, &ysize, &gens); } else { } xsize = atoi(argv[1]); ysize = atoi(argv[2]); gens = atoi(argv[3]); grid = calloc(xsize, sizeof (char *); for (j = 0; j < xsize; j++) grid[j] = calloc(ysize, sizeof (char)); /* rest of program using grid[i][j] */ for (j = 0; j < xsize; j++) free(grid[j]; free(grid); return 0; } // main(argc, argv) 29

Example PA #2 (continued) int main(int argc, char *argv[])){ int xsize, ysize, gens, j; char grid[][]; if (argc <= 1) { printf("input parameters:- "); scanf("%d%d%d", &xsize, &ysize, &gens); } else { } xsize = atoi(argv[1]); ysize = atoi(argv[2]); gens = atoi(argv[3]); grid = calloc(xsize, sizeof (char *); for (j = 0; j < xsize; j++) grid[j] = calloc(ysize, sizeof (char)); /* rest of program using grid[i][j] */ for (j = 0; j < xsize; j++) free(grid[j]; Convert free(grid); argument #1 to Convert int for argument xsize #2 to return 0; Convert int for argument ysize #3 to } int // for main(argc, gens argv) 30

Questions? 31