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

Slides:



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

1 Arrays and Strings Chapter 9 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
 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 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.
 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.
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.
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:
Chapter 10.
Pointer What it is How to declare it How to use it Relationship between arrays and pointers Relationship between strings and pointers.
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
CS 192 Lecture 11 Winter 2003 December 29-30, 2003 Dr. Shafay Shamail.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
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.
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.
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.
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
Functions Why we use functions C library functions Creating our own functions.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
Array, Pointer and Reference ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
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’};
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
Structured Programming Instructor: Prof. K. T. Tsang Lecture 6: Arrays 数据序列, 数组 1.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
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.
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. 5.11Function Pointers Pointers to functions –Contain address of function –Similar to how array name is.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization.
 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.
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:
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)
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Chapter Nine Strings. Char vs String Literals Size of data types: Size of data types: –sizeof(“hello\n”)7 bytes –sizeof(“hello”)6 bytes –sizeof(“X”)2.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual.
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.
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.
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.
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,
Fundamentals of Characters and Strings
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
String What it is Why it’s useful
C++ Programming Lecture 20 Strings
CS31 Discussion 1H Fall18: week 6
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’}; char letter[ ] = { “A”}; letter A 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’}; ‘0’ ‘1’ ‘3’ ‘4’ ‘5’ ‘6’ ‘7’ ‘8’ ‘9’ digits[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] ‘2’ How many elements does the digits array contain?

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

string variable char digits [ ] = {‘0’, ’1’, ’2’, ’3’, ’4’, ’5’, ’6’, ’7’, ’8’, ’9’, ’\0’}; ‘0’ ‘1’ ‘3’ ‘4’ ‘5’ ‘6’ ‘7’ ‘8’ ‘9’ digits[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] ‘2’ ‘\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 CS1005!” using an array of characters. const int size = 14; char message [size] = {'I', ' ', 'l', 'o', 'v', 'e', ' ','C', 'S', '1', '0', '0', '5', '!'}; for (int i = 0; i < size; i++) { cout << message[i]; } cout << endl;

outputting a message using a string char Message [] = {“I Love CS1005!” }; 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 ‘ ’ ‘o’ ‘v’ ‘e’ ‘ ’ ‘C’ ‘S’ ‘1’ [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] ‘l’ NULL ‘I’ ‘0’ ‘5’ ‘!’ cout << “I Love CS1005!”; 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!

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 handling library functions strcpy - copies one string to another strcat - appends one string to another strlen - returns the length of a string

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]; }

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

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 ‘H’ ‘e’ ‘l’ ‘o’ Sentence[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] ‘l’ ‘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"};

Choose word from array randomly const int Size = 5; const int WordSize = 10; char article [Size] [WordSize] = {"the", "a", "one", "some", "any"}; int index; srand((unsigned) time (NULL)); index = rand() % Size;

Copy word to sentence const int Size = 5; const int WordSize = 10; char article [Size] [WordSize] = {"the", "a", "one", "some", "any"}; char Sentence[6 * WordSize]; int index; srand((unsigned) time (NULL)); index = rand() % Size; strcpy (Sentence, article[index]);

Concatenate space after word const int Size = 5; const int WordSize = 10; char article [Size] [WordSize] = {"the", "a", "one", "some", "any"}; char Sentence[6 * WordSize]; int index; srand((unsigned) time (NULL)); index = rand() % Size; strcpy (Sentence, article[index]); strcat (Sentence, “ “);

Create sentence code 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"}; char Sentence[6 * WordSize]; int index; srand((unsigned) time (NULL));

Create sentence code (cont) for (int i = 0; i < 10; i++) { index = rand() % Size; strcpy (Sentence, article[index]); strcat (Sentence, " "); index = rand() % Size; strcat (Sentence, noun[index]); strcat (Sentence, " "); index = rand() % Size; strcat (Sentence, verb[index]); strcat (Sentence, " ");

Create sentence code (cont) index = rand() % Size; strcat (Sentence, preposition[index]); strcat (Sentence, " "); index = rand() % Size; strcat (Sentence, article[index]); strcat (Sentence, " "); index = rand() % Size; strcat (Sentence, noun[index]); cout << Sentence << endl << endl; }

Exercise char Sentence [6 * WordSize] contains a sentence to display. Capitalize the first letter of the sentence using the library function toupper. int toupper( int c );

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. !!

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;

inputting a string using cin const maxSize = 20; char inputMessage[maxSize]; cin >> setw(maxSize) >> inputMessage; inputMessage[maxSize -1] = NULL;

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.

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.