Introduction to Algorithms and Data Structures. Lecture 6 - Stringing Along - Character and String Manipulation

Similar documents
A function is a named group of statements developed to solve a sub-problem and returns a value to other functions when it is called.

Characters, c-strings, and the string Class. CS 1: Problem Solving & Program Design Using C++

Scientific Programming in C V. Strings

Converting a Lowercase Letter Character to Uppercase (Or Vice Versa)

System Design and Programming II

LESSON 4. The DATA TYPE char

Chapter 8 C Characters and Strings

Chapter 8 - Characters and Strings

CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12

C: How to Program. Week /May/28

C++ Quick Reference. switch Statements

String Class in C++ When the above code is compiled and executed, it produces result something as follows: cin and strings

C++ Arrays. Arrays: The Basics. Areas for Discussion. Arrays: The Basics Strings and Arrays of Characters Array Parameters

Strings and Stream I/O

Strings and Library Functions

AIR FORCE SCHOOL,BAMRAULI COMPUTER SCIENCE (083) CLASS XI Split up Syllabus (Session ) Contents

Course organization. Course introduction ( Week 1)

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Fundamentals of Programming. Lecture 11: C Characters and Strings

2 nd Week Lecture Notes

Split up Syllabus (Session )

Introduction to string

Strings. Daily Puzzle

CSCE 110 PROGRAMMING FUNDAMENTALS

CSC 270 Survey of Programming Languages. C-String Values

BITG 1113: Array (Part 2) LECTURE 9

Chapter 10 Characters, Strings, and the string class

Discussion 1H Notes (Week 4, April 22) TA: Brian Choi Section Webpage:

Lab # 02. Basic Elements of C++ _ Part1

Chapter 10: Character Testing. From Program Character Case Conversion 8/23/2014. Character Testing. Character Case Conversion

Chapter 10: Characters, C- Strings, and More About the string Class

Structured programming

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

A First Program - Greeting.cpp

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Chapter 8: Character & String. In this chapter, you ll learn about;

Programming. C++ Basics

Strings in C++ Dr. Ferdin Joe John Joseph Kamnoetvidya Science Academy

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

CS31 Discussion 1E. Jie(Jay) Wang Week5 Oct.27

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

Exercise 1.1 Hello world

CCE1111 Programming for Engineers [C Programming Language]

CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING Chapter 6: One Dimensional Array

6.096 Introduction to C++ January (IAP) 2009

Chapter 10: Characters, C- Strings, and More About the string Class Character Testing

CS2141 Software Development using C/C++ C++ Basics

Introduction. What is function? Multiple functions form a larger program Modular programming

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100


CS 31 Review Sheet. Tau Beta Pi - Boelter Basics 2. 2 Working with Decimals 2. 4 Operators 3. 6 Constants 3.

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

C-String Library Functions

Characters and Strings

C++ basics Getting started with, and Data Types.

Objectives. In this chapter, you will:

today cs3157-fall2002-sklar-lect05 1

case control statement (switch case)

C Libraries. Bart Childs Complementary to the text(s)

CSE2301. Functions. Functions and Compiler Directives

Engineering Problem Solving with C++, Etter

int n = 10; int sum = 10; while (n > 1) { sum = sum + n; n--; } cout << "The sum of the integers 1 to 10 is " << sum << endl;

Introduction to Programming EC-105. Lecture 2

Chapter 1 INTRODUCTION

ONE DIMENSIONAL ARRAYS

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Unit 2: The string class

Part II Answer to all the questions (2 Marks):

1 Pointer Concepts. 1.1 Pointer Examples

Appendix A Developing a C Program on the UNIX system

CMSC 202 Midterm Exam 1 Fall 2015

ME240 Computation for Mechanical Engineering. Lecture 4. C++ Data Types

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

C mini reference. 5 Binary numbers 12

o Echo the input directly to the output o Put all lower-case letters in upper case o Put the first letter of each word in upper case

Fundamentals of Programming CS-110. Lecture 2

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Intermediate Programming, Spring 2017*

Unit 3, Lesson 2 Data Types, Arithmetic,Variables, Input, Constants, & Library Functions. Mr. Dave Clausen La Cañada High School

Chapter 15 - C++ As A "Better C"

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

LOGO BASIC ELEMENTS OF A COMPUTER PROGRAM

LECTURE 02 INTRODUCTION TO C++

What we will learn about this week:

CS 115 Exam 3, Spring 2010

Goals of this Lecture

Computer Programming

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

SYSC 2006 C Winter String Processing in C. D.L. Bailey, Systems and Computer Engineering, Carleton University

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

Iosif Ignat, Marius Joldoș Laboratory Guide 9. Character strings CHARACTER STRINGS

