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

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

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

Array Initialization

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

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

8. Characters, Strings and Files

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

Computer Programming: Skills & Concepts (CP) Strings

Introduction to string

Computers Programming Course 11. Iulian Năstac

ONE DIMENSIONAL ARRAYS

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

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

C mini reference. 5 Binary numbers 12

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

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

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

Pointers, Arrays, and Strings. CS449 Spring 2016

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

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

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

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

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

Chapter 8 - Characters and Strings

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

C: How to Program. Week /May/28

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

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

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

ARRAYS(II Unit Part II)

Chapter 5. Section 5.1 Introduction to Strings. CS 50 Hathairat Rattanasook

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

CS107 Spring 2019, Lecture 4 C Strings

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

C Characters and Strings

CS107, Lecture 4 C Strings

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

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

Arrays, Strings, & Pointers


Week 5. Muhao Chen.

Characters and Strings

CS1100 Introduction to Programming

Chapter 8 Character Arrays and Strings

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

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

For instance, we can declare an array of five ints like this: int numbers[5];

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

Course organization. Course introduction ( Week 1)

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

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

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

C-String Library Functions

Strings. Steven R. Bagley

Procedural Programming

Create a Program in C (Last Class)

CSE2301. Arrays. Arrays. Arrays and Pointers

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

C Programming. Unit 9. Manipulating Strings File Processing.

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

C Programming Language Review and Dissection III

Programming for Engineers Arrays

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

COP 3223 Introduction to Programming with C - Study Union - Spring 2018

Strings. Part III. 1) Introduction. 2) Definition. 3) Using Strings. 4) Input/Output. 5) Using string.h Library. 6) Strings as Function Parameters

Scientific Programming in C V. Strings

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

Lecture 17. Strings II. CptS 121 Summer 2016 Armen Abnousi

Chapter 8 C Characters and Strings

C Programming Language

Arrays. Lecture 9 COP 3014 Fall October 16, 2017

DECLARAING AND INITIALIZING POINTERS

Arrays and Strings. CS449 Fall 2017

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

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

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

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

Fundamentals of Programming & Procedural Programming

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

This is CS50. Harvard University Fall Quiz 0 Answer Key

Computers Programming Course 12. Iulian Năstac

Computers Programming Course 10. Iulian Năstac

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

COMP1917: 09 Arrays and Strings

UNIT 6. STRUCTURED DATA TYPES PART 1: ARRAYS

CMPSC 311- Introduction to Systems Programming Module: Strings

Lecture 04 Introduction to pointers

COP 3223 Final Review

Arrays and Pointers. CSE 2031 Fall November 11, 2013

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

C Strings. Abdelghani Bellaachia, CSCI 1121 Page: 1

Lecture 9 Assertions and Error Handling CS240

BITG 1113: Array (Part 2) LECTURE 9

PES INSTITUTE OF TECHNOLOGY (BSC) I MCA, First IA Test, November 2015 Programming Using C (13MCA11) Solution Set Faculty: Jeny Jijo

Procedural Programming & Fundamentals of Programming

Copyright 2003 Pearson Education, Inc. Slide 1

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7

Chapter 10. Arrays and Strings

CSC 270 Survey of Programming Languages. C-String Values

Transcription:

CS 201 String Debzani Deb Strings(2) Two interpretations of String Arrays whose elements are characters. Pointer pointing to characters. Strings are always terminated with a NULL characters( \0 ). C needs this to be present in every string so it knows where the string ends. char a[] = hello\n ; char* b = hello\n ; a[0] *b h 104 a[1] *(b+1) e 101 a[2] *(b+2) l 108 a[3] *(b+3) l 108 a[4] *(b+4) o 111 a[5] a[6] *(b+5) *(b+6) \n 10 null 0 1 4 Characters Characters are small integers (0-255) Character constants are integers that represent corresponding characters. 0 48 A 65 \0 (NULL) 0 \t (TAB) 9 \n (newline) 10 SPACE 32 ASCII code maps integers to characters. String Constants We have already used string constants extensively in our earlier work: printf( Hello There ); Hello There is a string constant In C, string constants are identified by surrounding Note that this is different from characters they use single quotes Spaces are just treated like any other character A null character is automatically inserted after the constant You can also use #define directive to associate a symbolic name with a string constant. #define ERR ***ERROR*** 2 5 Strings(1) String a group of characters C implements the string data structure using arrays of type char. Two things to remember 1. All strings have a hidden extra character at the end \0, the null character. Hello is a string of length 6 2. Strings are managed by pointers H e l l o \0 char *s = Hello ; Creates a pointer s such that S Initializing and Declaring String char* s_ptr = hello ; char s[] = { h, e, l, l, o, \0 ; We can leave out the dimension of the array, the compiler can compute it automatically based on the size of the string (including the terminating \0). String Declaration char s[30]; // declared as an array of 30 char char *s; // declared as a pointer to char Note that the array s actually holds 30 characters, but the string can only contain up to 29. Because the 30th character would be \0. 3 6 1

