Strings, Slide 1 310201 Fundamental Programming Strings.

Slides:



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

C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
 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.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
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:
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
Chapter 10.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
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.
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
Chapter 8 Arrays and Strings
String What it is Why it’s useful library routines for handling strings how to input a string from the keyboard.
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.
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
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.
February 14, 2005 Characters, Strings and the String Class.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
C++ PROGRAMMING: PROGRAM DESIGN INCLUDING DATA STRUCTURES, FIFTH EDITION Chapter 10: Strings and string type.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
Fundamental Programming: Fundamental Programming Introduction to C++
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
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 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
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:
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
Sahar Mosleh California State University San MarcosPage 1 Character String.
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. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
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.
Data Types Storage Size Domain of all possible values Operations 1.
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
CMSC 104, Version 8/061L25Strings.ppt Strings Topics String Libraries String Operations Sample Program Reading Sections
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
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.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
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.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
Introduction Programs which manipulate character data don’t usually just deal with single characters, but instead with collections of them (e.g. words,
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Fundamentals of Characters and Strings
Arrays in C.
C Stuff CS 2308.
Week 9 – Lesson 1 Arrays – Character Strings
Strings A collection of characters taken as a set:
String in C++.
String What it is Why it’s useful
Fundamental Programming
C++ Programming Lecture 20 Strings
Characters and Strings Functions
CS31 Discussion 1H Fall18: week 6
Presentation transcript:

Strings, Slide Fundamental Programming Strings

Strings, Slide 2 Strings in C++, names, titles, messages, etc, can be held as an array of characters - the term string describes this type of data in C++, the null character marks the end of a string (the “character” with value 0 in the ASCII coding system - '\0' in C++) as such, character arrays to hold strings must be declared one element larger than the longest string they will hold

Strings, Slide 3 Newer Compilers eg Visual C #include string myName = “Sue Smith”; text pages

Strings, Slide 4 Older Compilers eg Borland Turbo C++ #include char myName[20] = “Sue Smith”; text pages

Strings, Slide 5 the following statement declares a variable that can hold a string containing up to five characters char Name[5]; Declaring Character Arrays

Strings, Slide 6 the following statements all declare an array holding four characters – the last being the null character char Company[4] = {'I','B','M','\0'}; char Company[4] = "IBM"; char Company[] = "IBM"; in last two, a C++ string is used to initialise the array – note use of double quotes Initialising Character Arrays

Strings, Slide 7 initialising an array to the empty string can be achieved by: char Company[25] = "\0"; char Company[25] = {‘\0'}; the former is commonly used Initialising Character Arrays

Strings, Slide 8 if no room is left for the null character, unexpected results can be expected char Array1[3] = "11"; char Array2[3] = "222"; char Array3[3] = "33"; given the above, the statement below may produce the following output cout << “Array2: “ << Array2; Initialising Character Arrays Array2: 22233

Strings, Slide 9 String Input and Output output is as expected, input is not… #include void main (void) { char Sentence[80] = "\0"; cout "; cin.getline(Sentence,80); cout << ”Sentence: ” << Sentence << endl; } cin >> stops accepting input when it meets a white space character – getline does not

Strings, Slide 10 String Input and Output notice that… #include void main (void) { char Sentence[80] = "\0"; cout "; cin.getline(Sentence,80); cout << ”Sentence: ” << Sentence << endl; } up to 79 characters will be assigned to Sentence – position 79 will hold the null character

Strings, Slide 11 getline( ) Function getline(InputArray,ArraySize,UntilChar); InputArray is a character array variable that will receive the input characters ArraySize is an integer expression stating the size of the array – (a maximum of ArraySize–1 characters will be assigned to array, followed by null) UntilChar is an optional parameter; if this character is encountered, input is terminated (‘\n’ is the default)

Strings, Slide 12 Other Issues… we cannot simply assign the contents of one character array to another: CharArray1 = CharArray2; or combine two character arrays CharArray1 = CharArray2 + CharArray3; or compare two character arrays: if (CharArray1 == CharArray2)... use standard library functions instead...

Strings, Slide 13 string Library Functions assignment and comparison of character arrays is can be accomplished using… strcpy(Str1,Str2); // Str2 is copied to Str1 strcat(Str1,Str2); // Str2 is joined to Str1 strcmp(Str1,Str2); // Str2 is compared to Str1 strlen(Str1); // length of Str1 –to use these functions, your program must include the string library: #include

Strings, Slide 14 strcpy( ) Function Example #include void main (void) { char Array1[] = "111"; char Array2[] = "222"; cout << "Array1: " << Array1 << endl; strcpy(Array1,Array2); cout << "Array1: " << Array1 << endl; } Array1: 111 Array1: 222

Strings, Slide 15 strcat( ) Function Example #include void main (void) { char Array1[] = "111"; char Array2[] = "222"; char Array3[7] = "333"; cout << "Array3: " << Array3 << endl; strcat(Array3,Array2); cout << "Array3: " << Array3 << endl; } Array3: 333 Array3:

Strings, Slide 16 strcmp( ) Function Example using this Compare function… void Compare(char Array1[], char Array2[]) { char Relationship = '='; // establish relationship between strings if (strcmp(Array1,Array2) < 0) Relationship = '<'; else if (strcmp(Array1,Array2) > 0) Relationship = '>'; // report relationship between strings cout << "Array1(" << Array1 << ") " << Relationship << " Array2(" << Array2 << ")" << endl; }

Strings, Slide 17 strcmp( ) Function Example the program below… #include void Compare(char Array1[], char Array2[]); void main (void) { Compare("111","222"); Compare("111","122"); Compare("abc","ABC"); Compare("ABC","ABC"); Compare("ABCD","ABC"); }

Strings, Slide 18 strcmp( ) Function Example produces the following output… comparison is made using the ASCII value on characters in the string – see page 49 in the textbook Array1(111) < Array2(222) Array1(111) < Array2(122) Array1(abc) > Array2(ABC) Array1(ABC) = Array2(ABC) Array1(ABCD) > Array2(ABC)

Strings, Slide 19 ctype Library Functions other useful functions for handling characters and strings are… toascii(Char); // returns ACSII code for char toupper(Char); // returns upper case of char tolower(Char); // returns lower case of char to use these functions, your program must include the ctype library: #include

Strings, Slide 20 toascii( ) Function Example #include void main (void) { cout << "code for 1 is: " << toascii('1') << endl; cout << "code for 2 is: " << toascii('2') << endl; cout << "code for a is: " << toascii('a') << endl; cout << "code for A is: " << toascii('A') << endl; } code for 1 is: 49 code for 2 is: 50 code for a is: 97 code for A is: 65

Strings, Slide 21 toupper( ) Function Example #include void main (void) { cout << "upper case of a is: " << toupper('a') << endl; cout << "upper case of a is: " << char (toupper('a')) << endl; cout << "upper case of 1 is: " << char (toupper('1')) << endl; cout << "upper case of A is: " << char (toupper('A')) << endl; } upper case of a is: 65 upper case of a is: A upper case of 1 is: 1 upper case of A is: A

Strings, Slide 22 Summary Newer compilers use the string library Older compilers use string.h Arrays are used to hold strings Strings terminate in a null zero So arrays must be of sufficient size to hold the characters in the string, plus one Many string.h library functions exist to handle strings, eg strcat, strcpy, strlen, etc