CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout << “str1:

Slides:



Advertisements
Similar presentations
LECTURE 17 C++ Strings 18. 2Strings Creating String Objects 18 C-string C++ - string \0 Array of chars that is null terminated (‘\0’). Object.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
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.
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
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.
Array_strcpy void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; }
Lecture 24: Strings. 2 Lecture Contents: t Library functions t Assignment and substrings t Concatenation t Comparison t Demo programs t Exercises.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 1 Overview 8.1 An Array Type for Strings 8.2 The Standard string.
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:
. Plab – Tirgul 2 Const, C Strings. Pointers int main() { int i,j; int *x; // x points to an integer i = 1; x = &i; j = *x; ijx 1.
Tuesday, January 23, 2007 "We can't solve problems by using the same kind of thinking we used when we created them." -Albert Einstein.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
1 C-strings String = null-terminated array of characters The null character ('\0') specifies where the string terminates in memory. Example: The string.
CS 192 Lecture 11 Winter 2003 December 29-30, 2003 Dr. Shafay Shamail.
Friday, January 05, 2007 A few weeks of developing and testing can save a whole afternoon in the library. -Anonymous.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
Programming Strings. COMP102 Prog. Fundamentals: Strings / Slide 2 Character Strings l A sequence of characters is often referred to as a character “string”.
Chapter 9: Arrays and Strings
CS Nov 2006 C-strings.
String What it is Why it’s useful library routines for handling strings how to input a string from the keyboard.
CS 117 Spring 2002 Review for Exam 3 arrays strings files classes.
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.
C-strings and C++ string Class
Chapter 5 Arrays and Strings C Programming © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
In Addition... To the string class from the standard library accessed by #include C++ also has another library of string functions for C strings that can.
Chapter 8 Strings and Vectors (8.1 and 8.2). An Array of characters Defined as: char firstName[20]; char firstName[] = {‘T’, ‘i’, ‘m’}; // an array of.
Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
String Library Calls (Representative, not Exhaustive) Rudra Dutta CSC Spring 2007, Section 001.
ITEC 320 C++ Examples.
C++ PROGRAMMING: PROGRAM DESIGN INCLUDING DATA STRUCTURES, FIFTH EDITION Chapter 10: Strings and string type.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
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)
Define our own data types We can define a new data type by defining a new class: class Student {...}; Class is a structured data type. Can we define our.
Xuan Guo Review for the Final Exam Xuan Guo July 29 8:30AM – 10:30AM Classroom South 300 CSC
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
String functions+ string I.Mona Alshehri. String Functions: Header file:#include Function: Int strlen(char s[n]) Description Calculates the length of.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
1 Character Strings (Cstrings) Reference: CS215 textbook pages
COIT29222-Structured Programming Lecture Week 08  Reading: Textbook (4 th Ed.), Chapter 4 Textbook (6 th Ed.), Chapter 7 Study Guide Book 2, Module 11.
CSC 270 – Survey of Programming Languages
1 CS 1430: Programming in C++. 2 Find Max, Min, Average of m Sections Max, Min and Average of each section Max, Min and Average of all sections together.
Sahar Mosleh California State University San MarcosPage 1 Character String.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
String operations. About strings To handle strings more easily, we need to include a library> #include To see what the library allows us to do, look here:
Data Types Storage Size Domain of all possible values Operations 1.
CS 1430: Programming in C++ 1. Class StudentList class StudentList { private: int numStudents; Student students[MAX_SIZE]; int find(const Student& s)
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Strings, Slide Fundamental Programming Strings.
Class Method Read class Student { private: string id; string firstName, lastName; float gpa; public: // Will the method change any data members? // Yes!
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Strings, and the string Class. C-Strings C-string: sequence of characters stored in adjacent memory locations and terminated by NULL character The C-string.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
Suyash Bhardwaj Dept. of Computer Science & Engineering Faculty of Engineering & Technology Gurukul Kangri University, Haridwar l1l1 UNIT 1 String Processing.
Introduction Programs which manipulate character data don’t usually just deal with single characters, but instead with collections of them (e.g. words,
Characters, Strings, and the cstring Library
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Lecture 8 String 1. Concept of strings String and pointers
Characters, Strings, and the cstring Library
CS 1430: Programming in C++ No time to cover HiC.
Strings A collection of characters taken as a set:
String in C++.
String What it is Why it’s useful
C++ Programming Lecture 20 Strings
Introduction to Problem Solving and Programming
Presentation transcript:

CS 1430: Programming in C++ 1

Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout << “str1: ” << str1 << endl << “str2: ” << str2; str2 = str1; 2

C++ Class string if (str1 == “CS2340”) cout << “Programming in VB!”; if (str1 > str2) cout << “str1 appears after str2 in a dictionary.”; else if (str1 < str2) cout << “str1 appears before str2 in a dictionary.”; else cout << “str1 is the same as str2.” str1 is less than str2 if word str1 is listed before str2 in a dictionary (ASCII code ordering) 3

C++ Class string cout << “str1 has “ << str1.length() << “ chars.”; cout << “str2 has “ << str2.size() << “ chars.”; // Just like cin.get() and cin.eof() // They are objects of some classes 4

C++ Class string String is implemented as an array of char cin >> str1; cout << “The first char of str1 ” << str1[0]; cout << “The last char of str1 ” //<< str1[?]; << str1[str1.length() - 1]; // Change the first char of str1 to ‘A’. str1[0] = ‘A’; 5

C++ Class string SubString Function cin >> str1; cout << “The first three chars of str1: ” << str1.substr(0, 3); // substring starting at index 0 // with length 3 cout << “The last three chars of str1: ” << str1.substr(str1.length() – 3, 3); 6

C++ Class string getline(cin, str1, ‘\n’); // not a member function of class string int pos = str1.find(“ ”); // a member function of class string str1[pos] = ‘_’; cout << str1; // Change the first space to ‘_’. 7

C String const int NAME_LENGTH = 15; char LastName[NAME_LENGTH + 1]; // One more for the null char ‘\0’. cout << "Enter last name: "; cin >> LastName; // No loops needed! // C++ reads chars until White Characters, // then inserts a ‘\0’. // The array is treated as a string. // and ended with a null char ‘\0’. 8

C String cout << "The last name is " << LastName; // C++ displays one char at a time // until a '\0' is reached. What if there is no null character? 9

C String char name1[16], name2[16]; // Up to 15 chars! cout << "Enter last name: "; cin >> name1; name2 = name1; // Can we do this? cin >> name2; if (name1 == name2) // Can we do this? cout << “Same name!”; NO! 10

C String Functions #include //#include Three functions: int strlen(const char str[]); void strcpy(char dest[], const char src[]); int strcmp(const char str1[], const char str2[]); 11

C String Functions #include char name1[16], name2[16]; cout << "Enter last name: "; cin >> name1; name2 = name1; // Valid? // NO! strcpy(name2, name1); // Yes! cout << “name1 has ” << strlen(name1) << “ chars.”; 12

Function strcmp() The function compares two strings one char at a time, and stops the first time the two strings have different chars or a null char is reached for both str1 and str2. The function returns the difference of the chars at the stopping postion of the two strings. Return value from strcmp(srt1, srt2) Result 0 str1 the same as str2 > 0 str1 is larger than str2 (later in a dictionary) < 0 str1 is smaller than str2 (earlier in a dictionary) str1 str2 strcmp(srt1, srt2) “CS143” “CS143” ? “CS1430” “CS143” ? “CS143” “CS1430” ? “CS113” “CS143” ? “100” “99” ? 13

C String Functions #include char name1[16], name2[16]; cin >> name1 >> name2; int result = strcmp(name1, name2); if (result == 0) cout << “Same string.”; else if (result < 0) cout << “name1 is smaller than name2.”; else cout << “name1 is larger than name2.”; 14

Function strcpy() // // The function has two parameters: // dest[], array of char, // src[], array of char. // The function copies src[] to dest[] and inserts // a null char at the end. // Parameter: ( ?, ? ) // Parameter: (out, in) // void strcpy(char dest[], const char src[]) { for (int i = 0; src[i] != ‘\0’; i ++) dest[i] = src[i]; return; } // Correct? 15

Function strcpy() // // The function has two parameters: // dest[], array of char, // src[], array of char. // The function copies src[] to dest[] and inserts // a null char at the end. // Parameter: (out, in) // void strcpy(char dest[], const char src[]) { for (int i = 0; src[i] != ‘\0’; i ++) dest[i] = src[i]; dest[i] = ‘\0’; // Copy the NULL character. return; } // Correct? 16

Function strcpy() // // The function has two parameters: // dest[], array of char, // src[], array of char. // The function copies src[] to dest[] and inserts // a null char at the end. // Parameter: (out, in) // void strcpy(char dest[], const char src[]) { int i; for (i = 0; src[i] != ‘\0’; i ++) dest[i] = src[i]; dest[i] = ‘\0’; return; } 17

Function strcpy() // // The function has two parameters: // dest[], array of char, // src[], array of char. // The function copies src[] to dest[] and inserts // a null char at the end. // Parameter: (out, in) // void strcpy(char dest[], const char src[]) { int i = 0; while (src[i] != ‘\0’) { dest[i] = src[i]; i ++; } dest[i] = ‘\0’; return; } 18

Function strlen() // // The function has one parameter: // str[], array of char. // The function finds and returns the length of // str[], excluding the null // char at the end. // Parameter: ( ? ) // Parameter: ( IN ) // int strlen(const char str[]) { int size = 0; while (str[size] != ‘\0’) size ++; return size; } 19

Function strcmp() // // The function has two parameters: // str1[], array of char, null terminated, // str2[], array of char, null terminated. // The function returns an integer: // 0 when str1 is the same as str2 // positive when str1 > str2 // negative when str1 < str2. // Parameter: ( ?, ? ) // Parameter: ( IN, IN ) // int strcmp(const char str1[], const char str2[]) { int i; for (i = 0; str1[i] != ‘\0’ && str2[i] != ‘\0’; i ++) if (str1[i] != str2[i]) return (str1[i] - str2[i]); } 20

Function strcmp() // // The function has two parameters: // str1[], array of char, null terminated, // str2[], array of char, null terminated. // The function returns an integer: // 0 when str1 is the same as str2 // positive when str1 > str2 // negative when str1 < str2. // Parameter: ( IN, IN ) // int strcmp(const char str1[], const char str2[]) { int i; // Can we check just str1[i]? for (i = 0; str1[i] != ‘\0’ && str2[i] != ‘\0’; i ++) if (str1[i] != str2[i]) return (str1[i] - str2[i]); } 21

Function strcmp() // // The function has two parameters: // str1[], array of char, null terminated, // str2[], array of char, null terminated. // The function returns an integer: // 0 when str1 is the same as str2 // positive when str1 > str2 // negative when str1 < str2. // Parameter: (in, in) // int strcmp(const char str1[], const char str2[]) { int i; // Just check str1[i]? for (i = 0; str1[i] != ‘\0’; i ++) if (str1[i] != str2[i]) return (str1[i] - str2[i]); } Very Good! 22

Schedule Prog5 Due Time: 10 PM, Thursday Lab10 Due 5 PM, Thursday Friday? 23