Input/Output with printf and scanf Both printf and scanf can handle string arguments as long as the placeholder %s is used in the format string: printf( %s\n, s); The printf function, like other standard library functions that take string arguments, depends on finding a null character in the character array to mark the end of the If printf were passed a character array that contained no \0, the function would continue to display as characters the content of memory locations following the array argument until it encountered a null character or until it attempted to access a memory cell that was not assigned to the program, causing a run-time error. String Copy (1) We typically use the assignment operator = to copy data into a variable. char c, s[10]; c = a ; s = Hello ; // Does not work, Why? Exception: we can use the assignment symbol in a declaration of a string variable with initialization. In all other cases, you must do s[0] = H s[1] = e s[2] = l // etc Or use string copy function. 7 10 Input/Output Cont. The approach scanf takes to string input is very similar to its processing of numeric input. When it scans a string, scanf skips leading whitespace characters such as blanks, newlines, and tabs. Starting with the first non-whitespace character, scanf copies the characters it encounters into successive memory cells of its character array argument. When it comes across a whitespace character, scanning stops, and scanf places the null character at the end of the string in its array argument. char s[10]; scanf( %s, s); // bad practice scanf( %9s, s); // Good practice, prevents overflowing s Notice that there is no &, this is because strings are arrays, and arrays are already pointers. 8 String copy (2) Function strcpy copies the string that is its second argument into its first argument. char *strcpy(char *dest, const char *source) Example: strcpy(s, Test String ); Overflowing is possible if destination is not long enough to hold source. Function strncpy copies upto n characters of the source string to the destination. strncpy(s, Test String, 4); //copies 4 characters of Test String If the first n characters do not contain the null character, then it is not copied into variable s and you must do a s[n] = \0 ; Good Practice: strncpy(dest, source, dest_len -1); dest[dest_len-1] = \0 ; 11 String Library Functions C provides functions to work with strings, these are located in the h file. Put #include <h> at the top of the program to use these. Look at Table 9.1 on page 441 to see what functions are provided. Note that all of these function expect the strings to be null terminated. 9 Figure 9.5 Execution of strncpy char *strcpy(char *dest, const char *source) Both strcpy and strncpy return pointer to the old value of destination. 12 2