Review: Constants. Modules and Interfaces. Modules. Clients, Interfaces, Implementations. Client. Interface. Implementation

BITG 1233: Introduction to C++

what are strings today: strings strings: output strings: declaring and initializing what are strings and why to use them reading: textbook chapter 8

Transcription:

Introduction to Algorithms and Data Structures Lecture 6 - Stringing Along - Character and String Manipulation

What are Strings? Character data is stored as a numeric code that represents that particular character. The most commonly used code is ASCII (American Standard Code for Information Interchange). A collection of characters that are read and written together to form words, numbers and so on are called strings. While strings are a built-in data type in some programming languages, this is not the case in C++.

How does C/C++ handle strings? In C, strings are stored as arrays of characters, where the end of the string is marked with an ASCII value of 0 (the NUL character). This is usually written as \0. Manipulating strings in C requires a little skill and the use of a library of functions, which begin str.

The C String Library The string library includes several functions that are quite useful: strcpy(s, t) - copies the contents of t into s strcat(s, t) - concatenates the contents of t to the strlen(s) end of s - returns the length of s strcmp(s, t) - returns a positive integer if s follows t in collating sequence - returns a negative integer if s precedes t in collating sequence - returns 0 if s and t are equivalent

strcpy strcpy is used to assign a value to a character string. The correct format is: strcpy(target_string,source_string); The statement #include <string.h> must appear above the first appearance of strcpy.

An Example of strcpy #include #include <iostream.h> <string.h> // Necessary if you're // using strcpy const int stringlen = 10; // main() - Show 2 examples of strcpy int main(void) { //Declare two string variables char s[stringlen], t[stringlen];

cout << "Type a word\t?"; cin >> t; strcpy(s, t);// Copy t's contents into s cout << s << endl; //It also works with literal strings strcpy(t, "something else"); cout << t << endl; } return(0);

strcat strcat is used to concatenate the contents of one character string to the end of another string. The correct format is: strcat(target_string,source_string); As in strcpy, the source can be a variable or a literal and the statement #include <string.h> must appear above the first appearance of strcat.

An Example of strcat #include #include <iostream.h> <string.h> // Necessary if you're // using strcat const int stringlen = 10; char s[stringlen], t[stringlen]; // main() - Show 2 examples of strcpy int main(void) { //Initialize the two strings strcpy(s, "Some"); strcpy(t, "thing");

//Concatenate the string strcat(s, t); cout << s << endl; //Concatenate the string again strcat(s, " else"); cout << s << endl; } return(0); Output Something Something else

strlen Occasionally you need to know how long a string is. strlen returns (as an integer value) the length of the string: #include <iostream.h> #include <string.h> const int stringlen = 10; int { } char main(void) s[stringlen]; strcpy(s, "Some"); cout << strlen(s) << endl; return(0);

strcmp strcmp returns an integer value that tells you which string comes first in the collating sequence: strcmp(s, t) is positive if s comes first strcmp(s, t) is negative if t comes first strcmp(s, t) is 0 if s and t are equivalent

An Example ofstrcmp #include <iostream.h> #include <string.h> const int stringlen = 10; int main(void) { char s[stringlen], t[stringlen], r[stringlen]; strcpy(s, "some"); strcpy(t, "thing"); strcpy(r, "rat");

//Is s > t? cout << strcmp(s, t) << endl; //Is s > r? cout << strcmp(s, r) << endl; } return(0); The output: -1 1

The ctype.h Library Another C/C++ library that is very useful is declared in the header file ctype.h. Most of the functions return a boolean value determining whether the character is of a certain type. These functions include: isalnum (c) true if c is alphanumeric isalpha (c) true if c is a letter (of either case) isdigit (c) true if c is a digit (0 through 9) islower (c) true if c is a lower case letter isspace (c) true if c is a white space character isupper (c) true if c is an upper case letter

isalpha(c) isalpha returns true if the character passed as a parameter is a letter; otherwise it returns false. The preprocessor #include <ctype.h> must appear above the first use of isalpha.

An Example of isalpha(c) #include #include isalpha <iostream.h> <ctype.h> // Necessary for using int main(void) { char c; } // Keep asking until you get a letter do { cout << "Enter a letter\t?"; cin >> c; } while (!isalpha(c)); return(0);

islower (c) islower returns true if the character parameter is a lower case letter; in all other cases (including upper case letters), it returns false. islower requires #include <ctype.h> islower allows the user to treat lower case and upper case letters differently.

An Example of islower #include <iostream.h> #include <ctype.h> // Necessary for using islower const int stringlen = 25; void allupper(char s[]); //A function that uses islower

