String What it is Why it’s useful

Slides:



Advertisements
Similar presentations
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
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 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.
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.
CMSC 2021 C++ I/O and Other Topics. CMSC 2022 Using C++ Stream I/O Default input stream is called cin Default output stream is called cout Use the extraction.
Chapter 10.
Pointer What it is How to declare it How to use it Relationship between arrays and pointers Relationship between strings and pointers.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
String What it is Why it’s useful library routines for handling strings how to input a string from the keyboard.
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.
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.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
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.
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
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
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.
Review Pointer Pointer Variables Dynamic Memory Allocation Functions.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Strings, Slide Fundamental Programming Strings.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
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.
Slide 1 Chapter 9 Strings. Slide 2 Learning Objectives  An Array Type for Strings  C-Strings  Character Manipulation Tools  Character I/O  get, put.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Introduction Programs which manipulate character data don’t usually just deal with single characters, but instead with collections of them (e.g. words,
Lecture 2A Data Types Richard Gesick
Strings (Continued) Chapter 13
CMSC 202 Lesson 2 C++ Primer.
Fundamentals of Characters and Strings
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Strings (part 2) Dr. Xiao Qin Auburn.
© 2016 Pearson Education, Ltd. All rights reserved.
Arrays Arrays exist in almost every computer language.
CSC 113: Computer Programming (Theory = 03, Lab = 01)
A First Book of ANSI C Fourth Edition
Standard Version of Starting Out with C++, 4th Edition
String in C++ A string is an array of characters which contains a non-printing null character ‘\0’ ( with ASCII value 0 ) marking its end. A string.
Arrays and Strings Chapter 9.
C Stuff CS 2308.
C++ STRINGS string is part of the Standard C++ Library
INC 161 , CPE 100 Computer Programming
Pointers and Pointer-Based Strings
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
String in C++.
Strings Dr. Soha S. Zaghloul updated by Rasha ALEidan
Chapter 9 Strings Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Chapter 8 Character Arrays and Strings
CPS120: Introduction to Computer Science
Engineering Problem Solving with C++, Etter
Standard Version of Starting Out with C++, 4th Edition
Arrays Arrays A few types Structures of related data items
CS250 Introduction to Computer Science II
Lecture 2 Arrays & Pointers May 17, 2004
C++ Programming Lecture 20 Strings
CS-161 Computer Programming Lecture 15 & 16: Arrays II
Lecture 2 Arrays & Pointers September 7, 2004
CS31 Discussion 1H Fall18: week 6
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

String What it is Why it’s useful library routines for handling strings how to input a string from the keyboard

strings What’s the difference between these two declarations? char letter [ ] = {‘A’}; char letter[ ] = { “A”};

strings What’s the difference between these two declarations? char letter [ ] = {‘A’}; letter A char letter[ ] = { “A”}; letter A NULL

string array of characters Terminated with the NULL character. \0

array of characters char digits [ ] = {‘0’, ’1’, ’2’, ’3’, ’4’, ’5’, ’6’, ’7’, ’8’, ’9’}; digits[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] ‘0’ ‘1’ How many elements does the digits array contain? ‘2’ ‘3’ ‘4’ ‘5’ ‘6’ ‘7’ ‘8’ ‘9’

string variable char digits [ ] = {“0123456789”}; digits[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] ‘0’ ‘1’ How many elements does the digits string contain? ‘2’ ‘3’ ‘4’ ‘5’ ‘6’ ‘7’ ‘8’ ‘9’ NULL

string variable char digits [ ] = {‘0’, ’1’, ’2’, ’3’, ’4’, ’5’, ’6’, ’7’, ’8’, ’9’, ’\0’}; digits[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] ‘0’ ‘1’ ‘2’ ‘3’ ‘4’ ‘5’ ‘6’ ‘7’ ‘8’ ‘9’ ‘\0’

Why are strings useful? Can treat a string as a unit instead of dealing with it as a bunch of individual characters.

Outputting a message without strings Suppose you wanted to output the message “I love JEDDAH!” using an array of characters. const int size = 14; char message [size] = {‘I', ' ', 'l', 'o', 'v', 'e', ' ',‘J', ‘E', ‘D', ‘D', ‘A', ‘H', '!'}; for (int i = 0; i < size; i++) { cout << message[i]; } cout << endl;

outputting a message using a string char Message [] = {“I Love JEDDAH!” }; cout << Message << endl; Note that only the name of the array is provided to cout not the size. cout Starts at the first element of the Message array. cout Stops when NULL character is encountered.

outputting a message using a string [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] ‘I’ cout << “I Love JEDDAH!”; Compiler creates char array in memory terminated by NULL character. This is called a string constant. It is an unnamed array and cannot be changed. Now you know how it’s done! ‘ ’ ‘l’ ‘o’ ‘v’ ‘e’ ‘ ’ ‘J’ ‘E’ ‘D’ ‘D’ ‘A’ ‘H’ ‘!’ NULL

Exercises 1) Write a code segment that displays your name. Create a string variable (not a string constant). Use cout to display it 2) What is the size of the string variable containing your name? String variable???

