Friday, January 05, 2007 A few weeks of developing and testing can save a whole afternoon in the library. -Anonymous.

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 09 Strings, IDEs. METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 29, 2002.
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.
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.
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:
Chapter 10.
Thursday, January 04, 2007 I think and think for months and years. Ninety-nine times, the conclusion is false. The hundredth time I am right. - Albert.
1 CS 105 Lecture 8 Strings; Input Failure Mon, Mar 7, 2011, 3:39 pm.
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
1 String Library and Stream I/O Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures Series.
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.
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.
Introduction to Computers and Programming Class 22 Character Arrays (Strings) Professor Avi Rosenfeld.
Programming Strings. COMP102 Prog. Fundamentals: Strings / Slide 2 Character Strings l A sequence of characters is often referred to as a character “string”.
To remind us We finished the last day by introducing If statements Their structure was:::::::::
 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 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.
Strings. Sentences in English are implemented as strings in the C language. Computations involving strings are very common. E.g. – Is string_1 the same.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Arrays and Strings Gabriel Hugh Elkaim Spring 2013.
Chapter 5 Arrays and Strings C Programming © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Introduction to C programming
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.
Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
One Dimensional Arrays (Part2) Sorting Algorithms Searching Algorithms Character Strings The string Class. 1.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
9-1 Learning Objectives  An Array Type for Strings  C-Strings.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
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’};
String functions+ string I.Mona Alshehri. String Functions: Header file:#include Function: Int strlen(char s[n]) Description Calculates the length of.
Copyright © 2002, Department of Systems and Computer Engineering, Carleton University CONTROL STRUCTURES Simple If: if (boolean exp) { statements.
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
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.
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
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.
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.
5.6 String Processing Part 2. Sprintf(destnvar,…..regularprintf) Write formatted data to string Same as printf except the output is put in variable. A.
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.
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
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.
Fundamentals of Characters and Strings
C Stuff CS 2308.
String What it is Why it’s useful
Engineering Problem Solving with C++, Etter
Presentation transcript:

Friday, January 05, 2007 A few weeks of developing and testing can save a whole afternoon in the library. -Anonymous

Practice Exercise! §Write down the code to multiply two matrices of sizes NxM and MxQ respectively.

Strings §One dimensional arrays is used to create character strings §In C++, a string is defined as a character array that is terminated by a null §A null is specified using ‘\0’ and is zero §Because of the null terminator, it is necessary to declare a character to be one character longer than the largest string it will hold.

Character Arrays char word[6] = “fruit”;//word has size 6 char list[6] = {‘f’,’r’,’u’,’i’,’t’, ‘\0’}; //list of characters, not a string Not necessary to add null terminator at end of string constants.

Character Arrays char word[] = “fruit”; //word has size 6

§What is wrong with the following? char name[5]="class"; int MyArray[];

§cout<<'3'; §cout<<','; §cout<<'\n';

§'a' and "a" are different

Null String "" Only contains null terminator.

Reading strings from keyboard §The easiest way to read a string entered from the keyboard is to make a character array int main() { char str[80]; cout << "Enter a string: "; cin >> str; // read string from keyboard cout << "Here is your string: "; cout << str <<"\n"; return 0; }

Reading strings from keyboard §There is a problem with previous program, if the string has whitespace characters // Using gets() to read a string from the keyboard. #include int main() { char str[80]; cout << "Enter a string: "; cout<<flush; gets(str); // read a string from the keyboard //NOTE: gets takes array_name as parameter cout << "Here is your string: "; cout << str<<“\n”; return 0; }

§When you send output to cout, it does not necessarily get printed immediately. Rather, it may wait in a buffer until some event, e.g. buffer full enough, reading from input etc. §Forcing all buffered output to actually be printed is known as "flushing" the stream. A flush can be forced by inserting flush into the stream, or inserting endl. cout << flush; cout << endl;

char str[]="This is CS192"; int i=0; while(str[i]){ cout<<str[i]<<endl; i++; }

ThisisCS192ThisisCS192

What if I insert a zero? char str[]="This is CS092"; int i=0; while(str[i]){ cout<<str[i]<<endl; i++; }

char str[80]; do{ gets(str); cout<<str<<endl; }while(str[0]);

String Library Functions strcpy() strcat() strlen() strcmp() #include strcpy(to, from) int main() { char str[80]; strcpy(str, "hello"); cout << str; return 0; }

String Library Functions strcpy() strcat() strlen() strcmp() #include strcpy(to, from) int main() { char str[80]; strcpy(str, "hello"); cout << str; return 0; } Output is: hello

String Library Functions (strcat) int main() { char s1[20], s2[10]; strcpy(s1, "hello"); strcpy(s2, " there"); strcat(s1, s2); cout << s1<< "\n"; return 0; } //output?

Output is: hello there

String Library Functions (strcmp) § strcmp(s1,s2) compares two strings and returns 0 if they are equal. If s1 is greater than s2 (dictionary order) then a positive number is returned; if it is less than s2 then a negative number is returned. cout<<strcmp("abc", "def")<<endl; Prints -1

String Library Functions (strcmp) bool password(); int main() { if(password()) cout << "Logged on.\n"; else cout << "Access denied.\n"; return 0; }

String Library Functions (strcmp) // Return true if password accepted; false otherwise. bool password(){ char s[80]; cout << "Enter password:\n"; gets(s); // strings differ if(strcmp(s, "stringfunction")) { cout << "Invalid password.\n"; return false; } // strings compared the same return true; }

String Library Functions (strcmp) int main() { char s[80]; for(;;) { cout << "Enter a string: "; cout<<flush; gets(s); if(!strcmp("quit", s)) break; } return 0; }

String Library Functions (strlen) strlen(s) returns the length of s (not counting null terminator) int main() { char str[80]; cout << "Enter a string\n"; gets(str); cout << "Length is: " << strlen(str)<<"\n"; return 0; } //what is the output if we enter: this is cs192