CSC 107 – Programming For Science. Today’s Goal  Understand what cStrings are  Relationship to arrays & primitives  What the null character is; why.

Slides:



Advertisements
Similar presentations
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Advertisements

LECTURE 17 C++ Strings 18. 2Strings Creating String Objects 18 C-string C++ - string \0 Array of chars that is null terminated (‘\0’). Object.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
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.
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.
C Strings. The char Data Type for Storing Characters The char data type can is used to declare a variable that can hold a single character. Examples:
Lesson 10 Characters, C-Strings, and the string Class CS1 Lesson John Cole1.
Chapter 10.
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
N-1 University of Washington Computer Programming I Lecture 19: Strings © 2000 UW CSE.
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.
1 Array, Pointer and Reference ( I ) Ying Wu Electrical Engineering and Computer Science Northwestern University EECS 230 Lectures.
CSC 107 – Programming For Science. Today’s Goal  Learn how pointers really used with arrays  Exploit the similarity between pointers & arrays  Take.
1 Chapter 10 Characters, Strings, and the string class.
CSC 107 – Programming For Science. Today’s Goal ALL  Understand why ALL I/O is file I/O  Common bugs to avoid when coding with files in C++  Get a.
Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
1 Principles of Computer Science I Honors Section Note Set 10 CSE 1341.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
Chapter 10. Characters, Strings and the string class Csc 125 Introduction to C++ Fall 2005.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
Array, Pointer and Reference ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
CSC 107 – Programming For Science. The Week’s Goal.
9-1 Learning Objectives  An Array Type for Strings  C-Strings.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10: Characters, Strings, and the string class.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
CSC 107 – Programming For Science. Announcements.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
Characters, Strings, And The string Class Chapter 10.
1 CSE 2341 Object Oriented Programming with C++ Note Set #2.
CSC 107 – Programming For Science. Today’s Goal  Better understand arrays and how they work  Using array variable & its entries  When calling function,
String Class. C-style and C++ string Classes C-style strings, called C-strings, consist of characters stored in an array ( we’ll look at them later) C++
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 10: Characters, C- Strings, and More About.
Representing Strings and String I/O. Introduction A string is a sequence of characters and is treated as a single data item. A string constant, also termed.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 10 Characters, Strings, and the string class.
CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
Chapter 15 Strings as Character Arrays
Chapter 9 Strings. Learning Objectives An Array Type for Strings – C-Strings Character Manipulation Tools – Character I/O – get, put member functions.
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. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
C++ STRINGS ● string is part of the Standard C++ Library ● new stuff: ● cin : standard input stream (normally the keyboard) of type istream. ● >> operator.
An Array Type For Strings. Two ways to represent strings – i.e. “Hello” cstring An array with base type char Older way of processing strings Null character.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Chapter Characters, Strings, and the string class 10.
Strings, Slide Fundamental Programming Strings.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
PHY 107 – Programming For Science. Today’s Goal  Understand what cStrings are  Relationship to arrays & primitives  What it is and why null character.
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.
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.
Slide 1 Chapter 9 Strings. Slide 2 Learning Objectives  An Array Type for Strings  C-Strings  Character Manipulation Tools  Character I/O  get, put.
CSC 107 – Programming For Science. Today’s Goal  Write functions that take & return values  How parameters declared and how we call functions  What.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Fundamentals of Characters and Strings
CSC 113: Computer Programming (Theory = 03, Lab = 01)
Standard Version of Starting Out with C++, 4th Edition
Strings A collection of characters taken as a set:
Chapter 9 Strings Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Standard Version of Starting Out with C++, 4th Edition
C++ Programming Lecture 20 Strings
getline() function with companion ignore()
Chapter 12: More on C-Strings and the string Class
Presentation transcript:

CSC 107 – Programming For Science

Today’s Goal  Understand what cStrings are  Relationship to arrays & primitives  What the null character is; why we rely upon it  Know how several built-in string functions work

Today’s Goal  Understand what cStrings are  Relationship to arrays & primitives  What the null character is; why we rely upon it  Know how several built-in string functions work  Unlike “string theory”, cStrings practical & useful  Also understood by more than 10 people

Strings

What We Want  Human beings generally like using words & test  “Oh, boy! Numbers are cool” rarely, if ever, said nobody  Absolutely nobody prefers using 0 s & 1 s  Happy when using our language and not a computers

cStrings  cString  cString is special name for text-based data  Already been using cString literals  Text in quotation marks C++ treats as cString literal “Hi, Mom” “Hello world” “Good morning, Prof. Hertz. You are the greatest.” “Yes, the ‘Skins are the best football team.” “Yes! The Red Sox came back & beat the Yankees” “Wake up, Matthew. You were dreaming again”

Strings

It Is Like String Theory!