// main() - A driver for allupper int main(void) { char s[stringlen]; //Input the word, convert to upper case // and print cout << "Type a word\t?"; cin >> s; allupper(s); cout << s << endl; } return(0);

// allupper() - Convert a string to entirely // upper case void allupper(char s[]) { int i; } // Convert any lower case character // to upper case for (i = 0; s[i]!= 0; i++) if (islower(s[i])) s[i] = s[i] - 'a' + 'A';

isupper(c) isupper returns true if the character parameter is a upper case letter; in all other cases (including lower case letters), it returns false. isupper requires #include <ctype.h> isupper allows the user to treat lower case and upper case letters differently.

An Example of isupper() // alllower() - Convert a string to entirely // lower case void allupper(char s[]) { int i; } // Convert any upper case character // to lower case for (i = 0; s[i]!= 0; i++) if (isupper(s[i])) s[i] = s[i] - A' + a';

isdigit(c) isdigit returns true if the character parameter is a digit; in all other cases, it returns false. isdigit requires #include <ctype.h> isdigit allows the user to read in a numeric input as a string, check to ensure it s a number and then convert it to a numeric data type. The string can be converted to int using atoi or to float using atof.

An Example using isdigit #include <iostream.h> // Necessary for using isdigit #include <ctype.h> // Necessary for using itoa #include <stdlib.h> const int stringlen = 25; int allnumeric(char s[]); // Is the number // solely digits?

// main() - A driver for allnumeric int main(void) { char numberstring[stringlen]; int x = 9999; //Input the word, convert to upper case // and print cout << "Type a number\t?"; cin >> numberstring; if (allnumeric(numberstring)) x = atoi(numberstring); cout << numberstring << " is equal to << x << endl; } return(0);

enum bool {false, true}; // Turbo C++ needs this // allnumeric() - Is the number solely digits? int allnumeric(char s[]) { bool answer = false; int i; for (i = 0; s[i]!= '\0'; i++) if (!isdigit(s[i])) break; if (s[i] == '\0') return(true); } return(false);

isalnum(c) isalum returns true if the parameter is either alphabetic or numeric (hence the name). It is useful in writing text scanners to spot variable names or in database programs to spot item codes.

isspace(c) isspace returns true if the parameter is a white space character (blank, newline or tab). It helps in spotting where words begin and end.

toupper(c)and tolower(c) A programmer needn t write their own function to convert characters from lower case to upper case or vice versa. There are standard functions in the ctype.h to do this: toupper(c) converts a lower case character to upper case; leaves it alone if it is anything else. tolower(c) converts an upper case character to lower case; leaves it alone if it is anything else.

An Example Using toupper(c)and tolower(c) #include #include <iostream.h> <ctype.h> //Handles up to a line of text const int stringlen = 81; // A prototype for the function void changecase(char s[]);

// main() - A driver program for ChangeCase int main(void) { char instring[stringlen]; //Read a word from the keyboard cout << "Enter a word\t?"; cin >> instring; } // Change the case of every letter and print changecase(instring); cout << "\"" << instring << "\"" << endl; return(0);

// ChangeCase() - Change the case of every // character in a string void changecase(char s[]) { int i; } // Character by character, look for lower case // character to convert to upper case; // otherwise convert to lower case for (i = 0; s[i]!='\0'; i++) if (islower(s[i])) s[i] = toupper(s[i]); else s[i] = tolower(s[i]);

The C++ String Class

A Very Simple String Example // Please note that there is no ".h" #include <iostream> #include <string> // Make the computer aware of standard // identifiers such as string using namespace std; int main(void) { string s = "concatenate"; } cout << s << endl; return(0);

Another Very Simple String Example #include #include <iostream> <string> using namespace std; int main(void) { string s("concatenate"), t; t = s; cout << t << endl; } return(0);

Initializing, Reading and Writing Strings

An Example of String Input/Output #include #include <iostream> <string> using namespace std; int main(void) { string s, t; //Prompt the user - read in two strings // separated by white space cout << "Enter two words with a space in << between\t?";

cin >> s; cin >> t; } cout << "\"" << s << "\"\t\"" << t << "\"" << endl; return(0);

Concatenating and Trimming Strings

An Example of Concatenation #include #include <iostream> <string> using namespace std; // Concatenating - Joining two character strings int main(void) { string r, s, t; // Prompt the user and read two words cout << "Enter a word\t?"; cin >> r;

cout << "Enter another word\t?"; cin >> s; } // Concatentate the words with a // blank in the middle t = r + " " + s; cout << "\"" << t << "\"\t" << endl; return(0);

Searching For a Substring

Searching for One of a Set of Characters

Retrieving A Single Character

Example: Writing Changing a Form Letter