DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Advertisements

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.
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.
Chapter 10.
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.
N-1 University of Washington Computer Programming I Lecture 19: Strings © 2000 UW CSE.
Friday, January 05, 2007 A few weeks of developing and testing can save a whole afternoon in the library. -Anonymous.
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.
To remind us We finished the last day by introducing If statements Their structure was:::::::::
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
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.
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.
Chapter 5 Arrays and Strings C Programming © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
CHAPTER 8 CHARACTER AND STRINGS
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.
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
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.
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
Chapter 8 Arrays and Strings
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.
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.
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.
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’};
Characters, Strings, And The string Class Chapter 10.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
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.
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
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
© Oxford University Press All rights reserved. CHAPTER 6 STRINGS.
C++ String Class nalhareqi©2012. string u The string is any sequence of characters u To use strings, you need to include the header u The string is one.
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
CSC 270 – Survey of Programming Languages
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 10 Characters, Strings, and the string class.
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
DCT1063 Programming 2 CHAPTER 1 POINTERS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
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.
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.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Strings, Pointers and Tools
Strings, Slide Fundamental Programming Strings.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
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.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
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.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
C Stuff CS 2308.
String What it is Why it’s useful
CPS120: Introduction to Computer Science
Chapter 12: More on C-Strings and the string Class
Presentation transcript:

DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College

Strings In C++, a string is defined as a character array that is terminated by a null. A null character is specified using '\0', and is zero. For example, if you want to declare an array str that could hold a 10-character string, you would write: char str[11]=“ABCDEFGHIJ”; //Specifying the size as 11 makes room for the null at the end of the string.

Strings(2) C++ allows you to define a string literal. Here are some examples: 1."hello there" 2."I like C++" 4. "“ The last string contains only the null terminator, and no other characters. It is not necessary to manually add the null onto the end of string constants; the C++ compiler does this for you automatically.

Reading a String from the Keyboard The easiest way to read a string entered from the keyboard is to make the array that will receive the string the target of a cin statement. For example, the following program reads a string entered by the user: Although this program is technically correct, there is still a problem. To see what it is, examine the following sample run. Enter a string: This is a test Here is your string: This

// Using cin to read a string from the keyboard. #include using namespace std; int main() { char str[80]; cout << "Enter a string: "; cin >> str; // read string from keyboard cout << "Here is your string: "; cout << str; return 0; }

Reading a String from the Keyboard(2) The >> operator stops reading a string when the first whitespace character is encountered. Whitespace characters include spaces, tabs, and newlines. One way to solve the whitespace problem is to use another of C++’s library functions, gets( ). The general form of a gets( ) call is: gets(array-name);

Reading a String from the Keyboard(3) The gets( ) function will continue to read characters until you press ENTER. The header used by gets( ) is. This version of the preceding program uses gets( ) to allow the entry of strings containing spaces. Now, when you run the program and enter the string "This is a test", the entire sentence is read and then displayed, as this sample run shows. Enter a string: This is a test Here is your string: This is a test

// Using gets() to read a string from the keyboard. #include using namespace std; int main() { char str[80]; cout << "Enter a string: "; gets(str); // read a string from the keyboard cout << "Here is your string: "; cout << str; return 0; }

Some String Library Functions C++ supports a wide range of string- manipulation functions. The most common are:- 1.strcpy( ) 2.strcat( ) 3.strlen( ) 4.strcmp( ) The string functions all use the same header,. Let’s take a look at these functions now.

Some String Library Functions(2) strcpy –A call to strcpy( ) takes this general form: –strcpy(to, from); –The strcpy( ) function copies the contents of the string from into to. –The following program will copy "hello" into string str: #include using namespace std; int main() { char str[80]; strcpy(str, "hello"); cout << str; return 0; }

Some String Library Functions(3) strcat –A call to strcat( ) takes this form: –strcat(s1, s2); –The strcat( ) function appends s2 to the end of s1; s2 is unchanged. –Both strings must be null-terminated, and the result is null-terminated. –The following program will print hello there on the screen:

#include using namespace std; int main() { char s1[20], s2[10]; strcpy(s1, "hello"); strcpy(s2, " there"); strcat(s1, s2); cout << s1; return 0; }

Some String Library Functions(4) strcmp –A call to strcmp( ) takes this general form: –strcmp(s1, s2); –The strcmp( ) function compares two strings and returns 1.0 if they are equal. 2.Positive number if s1 is greater than s2 3.Negative number if s1 is less than s2 –The password( ) function, shown in the following program, is a password-verification routine. It uses strcmp( ) to check a user’s input against a password.

#include using namespace std; bool password(); int main() { if(password()) cout << "Logged on.\n"; else cout << "Access denied.\n"; return 0; } // Return true if password accepted; false otherwise. bool password() { char s[80]; cout << "Enter password: "; gets(s);

if(strcmp(s, "password")) { // strings differ cout << "Invalid password.\n"; return false; } // strings compared the same return true; }

Some String Library Functions(4) strlen –The general form of a call to strlen( ) is strlen(s); –where s is a string. –The strlen( ) function returns the length of the string pointed to by s. –Consider the following program.

#include using namespace std; int main() { char str[80]; cout << "Enter a string: "; gets(str); #include using namespace std; int main() { char str[80]; cout << "Enter a string: "; gets(str);

Strlen(cont) –If the user enters the string "Hi there", this program will display 8. The null terminator is not counted by strlen( ). –When the following program is run, the string entered at the keyboard is printed in reverse. For example, "hello" will be displayed as olleh.

// Print a string backwards. #include using namespace std; int main() { char str[80]; int i; cout << "Enter a string: "; gets(str); // Print the string in reverse. for(i=strlen(str)-1; i>=0; i--) cout << str[i]; return 0; }

Some String Library Functions(5) As a final example, the following program illustrates the use of all four string functions: #include using namespace std; int main() { char s1[80], s2[80]; cout << "Enter two strings: "; gets(s1); gets(s2); cout << "lengths: " << strlen(s1); cout << ' ' << strlen(s2) << '\n';

if(!strcmp(s1, s2)) cout << "The strings are equal\n"; else cout << "not equal\n"; strcat(s1, s2); cout << s1 << '\n'; strcpy(s1, s2); cout << s1 << " and " << s2 << ' '; cout << "are now the same\n"; return 0; }

Some String Library Functions(6) If this program is run and the strings "hello" and "there" are entered, then the output will be lengths: 5 5 not equal hellothere there and there are now the same Remember that strcmp( ) returns false if the strings are equal. This is why you must use the ! operator to reverse the condition, as shown in the preceding example, if you are testing for equality.

Using the Null Terminator All strings are null-terminated -can often be used to simplify various operations. Example-how little code is required to uppercase every character in a string. This program will print THIS IS A TEST. It uses the library function toupper( ). Notice that the test condition of the for loop is simply the array indexed by the control variable. This works because a true value is any non-zero value.

// Convert a string to uppercase. #include using namespace std; int main() { char str[80]; int i; strcpy(str, "this is a test"); for(i=0; str[i]; i++) str[i] = toupper(str[i]); cout << str; return 0; }

Summary The subscript operator, [], provides read/write access to any element of a string. string member function strcmp() compares two strings (or substrings) and returns 0 if the strings are equal, a positive number if the first string is lexicographically greater than the second or a negative number if the first string is lexicographically less than the second. string member functions strlen() return the size or length of a string (i.e., the number of characters currently stored in the string).