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.

Slides:



Advertisements
Similar presentations
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.
Advertisements

 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.
Ch 8. Characters and Strings Timothy Budd 2 Characters and Literals Strings Char in C++ is normally an 8-bit quantity, whereas in Java it is a 16-bit.
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.
Strings CS240 Dick Steflik. What is a string A null terminated array of characters: char thisIsAString[10]; \0 The “\0” (null character)
 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.
Array_strcpy void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; }
Pointers in C Rohit Khokher
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
CS 241 Section Week #2 2/4/10. 2 Topics This Section MP1 overview Part1: Pointer manipulation Part2: Basic dictionary structure implementation Review.
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.
CS Nov 2006 C-strings.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
C-strings Array with base type char One character per indexed variable
C-Strings Joe Meehean. C-style Strings String literals (e.g., “foo”) in C++ are stored as const char[] C-style strings characters (e.g., ‘f’) are stored.
 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
Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
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.
APS105 Strings. C String storage We have used strings in printf format strings –Ex: printf(“Hello world\n”); “Hello world\n” is a string (of characters)
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.
Chapter 8: Character and String CMPD144: Programming 1.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve.
 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. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
C String, Proper Type, and String Objects Andy Wang COP 4530: Data Structures, Algorithms, and Generic Programming.
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)
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Characters and Strings Functions.
Strings, Pointers and Tools
Built-in Functions for NTCAs. strlen char array[10] = “Hello”; int length = strlen(array); cout
Principles of Programming Chapter 8: Character & String  In this chapter, you’ll learn about;  Fundamentals of Strings and Characters  The difference.
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.
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.
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.
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
C Characters and Strings
Lecture 8 String 1. Concept of strings String and pointers
CSE 303 Lecture 14 Strings in C
Object Oriented Programming COP3330 / CGS5409
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.
C++ Pointers and Strings
C Characters and Strings
C++ Pointers and Strings
Presentation transcript:

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 entity

Strings as Arrays A string is an array of characters terminated by the null character ‘\0’ H e l l o \0

An Example int findFirstVowel(char word[]) { int i; for (i = 0; word[i] != ‘\0’; i++) { if (isVowel(word[i])) return i; } return -1; }

An Example int findFirstVowel(char *word) { char *cp; for (cp = word; *cp != ‘\0’; cp++) { if (isVowel(*cp)) return cp - word; } return -1; }

An Example int findFirstVowel(string word) { int i; for (i = 0; i < stringLength(word); i++) { if (isVowel(ithChar(word, i))) return i; } return -1; }

Common Pitfalls main() { char carray[6]; char *cptr; carray = “hello”; /* error: array name has no lvalue */ cptr = “hello”; strcpy(carray, “hello”); strcpy(cptr, “hello”); /* error: no memory spaces */ }

Common Pitfalls char *charToString(char ch) { char *cptr; cptr = (char *) malloc(2); cptr[0] = ch; cptr[1] = ‘\0’; return cptr; } char *charToString(char ch) { char carray[2]; carray[0] = ch; carray[1] = ‘\0’; return carray; /* error */ }

ANSI String Library strcpy(dst, src): copy characters from src to dst strncpy(dst, src, n): copy at most n characters from src to dst strcat(dst, src): append characters from src to the end of dst strncat(dst, src, n): append at most n characters from src to the end of dst strlen(s): return the length of s

ANSI String Library strcmp(s1, s2): return an integer indicating the result of the comparison strncmp(s1, s2, n): like strcmp but compare at most n characters strchr(s, ch): return a pointer to the first instance of ch in s (or NULL) strrchr(s, ch): return a pointer to the last instance of ch in s (or NULL) strstr(s1, s2): return a pointer to the first instance of s2 in s1 (or NULL)

strcpy void strcpy(char *dst, char *src) { while (*dst++ = *src++) ; } void strcpy(char dst[], char src[]) { int i; for (i = 0; src[i] != ‘\0’; i++) { dst[i] = src[i]; } dst[i] = ‘\0’; }

strncpy void strncpy(char dst[], char src[], int n) { int i; for (i = 0; src[i] != ‘\0’ && i < n; i++) { dst[i] = src[i]; } for (; i < n; i++) { dst[i] = ‘\0’; }

strcat void strcat(char dst[], char src[]) { int i, j; for (i = 0; dst[i] != ‘\0’; i++) ; for (j = 0; src[j] != ‘\0’; j++, i++) { dst[i] = src[j]; } dst[i] = ‘\0’; }

strncat void strncat(char dst[], char src[], int n) { int i, j; for (i = 0; dst[i] != ‘\0’; i++) ; for (j = 0; src[j] != ‘\0’ && j < n; j++, i++) { dst[i] = src[j]; } for (; j < n; j++, i++) { dst[i] = ‘\0’; }

strlen int strlen(char str[]) { int i; for (i = 0; str[i] != ‘\0’; i++) ; return i; }

strcmp int strcmp(char s1[], char s2[]) { int i; for (i = 0; s1[i] != ‘\0’ && s2[i] != 0; i++) { if (s1[i] < s2[i]) return –1; if (s1[i] > s2[i]) return 1; } if (s1[i] < s2[i]) return –1; if (s1[i] > s2[i]) return 1; return 0; }

strncmp int strncmp(char s1[], char s2[], int n) { int i; for (i = 0; i < n && s1[i] != ‘\0’ && s2[i] != 0; i++) { if (s1[i] < s2[i]) return –1; if (s1[i] > s2[i]) return 1; } if (i == n || s1[i] == s2[i]) return 0; if (s1[i] < s2[i]) return –1; return 1; }

strchr char *strchr(char s[], char ch) { int i; for (i = 0; s[i] != ‘\0’; i++) { if (ch == s[i]) return &s[i]; } return NULL; }

strrchr char *strrchr(char s[], char ch) { int i; char *ptr; ptr = NULL; for (i = 0; s[i] != ‘\0’; i++) { if (ch == s[i]) ptr = &s[i]; } return ptr; }

strstr char *strstr(char s1[], char s2[]) { int i, len1, len2; len1 = strlen(s1); len2 = strlen(s2); for (i = 0; i < len1-len2; i++) { if (!strncmp(&s[i], s2, len2)) return &s[i]; } return NULL; }

An Example First Middle Last Last, First Middle

An Example static void invertName(char result[], char name[]) { int len; char *sptr; len = strlen(name); sptr = strrchr(name, ‘ ’); if (sptr != NULL) len++; if (len > MaxName) Error(“Name too long”); if (sptr == NULL) { strcpy(result, name); } else { strcpy(result, sptr + 1); strcat(result, “, ”); strncat(result, name, sptr-name); result[len] = ‘\0’; }

Implementing strlib.h Using string.h, the user uses the type char *, and needs to worry about the memory allocation for strings Using strlib.h, the user uses the type string, and needs not worry about the memory allocation for strings. The memory for new strings are automatically allocated from heap

Pass-Through Functions int stringLength(string s) { return strlen(s); } int stringCompare(string s1, string s2) { return strcmp(s1, s2); }

String Allocation Functions static string createString(int len) { return (string) malloc(len+1); }

String Allocation Functions string charToString(char ch) { string result; result = createString(1); result[0] = ch; result[1] = ‘\0’; return result; }

String Allocation Functions string strcat(string s1, string s1) { string s; int i, len1, len2; len1 = strlen(s1); len2 = strlen(s2); s = createString(len1 + len2); for (i = 0; i < len1; i++) s[i] = s1[i]; for (i = 0; I < len2; i++) s[i+len1] = s2[i]; s[len1+len2] = ‘\0’; return s; }