Using a cString  Remember: cString simply array of characters  Any legal use of char array okay for cString char bob[4] = “Bob”; char longStr[20] = “This is very long.”; char reallyLong[10000] = “Dummy”; char critics[] = “Statler & Waldorf”; char number[4] = “Four”; char bad[2] = “Will this compile?”; char hello[6] = {‘H’,‘e’,‘l’,‘l’,‘o’,‘\0’}; longStr = “Seventy-five”; bob = “o”; reallyLong[0] = ‘B’; bob[3] = ‘a’;

Using a cString  Remember: cString simply array of characters  Any legal use of char array okay for cString char bob[4] = “Bob”; char longStr[20] = “This is very long.”; char reallyLong[10000] = “Dummy”; char critics[] = “Statler & Waldorf”; char number[4] = “Four”; char bad[2] = “Will this compile?”; char hello[6] = {‘H’,‘e’,‘l’,‘l’,‘o’,‘\0’}; longStr = “Seventy-five”; bob = “o”; reallyLong[0] = ‘B’; bob[3] = ‘a’;

Using a cString  Remember: cString simply array of characters  Any legal use of char array okay for cString char bob[4] = “Bob”; char longStr[20] = “This is very long.”; char reallyLong[10000] = “Dummy”; char critics[] = “Statler & Waldorf”; char number[4] = “Four”; char bad[2] = “Will this compile?”; char hello[6] = {‘H’,‘e’,‘l’,‘l’,‘o’,‘\0’}; longStr = “Seventy-five”; bob = “o”; reallyLong[0] = ‘B’; bob[3] = ‘a’;

Using a cString  Remember: cString simply array of characters  Any legal use of char array okay for cString char bob[4] = “Bob”; char longStr[20] = “This is very long.”; char reallyLong[10000] = “Dummy”; char critics[] = “Statler & Waldorf”; char number[4] = “Four”; char bad[2] = “Will this compile?”; char hello[6] = {‘H’,‘e’,‘l’,‘l’,‘o’,‘\0’}; longStr = “Seventy-five”; bob = “o”; reallyLong[0] = ‘B’; bob[3] = ‘a’;

Using a cString  Remember: cString simply array of characters  Any legal use of char array okay for cString char bob[4] = “Bob”; char longStr[20] = “This is very long.”; char reallyLong[10000] = “Dummy”; char critics[] = “Statler & Waldorf”; char number[4] = “Four”; char bad[2] = “Will this compile?”; char hello[6] = {‘H’,‘e’,‘l’,‘l’,‘o’,‘\0’}; longStr = “Seventy-five”; bob = “o”; reallyLong[0] = ‘B’; bob[3] = ‘a’;

Using a cString  Remember: cString simply array of characters  Any legal use of char array okay for cString char bob[4] = “Bob”; char longStr[20] = “This is very long.”; char reallyLong[10000] = “Dummy”; char critics[] = “Statler & Waldorf”; char number[4] = “Four”; char bad[2] = “Will this compile?”; char hello[6] = {‘H’,‘e’,‘l’,‘l’,‘o’,‘\0’}; longStr = “Seventy-five”; bob = “o”; reallyLong[0] = ‘B’; bob[3] = ‘a’;

Using a cString  Remember: cString simply array of characters  Any legal use of char array okay for cString char bob[4] = “Bob”; char longStr[20] = “This is very long.”; char reallyLong[10000] = “Dummy”; char critics[] = “Statler & Waldorf”; char number[4] = “Four”; char bad[2] = “Will this compile?”; char hello[6] = {‘H’,‘e’,‘l’,‘l’,‘o’,‘\0’}; longStr = “Seventy-five”; bob = “o”; reallyLong[0] = ‘B’; bob[3] = ‘a’;

Using a cString  Remember: cString simply array of characters  Any legal use of char array okay for cString char bob[4] = “Bob”; char longStr[20] = “This is very long.”; char reallyLong[10000] = “Dummy”; char critics[] = “Statler & Waldorf”; char number[4] = “Four”; char bad[2] = “Will this compile?”; char hello[6] = {‘H’,‘e’,‘l’,‘l’,‘o’,‘\0’}; longStr = “Seventy-five”; bob = “o”; reallyLong[0] = ‘B’; bob[3] = ‘a’;

Using a cString  Remember: cString simply array of characters  Any legal use of char array okay for cString char bob[4] = “Bob”; char longStr[20] = “This is very long.”; char reallyLong[10000] = “Dummy”; char critics[] = “Statler & Waldorf”; char number[4] = “Four”; char bad[2] = “Will this compile?”; char hello[6] = {‘H’,‘e’,‘l’,‘l’,‘o’,‘\0’}; longStr = “Seventy-five”; bob = “o”; reallyLong[0] = ‘B’; bob[3] = ‘a’;