String Length int strlen(char *s) Counts the number of characters before the null terminator. If the null terminator is the first character (empty string), it returns 0. String Concatenation Concatenation is taking two strings and then make them become one char* strcat(char *dest, char *src) Puts src at the end of dest, including src s null terminator. Puts the first character of src in place of dest s null terminator. Reurns pointer to the old value of dest. strncat does the same as strcat, but it only copies n characters from the second string strncat(s1, s2,3); would copy only 3 character from s2. Puts a null terminator at the end of dest when it is done copying. In both cases, overflowing is possible and as a result we may overwrite other variables or get a run-time error. 13 16 Substring Extraction We frequently need to reference a substring of a longer For example we might want to examine the Cl in the string NaCl. We can use strncpy to copy a middle or an ending portion of a strncpy(dest, &source[2], 2); This goes to the 3rd spot in the array source, gets the memory location, and then copies 2 elements starting at this new memory location. Function strstr finds the first occurrence of one string inside another. char* strstr(char *s_in, char *s_for) Returns a pointer to the first location in s_in where s_for appears. Returns NULL if not found. Case sensitive. 14 Array of Pointers (1) Arrays can be of any type, including pointers. char *suit[4] = { Hearts, Diamonds, Clubs, Spade ; Each element of array suit is an address of a character string and there are 4 such elements in it. Suit[0][0] refers to H Suit[3][4] refers to e in Spade Suit[2][20] is an illegal access. Suit[0] refers to the address of Hearts 17 String Comparison Will string_1 < string_2 work? Wrong! they are only pointers int strcmp(char *s1, char *s2) Compares two strings lexicographically (based on ASCII code). Returns 0 if they are same Returns negative if s1< s2 Returns positive if s1> s2 int strncmp(char *s1, char *s2, int n) Compares two strings upto n characters. How to compare and swap two strings: if (strcmp(s1, s2) > 0){ strcpy(tmp, s1); strcpy(s1, s2); strcpy(s2, tmp); 15 Arrays of pointers are more flexible and faster to move around than arrays of data. More efficient use of memory No need to allocate memory in advance Applications Alphabetical sorting of words Memory management by operating system. Array of Pointers (2) Example: int *W[6]; int **PP = W; PP W and PP are pointer to pointer to int W[5] W[4] W[3] W[2] W[1] W[0] W[i] and *PP are pointer to int *W[i] and **PP are int 18 3

Ragged Arrays (1) An array of pointers whose elements point to arrays of different sizes is called a ragged array. A group of words may be stored as A two dimensional array of type char (array of strings), or A one dimensional array of pointers to char. Example: #include <stdio.h> int main(void){ char a[2][9] = { abc:, an apple ; char *p[2] = { abc:, an apple ; printf( %c%c%c %s %s\n, a[0][0], a[0][1], a[0][2], a[0], a[1]); printf( %c%c%c %s %s\n, p[0][0], p[0][1], p[0][2], p[0], p[1]); return 0; Example: sortwords in your textbook 19 22 char a[2][9] = { abc:, an apple ; a is a 2D array, and its declaration allocates space for 2x9 = 18 chars, only the first five elements a, b, c, : and \0 are used in a[0]; the rest are initialized to zero. Note that a[0][8] is a valid Reference, but p[0][8] is not a b c : \0 a n a p p l e \0 Ragged Arrays (2) char *p[2] = { abc:, an apple ; p is a 1D array of pointers to char, When it is declared, space for two pointers are allocated. p[0] is initialized to point at abc:, a string requiring space for 5 chars. p[1] is initialized to point at an apple which requires space for 9 chars. Thus p does its job in less space than a; it is also faster, as no mapping function is generated a b c : \0 a n a p p l e \0 20 Example: sortwords(2) void sort_words (char *w[], int n) { /* n elements (words) to be sorted */ int i, j; for(i = 0; i < n; ++i) for(j = i+1; j < n; ++j) if ( strcmp(w[i], w[j]) > 0) swap ( &w[i], &w[j] ); void swap (char **p, char **q) { char * tmp; tmp = *p; *p = *q; *q = tmp; Compares each pair of string pointed to by addresses. If they are out of order swap the pointers. The arguments passed to the swap function are addresses of pointers to char or equivalently pointers to pointers to char. Hence they are declared as output parameters in the function swap. 23 Example: sortwords(1) Example: sortwords(3) Storage in memory Pointer assignment after sorting w The idea of the sorting algorithm is that, instead of copying and swapping the arrays of words, just the pointers are swapped in case words are out of order. We send the function sort_words the array w of pointers, and its size n. 21 w Reading through the words pointed to by the elements of w, we obtain the sorted list of words. Advantages Pointer requires less space Faster copy involving only pointers Original list is preserved. 24 4

Summary The two most important questions dealing with strings are the following: 1. Is there enough room to perform the given operation? 2. Does the created string end in \0? When we write our own string-building functions, we must be sure that a null character is inserted at the end of every This inclusion of the null character is automatic for constant The caller of a string function must provide space into which the called function can store its result. Proper behavior is entirely dependent on the availability of adequate space in the output parameter even though the called function does not require the user to provide this size as an input argument. 25 Remember I will be out of town in the coming week. On 2 nd and 4 th April classes will be taken by Shahriar. You can go to the lab, but there will be no grading and no TA will be there. I will ask the student consultants to be there to help you. There will be a quiz in either 2 nd or 4 th april. Will cover upto String. Do not get too comfortable with the LOOOOG lab deadlines. 48 hours deadlines are coming soon. I will discuss array (specially multidimensional) a little bit more once I get back. 26 5