CS 1400 Chapter 10 Strings. Character testing library #include bool isalpha (char c); bool isalnum (char c); bool isdigit (char c); bool islower (char.

Slides:



Advertisements
Similar presentations
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
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.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: More on C-Strings and the string Class Starting Out with.
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.
Lesson 10 Characters, C-Strings, and the string Class CS1 Lesson John Cole1.
Chapter 8 Characters and Strings Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 8 - Characters and Strings Outline 8.1Introduction.
CS 1400 Apr 18, 2007 Chapter 10 Strings. Character testing library #include bool isalpha (char c); bool isalnum (char c); bool isdigit (char c); bool.
CS 1400 Chapters 9, 10 C-strings and Pointers. A few C-string library functions… #include int strcmp (char a[ ], char b[ ]); returns 0 if string a is.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 12: More About.
CS 1400 Apr 6, 2007 Chapters 9, 10 C-strings and Pointers.
CS 1400 Chapters 9, 10 C-strings and Pointers. A few C-string library functions… #include int strcmp (char a[ ], char b[ ]); returns 0 if string a is.
CS Nov 2006 C-strings.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 12 More.
C-strings and C++ string Class
 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.
1 Chapter 10 Characters, Strings, and the string class.
CSIS 123A Lecture 5 Friend Functions Glenn Stevenson CSIS 113A MSJC.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
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.
One Dimensional Arrays (Part2) Sorting Algorithms Searching Algorithms Character Strings The string Class. 1.
Lecture 17: Characters, Strings, and the string class Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
February 14, 2005 Characters, Strings and the String Class.
EGR 2261 Unit 9 Strings and C-Strings  Read Malik, pages in Chapter 7, and pages in Chapter 8.  Homework #9 and Lab #9 due next week.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: More on C-Strings and the string Class Starting Out with.
Chapter 10. Characters, Strings and the string class Csc 125 Introduction to C++ Fall 2005.
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.
STARTING OUT WITH STARTING OUT WITH Class 9 Honors.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
Arrays II (Strings). Data types in C Integer : int i; Double: double x; Float: float y; Character: char ch; char cha[10], chb[]={‘h’,’e’,’l’,’l’,’o’};
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.
Characters, Strings, And The string Class Chapter 10.
Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 10: Characters, C- Strings, and More About.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 10 Characters, Strings, and the string class.
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.
Chapter Characters, Strings, and the string class 10.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
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.
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.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
C-Strings We have already seen that a C-string is a null-terminated array of characters.
C Characters and Strings
Characters, C-Strings, and More About the string Class
CSE 303 Lecture 14 Strings in C
Chapter 8 - Characters and Strings
Chapter 12: More on C-Strings and the string Class
Standard Version of Starting Out with C++, 4th Edition
10.1 Character Testing.
Standard Version of Starting Out with C++, 4th Edition
C Characters and Strings
Presentation transcript:

CS 1400 Chapter 10 Strings

Character testing library #include bool isalpha (char c); bool isalnum (char c); bool isdigit (char c); bool islower (char c); bool isupper (char c); bool ispunct (char c); bool isprint (char c); bool isspace (char c);

Example… Verify that a password contains at least 5 alpha and 3 numeric characters char password[10]; int numalpha = 0, numdigit = 0; cin >> password; for (int n=0; n<strlen(password); n++) {if (isalpha(password[n])) numalpha++; else if (isdigit(password[n])) numdigit++; } if (numalpha >= 5 && numdigit >= 3) cout << “valid!\n”;

Character conversion library #include char toupper (char c); char tolower (char c);

Example… Convert a C-string to all lower-case void ConvertToLower (char line[ ]) { int len = strlen(line); for (n=0; n<len; n++) if (isupper(line[n])) line[n] = tolower(line[n]); }

C-String library functions #include int strlen (char str[ ]); void strcpy (char dest[ ], char src[ ]); int strcmp (char src1[ ], char src2[ ]); FYI void strcat (char dest[ ], char src[ ]); void strncpy (char dest[ ], char src[ ], int count); char* strstr (char str[ ], char search[ ]);

The NULL pointer… A special pointer value of zero has been defined as NULL. Some functions returning pointers will return NULL on some occasions Ex: char line[80]; cin >> line; if (strstr (line, “Mr.”) == NULL) cout << “this line does not contain Mr.”;

String-to-Number conversions #include int atoi (char word[ ]); int atol (char word[ ]); float atof (char word[ ]); Note: itoa() has been deprecated under Microsoft Visual Studio.NET.

Number-to-string conversions #include void sprintf (char line[ ], format_literal, var_list); // convert these variables into a C-String –format_literal: a C-string containing a format specifier for each variable to be converted –var_list: a comma-separated list of variables

Format Specifiers for sprintf() %dan integer position %fa float position %sa C-String position %ndan integer position of n columns %n.mfA float position of n columns, rounding to m decimal places any word that doesn’t begin with % is simply inserted as a literal…