Using a cString  Remember: cString simply array of characters  Any legal use of char array okay for cString char bob[4] = “Bob”; char longStr[20] = “This is very long.”; char reallyLong[10000] = “Dummy”; char critics[] = “Statler & Waldorf”; char number[4] = “Four”; char bad[2] = “Will this compile?”; char hello[6] = {‘H’,‘e’,‘l’,‘l’,‘o’,‘\0’}; longStr = “Seventy-five”; bob = “o”; reallyLong[0] = ‘B’; bob[3] = ‘a’;

Using a cString  Remember: cString simply array of characters  Any legal use of char array okay for cString char bob[4] = “Bob”; char longStr[20] = “This is very long.”; char reallyLong[10000] = “Dummy”; char critics[] = “Statler & Waldorf”; char number[4] = “Four”; char bad[2] = “Will this compile?”; char hello[6] = {‘H’,‘e’,‘l’,‘l’,‘o’,‘\0’}; longStr = “Seventy-five”; bob = “o”; reallyLong[0] = ‘B’; bob[3] = ‘a’;

Using a cString  Remember: cString simply array of characters  Any legal use of char array okay for cString char bob[4] = “Bob”; char longStr[20] = “This is very long.”; char reallyLong[10000] = “Dummy”; char critics[] = “Statler & Waldorf”; char number[4] = “Four”; char bad[2] = “Will this compile?”; char hello[6] = {‘H’,‘e’,‘l’,‘l’,‘o’,‘\0’}; longStr = “Seventy-five”; bob = “o”; reallyLong[0] = ‘B’; bob[3] = ‘a’;

cString Input & Output  Already works with cout just int or double  This is not normal & only for array of characters  Prints all characters until null character is hit  Array’s length unknown, only stops at null character  Two possible ways to read in a cString using cin  Can read in all characters until whitespace reached  Read in at most n characters on a single line

Reading in “word” with cin  Can use cin to read in single “word”  Skips over any whitespace until finds a real character  Continues reading until it hits next whitespace  Result (& end null character) stored in cString  Caveat emptor: no check that array has enough space char lastName[80]; cin >> lastName; char middleName[2]; cin >> middleName;

Reading in Line with cin  Can use cin to read in up to an entire line  Where last cin statement stopped, this call will start  Continues reading until it hits end of the line  Result (& end null character) stored in cString  No worries: includes maximum characters to read char lastName[80]; cin.getline(lastName, 80); char middleInitial[2]; cin.getline(middleName, 2);

Built-in String Functions  To use built-in string function, your program needs: #include #include  Many, many function exist for you to use  Several of these listed on page 311 of book  Special feature of cStrings used by these functions  Unlike other arrays, can pass cString literals to functions char str[10] = “c”; str = “b”; strcat(str, “at”); cout << strlen(“Hi mom.”);

Built-in String Functions  To use built-in string function, your program needs: #include #include  Many, many function exist for you to use  Several of these listed on page 311 of book  Special feature of cStrings used by these functions  Unlike other arrays, can pass cString literals to functions char str[10] = “c”; str = “b”; strcat(str, “at”); cout << strlen(“Hi mom.”);

Important String Functions  strlen(s)  Counts characters in s before null character  strcpy(s,t)  Copies characters in t into s.  strcat(s,t)  Appends t to end of s.  strcmp(s,t)  Returns negative int if s t, and 0 if s==t  strupr(s)  Converts all characters in s to uppercase  strlwr(s)  Converts all characters in s to lowercase  strncpy(s,t,n)  Copied first n characters from t to s  stricmp(s,t)  Like strcmp, but treats upper & lowercase characters as equals

Using String Functions char cat[100] = “ca”; int i; i = strlen(cat); cat[2] = ‘t’; i = strlen(cat); strcpy(cat, “dog”); cout << cat << endl; strcat(cat, “ged”); cout << cat << endl; strcpy(cat, “ca”); cat[2] = ‘t’; cat[3] = ‘\0’; cout << cat << endl;

Using String Functions char cat[100] = “cat” char str[100] = “apult”; strcat(cat, str); cout << cat << “ ” << str << endl; cout << strcmp(cat, str) << endl; cat[0] = ‘C’; cout << strcmp(cat, “catapult”) << endl; cout << stricmp(cat, “catapult”) << endl; cat[1] = ‘\0’; strcat(str, cat); cout << cat << “ ” << str << endl;

Your Turn  Get into your groups and try this assignment

For Next Lecture  Multidimensional arrays in Section –  What do these arrays look like?  How can we create pictures & bridges in C++?  How much cooler can arrays get?  Angel also has Weekly Assignment #9 due today  Programming Assignment #2 due on Friday