string handling library functions strcpy - copies one string to another strcat - appends one string to another strlen - returns the length of a string Cat = concatenated

copying arrays const int size = 6; char String1[ ] = {“Hello”}; char Sentence[100]; Sentence = String1; // ERROR! would have to copy it one character at a time. for (int i = 0; i < size; i++) { Sentence [i] = String1[i]; } Without strcpy would have to copy it one character at a time

using strcpy strcpy (Destination, Source); char String1[ ] = {“Hello”}; char Sentence[100]; strcpy (Sentence, String1); cout << Sentence << endl; displays Hello Sentence[0] [1] [2] [3] [4] [5] ‘H’ ‘e’ ‘l’ ‘l’ ‘o’ NULL

using strcat strcat (Destination, Source); char String1[ ] = {“Hello”}; char String2[ ] = {“ World!}; char Sentence[100]; strcpy (Sentence, String1); strcat (Sentence, String2); cout << Sentence << endl; displays Hello World! Note: strcpy copied a NULL char at the end of String1. strcat replaced that NULL with beginning of String2 Sentence[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] ‘H’ ‘e’ ‘l’ ‘l’ ‘o’ ‘W’ ‘o’ ‘r’ ‘l’ ‘d’ ‘!’ NULL’

using strlen char String1[ ] = {“Hello”}; int length = strlen (String1); cout << length << endl; Displays 5 Note: strlen returns the length of the string excluding the NULL character.

Creating sentences example Write a program that uses a random number generator to create sentences. The program should use 4 arrays of strings called article, noun, verb and preposition. The program should create a sentence by selecting a word at random from each array in the following order: article, noun, verb, proposition, article, noun. As each word is picked it should be concatenated to the previous words in the sentence.

Array of strings const int Size = 5; const int WordSize = 10; char article [Size] [WordSize] = {"the", "a", "one", "some", "any"}; char noun [Size] [WordSize] = {"boy", "girl", "dog", "town", "car"}; char verb [Size] [WordSize] = {"drove", "jumped", "ran", "walked", "skipped"}; char preposition [Size] [WordSize] = {"to", "from", "over", "under", "on"};

inputting a string using cin const maxSize = 20; char inputMessage[maxSize]; cin >> inputMessage; cin reads in characters from the keyboard and puts them in the inputMessage array cin doesn’t stop until it encounters whitespace. Could read more characters than will fit in the array. !! Problem with cin and more character in array which cause an error and whitespace -Error for having long string than the array can hold - Whitespace will stop entering string in array - Cin will store value in array by index inputMessage[i] by using for loop

inputting a string using cin const maxSize = 20; char inputMessage[maxSize]; cin >> setw(maxSize) >> inputMessage; setw(maxSize) tells cin the size of the array is 20. cin will read in only 19 characters to leave room for the terminating NULL character. cin does not store the NULL character for you. //inputMessage[maxSize -1] = NULL; - Setw will solve the problem of string overrun array size - Off course cin will not store null character for you. Null will be stored by default. - maxSize – 1 -> 20 – 1 = 19 -> null stored in this location - inputMessage[maxSize -1] = NULL  will only show the location of null it is not a statement at all.

inputting a string containing whitespace using cin.get const int Size = 20; char Message [Size]; cin.get (Message, Size); cin.get continues reading characters into the Message array until: - Size -1 characters have been read - Newline character is encountered. cin.get stores the NULL character immediately following the last character read. - Solving whitespace problem with cin.get - cin.get will include whitespace as an input for the array. - newline character is '\n'; it causes the insertion point to move to the beginning of the next line.

Exercises 1) How many elements in vowels array: char vowels [] = {‘a’,’e’, ‘i’,’o’, ‘u’}; 2) How many elements in vowels string: char vowels [] = {“aeiou”}; 3) Find the error in the following code segment: const int Size = 7; char inputMessage[Size]; cin >> setw (Size) >> inputMessage; // user types the word goodbye at the keyboard. 1- 5 2- 6 3- e has no location in the array (the last position will be used for null character)