sprintf() examples… char line[80]; int x = 1234; float y = ; sprintf (line, “age: %d cost: $ %f”, x, y); cout << line << endl; sprintf (line, “cost rounded: $ %5.2f”, y); cout << line << endl; output age: 1234 cost: $ cost rounded: $ 3.14

C-String example: Write a program to read in a form letter containing occurrences of the word “CUSTOMER”. Next, allow the user to input a person’s name. Now, replace all occurrences of the word “CUSTOMER” with this name and print the letter. formletter.zip

Form letter Dear CUSTOMER: Congratulations! You have won one of the following trips; 7 days in Hawaii, 10 days in San Francisco, or one day in Park City being hounded by a condo salesman. Yes CUSTOMER, this is the dream award of a lifetime! To claim your trip CUSTOMER, just call CHEATEM. We look forward to taking you on this trip, Bogus Travel and Condo Sales

Example execution: Enter a customer name: Fred Dear Fred: Congratulations! You have won one of the following trips; 7 days in Hawaii, 10 days in San Francisco, or one day in Park City being hounded by a condo salesman. Yes Fred, this is the dream award of a lifetime! To claim your trip Fred, just call CHEATEM. We look forward to taking you on this trip, Bogus Travel and Condo Sales

Manipulating strings There are several ways of manipulating textual strings; –C-strings strings are stored in arrays of characters –C++ string objects strings are stored in an abstract data type –CString objects strings are stored in an abstract data type specific to Microsoft GUIs. –etc…..

The C++ string class A class is an abstract data type -- a new programmer-defined data type with its own set of manipulation functions class functions are called using “dot” notation. You’ve seen this before! ifstream fin; fin.open(“myfile.txt”);

The C++ string class #include A C++ string variable can be treated as a simple variable capable of holding a string. A C++ string is not just a char array. A C++ string can be assigned and compared with ordinary assignment and comparison operators such as = and <. string word1, word2; word1 = “Hello”; cin >> word2; if (word1 < word2) cout << “less than”;

C++ string class How big is a string variable (how many characters can it hold)? –as big as it needs to be How is this all accomplished? –don’t know and don’t care (at this point)

Concatenation The “+” operator can be used between two strings, but it means concatenation (not addition) string word1 = “Hello “; string word2 = “and Goodbye!”; string line; line = word1 + word2; cout << line << endl;

Accessing individual characters… Individual characters in a string can be accessed using bracket [ ] notation. cout << word1[3]; // output character 3 word1[4] = ‘X’;// replace character 4 While this makes it appear like a string is an array, it is not! In C++, an operator used with a class means anything the designer of that class wants it to. (More on this to come…)

string member functions string line, word; line.append (word);// append word onto line cout << line.length(); // output the length of line line.erase (position, count); // erase count characters beginning at position pos = line.find (search, position); // determine the index position of search in line (beginning at position) line.swap (word); // swap the contents of line and word cout << line.substr (position, count); // output a substring beginning at position for count characters and many many more…

Conversions… A C-string or literal can be assigned to a string char name[5] = “Fred”; string othername; othername = name; But a string cannot be assigned to a C- string name = othername;

Rule of Thumb… There are many compatibilities between C-strings and strings, but it is best to use one or the other in a program (at this point in your career).

Examples… Output “YES” if an input string contains the word “MONEY” Find and output a dollar-and-cents amount in a string. Erase all the blanks in a string. Capitalize the name “fred” in a string. Replace, with

Sorting strings void SelectionSort (string array[ ], int size) { int smallest_position; for (int n=0; n<size-1; n++) {smallest_position = FindSmallest (array, n, size); Swap (array, n, smallest_position); } If you have a sorting function that works for integer arrays, it will also work for string arrays – just change int to string! sorting.zip

Find_Smallest() int FindSmallest (string array[ ], int start, int size) { int position = start; string smallest = array[start]; for (int k=start+1; k<size; k++) if (array[k] < smallest) { smallest = array[k]; position = k; } return position; }

Swap() void Swap (string array[ ], int j, int k) { string temp; temp = array[j]; array[j] = array[k]; array[k] = temp; } or… void Swap (string array[ ], int j, int k) { array[j].swap(array[k]); }

Input for strings… ifstream fin; string string_a, string_b, string_c; char c; … fin >> string_a; // input the next word getline (fin, string_b); // input next line while (fin.get(c)) string_c += c; // input to end of file

More string member funcs… string s; s.insert (pos, substr); // insert substr at pos s.replace(pos, n, str); // replace n chars at pos with str example: s = “ me”; s.insert(0,”hello”); s.replace(3, 2, “p”); cout << s;// outputs “help me”

C++ string example: Write a program to read in a form letter containing occurrences of the word “CUSTOMER”. Next, allow the user to input a person’s name. Now, replace all occurrences of the word “CUSTOMER” with this name and print the letter. stringformletter.zip

Example Write a program to prompt a user to input a value. Next, add 6% and output this result in standard commercial notation (commas and dollar sign, rounded to 2 dec. places) Input a value: With 6% added: $24,864, strings.cpp