Strings CS240 Dick Steflik. What is a string A null terminated array of characters: char thisIsAString[10]; 0 1 2 3 4 5 6 7 8 9 \0 The “\0” (null character)

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
 A string is an array of characters.  Strings must have a 0 or null character after the last character to show where the string ends.  The null character.
Strings string.h library. String Library Functions Dr. Sadık EşmelioğluCENG 1142 NameDescription strlen return the length of string not counting \0 strcopy.
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 09 Strings, IDEs. METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 29, 2002.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Character String Manipulation. Overview Character string functions sscanf() function snprintf() function.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
Lecture 20 Arrays and Strings
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Chapter Fourteen Strings Revisited. Strings A string is an array of characters A string is a pointer to a sequence of characters A string is a complete.
Pointer, malloc and realloc 1. Name entered was 6 char, not enough space to put null terminator 2 Array of char.
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
Buffer Overflow Exploits CS-480b Dick Steflik. What is a buffer overflow? Memory global static heap malloc( ), new Stack non-static local variabled value.
Chapter 8 Characters and Strings Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 C-strings String = null-terminated array of characters The null character ('\0') specifies where the string terminates in memory. Example: The string.
C-Strings A C-string (also called a character string) is a sequence of contiguous characters in memory terminated by the NUL character '\0'. C-strings.
C strings (Reek, Ch. 9) 1CS 3090: Safety Critical Programming in C.
CSSE221: Software Dev. Honors Day 28 Announcements Announcements Simulation grades coming back Simulation grades coming back All C Projects due Friday.
Strings String - a string is a series of characters treated as a unit. A C string is a variable-length array of characters that is delimited by the null.
C-strings Array with base type char One character per indexed variable
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
Introduction to C programming
While Loop Structure while (condition) { …. // This line will process when while condition is true }
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
Array with base type char One character per indexed variable One extra character: '\0' Called ‘null character’ Delimiter of the string To declare a string,
CS 162 Introduction to Computer Science Chapter 17 C++ String Objects Herbert G. Mayer, PSU (Copied from Prof. Phillip Wong at PSU) Status 11/30/2014.
Strings in C are my friend, and they can be yours, too. An inspiring and epic tale by Erik Speyer, John Sullivan, and Dane Bennington.
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
Chapter 8 Characters and Strings Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
Chapter 8: Character and String CMPD144: Programming 1.
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
13. Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal.
 2003 Prentice Hall, Inc. All rights reserved. 5.11Function Pointers Pointers to functions –Contain address of function –Similar to how array name is.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
String Array (Multidimensional Arrays) 1. A string array is a multidimensional array of strings. It is declared in the following syntax: char variable_name[No_of_strings][size_of_each_string];
Charles Clute Tom Most Michael Hein. Strings in C  There is no String... But there’s hope! Strings are character arrays char volume[6]; char volume[6]
5.6 String Processing Part 2. Sprintf(destnvar,…..regularprintf) Write formatted data to string Same as printf except the output is put in variable. A.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Chapter 9 Strings J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of Technology.
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal may.
CSE 251 Dr. Charles B. Owen Programming in C1 Strings and File I/O.
ECE 103 Engineering Programming Chapter 29 C Strings, Part 2 Herbert G. Mayer, PSU CS Status 7/30/2014 Initial content copied verbatim from ECE 103 material.
13. Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal.
1 C Basics. 2 The C Language Spirit Made by professional programmers for professional programmers Very flexible, very efficient, very liberal Does not.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
C Characters and Strings
Strings CSCI 112: Programming in C.
C Characters and Strings
Fundamentals of Characters and Strings
CSE 303 Lecture 14 Strings in C
C-strings In general, a string is a series of characters treated as a unit. Practically all string implementations treat a string as a variable-length.
Buffer Overflows.
C++ Programming Lecture 20 Strings
Presentation transcript:

Strings CS240 Dick Steflik

What is a string A null terminated array of characters: char thisIsAString[10]; \0 The “\0” (null character) in the 0 th position indicates the string is empty Remember that the name of an array is a pointer to it's first element; thisIsAString is the same as s where: char *s = thisIsAString;

scanf scanf is used for reading data from stdin according to some format specifier. For strings the format specifier is %s, scanf will read characters from stdin, up to the first blank and assign the string to the specified variable. scanf(“%s”,s); printf(“%d %s \n”,strlen(s),s); given the input from stdin “abc def ghi” the output will be: 3 abc The format specifier can have a modifier such as %12s, this indicated that the maximum number of characters to be read are 12, given the prev example the output will be the same, the read stops at the first blank

gets To read “whitespace” gets(b); printf("%d %s\n",strlen(b),b); given the input: “a b c d” the output will be: 7 a b c d Problem with gets is there is nothing to tell it when to stop, the same code above with the input: a b c d e f g h i j k l m will give the output: 25 a b c d e f g h i j k l m it does this by overflowing the array reserved for b and overwriting whatever happens to be defined next in memory. This is a classic “buffer overflow” Even though gets is useful it is “unsafe” and should be avoided

fgets fgets(targetstring,maxlength,filepointer) char b[5]; fgets(b,5,stdin); printf(“%d %s\n”,strlen(b),b); given the input: aaaaaaaaaaa the output is: 4 aaaa Successive fgets calls will keep reading from stdin as long as there is input: char a[10], b[10]; fgets(b,10,stdin); printf("%d %s\n",strlen(b),b); fgets(a,10,stdin); printf("%d %s\n",strlen(a),a); given the input: aaaaaaaaaabbbbbbbbbbcccccccccc the output is: 9 aaaaaaaaa 9 abbbbbbbb

strcpy char * strcpy(char * s1, char * s2); copies s2 into the char array s1, s1 is returned If s2 is longer than s1 then s1 will be overflowed, indicating that you must be careful using strcpy as it is unsafe; instead use strncpy. strncpy(dst, src, dst_size-1); dst[dst_size -1] = ‘\0’; /* just to be safe */ -or- /* allocate the destination buffer when you need it */ dst = (char *)malloc(strlen(src) +1); srtcpy(dst, src)

strcat (string concatenation) char * strcat(char * s1, char * s2) append string s2 onto the end of s1. Same problem as strcpy; if s2 is longer than the array defined for s1, the resulting string will overflow s1 and write over whatever happens to be defined in memory after s1. Better to use strncat as: strncat(dst, src, dst_size - strlen(dst) -1);

strchr & strrchr (search for a character) char * strchr(char * s, char c) search s for c return a pointer to first occurance, return null if not found. char * strrchr(char * s, char c) search s for c return a pointer to last occurance, return null if not found.

strcmp & strncmp (comparing strings) int strcmp(char * s1, char * s2) // compares s1 and s2 returns : less than 0 if s1 is lexically less than s2 0 if s1 and s2 are lexically equal greater than 1 if s1 is lexically greater than s2 int strncmp(char * s1, char * s2, int n) // compares first n characters of s1 and s2

strlen Int strlen(char * s1) returns the length of the specified character string

strcasecmp & strncasecmp Case insensitive versions of strcmp and strncmp; same return values as strcmp and strncmp

strstr char * strstr(char * s1, char * s2) find first occurrence of s2 in s1, return pointer to the occurrence strpbrk char * strpbrk(char * s1, char * s2) find the first occurrence of any character in s2 in s1, return a pointer to the occurrence or a null if no character in s2 is found in s1

strspn int strspn(char * s1, char * s2) returns the number of characters at the beginning of s1 that match s2 strcspn int strcspn(char * s1, char * s2) returns the number of characters at the beginning of s1 that do not match s2

strtok (string tokenizer) char * strtok(char * s1, char * s2) repeated calls to strtok breakes the string s1 into tokens that are seperated by the characters in s2), returns null if no more tokens. Each returned token will ne null (\0) terminated