An AnsiString class reference

Size: px
Start display at page:

Download "An AnsiString class reference"

Transcription

1 1 de 9 25/08/ :04 August 1997 An AnsiString class reference by Kent Reisdorph The AnsiString class (or String, for short) that comes with C++Builder is designed to work and play well with VCL. However, the documentation for the String class is somewhat lacking--ok, it's terrible. We'll do our best to remedy that situation in this article by describing the most commonly used String class operators, constructors, and functions. (We won't deal with wide-character support functions, however.) Constructors The String class includes constructors to let you create a String object from different data types. Table A shows the constructors and provides an example of each constructor's use. Table A: String class constructors Constructor Description Example AnsiString(); Constructs an empty String object AnsiString( const char* src); AnsiString( const AnsiString& src); AnsiString( const char* src, unsigned char len); AnsiString(char src); AnsiString(int src); AnsiString(double src); Constructs a String object from a char* (a character array) Constructs a String object from another String object Constructs a String object from a char* with the length specified in len Constructs a String object from a char data type Constructs a String object from an int data type Constructs a String object from a double data type String ("This is a test"); char* buff = "Hello"; String s2(buff); String s1 = "String One"; String s2(s1); String s("", 255); char c = '\n'; String lf(c); int x = 200; String s(x); double d = 24.53; String s(d); Operators Table B shows the String operators and gives an example of each. These operators let you perform operations on strings using common-sense syntax rather than relying on cryptic function names. Table B: String class operators Operator Description Example = Assignment s = "Test"; + Concatentation s = path + filename; == Equality if (s == "Hello World")...!= Inequality if (s!= "Logon")... < Less than if (s < "a")... > Greater than if (s > "z")...

2 2 de 9 25/08/ :04 <= Less than or = if (s <= "1")... >= Greater than or = if (s > = "10")... [ ] Index Label1->Caption = s[10]; Primary functions The String class's many functions let you manipulate strings just about any way you choose. We'll describe the functions you'll probably use most often with the String class. AnsiCompare() The AnsiCompare() function compares one string to another, using the following syntax: int AnsiCompare(const AnsiString& rhs); The return value will be -1 if the string is less than rhs, 1 if the string is greater than rhs, and 0 if the string's contents equal rhs. You don't use this function to test for equality, but rather to sort strings. For example, String s1 = "Adam"; int result = s1.ansicompare("bob"); In this case, result will be -1 because A is less than B. You can also use the < and > operators to sort strings. AnsiCompareIC() The AnsiCompareIC() function compares one string to another without regard to case sensitivity, using the following syntax: int AnsiCompareIC(const AnsiString& rhs); AnsiLastChar() The AnsiLastChar() function uses the syntax char* AnsiLastChar(); to return the last character in a string. For example, the code String s = "This is a test"; Label1->Text = s.ansilastchar(); displays t in the Label component. c_str() The c_str() function returns a pointer to the character array used to hold the string's text. It uses this syntax: char* fastcall c_str(); You must use this function any time you need a char pointer to the string's text. For example, the Windows API function DrawText() requires a pointer to a character buffer. In such a case, you'll need to use the c_str() function as follows:

3 3 de 9 25/08/ :04 DrawText(Canvas->Handle, s.c_str(), s.length(), &rect, DT_CALCRECT); The c_str() function is also handy if you need to use a String object with the C-style string manipulation functions, such as strcpy(). Delete() The Delete() function deletes count characters from the string beginning at index. The function's syntax is as follows: void Delete(int index, int count); In this example, String s = "This is a test"; s.delete(1, 5); Label1->Caption = s; the label will display is a test, since the code deletes the first five characters of the original string. Remember that the string indexing is 1-based, not 0-based. Insert() The Insert() function uses the syntax void Insert(const AnsiString& str, int index); to insert the string str starting at position index. For example, if you use the code String s = "data.txt"; s.insert("c:\\myprog\\", 1); s will contain the text c:\myprog\data.txt after the call to Insert(). IsEmpty() The IsEmpty() function has this syntax: bool IsEmpty(); The function returns true if the string is empty and false if the string contains text. IsPathDelimiter() The IsPathDelimiter() function returns true if the character at the specified index is a backslash and false if it isn't. The function's syntax is as follows: bool IsPathDelimiter(int index); IsPathDelimiter() could be useful for traversing a string containing a path and filename. LastDelimiter() The LastDelimiter() function uses the following syntax: int LastDelimiter(const AnsiString& delimiters); You can use this function to find the last character specified in delimiters. For instance, to extract just the path from a string containing a path and filename, you could use these

4 4 de 9 25/08/ :04 lines: String s = "c:\\myprog\\data.txt"; int pos = s.lastdelimiter("\\"); s.delete(1, pos); This code finds the last backslash in the string and then deletes everything from the beginning of the string to that position. Length() The Length() function returns the number of characters in the string. Its syntax is: int Length(); The returned value will be the length of the text in the string, unless you've modified the length using the SetLength() function. The returned value doesn't include a terminating null character. For example, you could use these lines: String s = "Hello World"; int length = s.length(); In this case, length will contain the value 11, since there are 11 actual characters in the text string. LowerCase() The LowerCase() function uses the syntax AnsiString LowerCase(); This function converts the string to lowercase and returns the converted string. (However, it doesn't modify the text in the string object.) LowerCase() is useful for converting a string to lowercase (such as strings containing filenames) or for comparing two strings regardless of case. For example, in the line if (s1.lowercase() == s2.lowercase()) DoSomething(); the code doesn't modify the text in the strings--it simply compares the strings' lowercase representations for equality. If you actually want to convert a string to lowercase you'll need to use code like the following: String s = "TEST"; s = s.lowercase(); Pos() The Pos() function uses the following syntax: int Pos(const AnsiString& substr); It returns the position of the string passed in substr. If the string can't be found, then Pos() returns 0. The AnsiPos() functions perform exactly the same way. The following code finds the position of a substring within the target string: String s = "This is a test"; int p = s.pos("test"); Label1->Caption = "Pos: " + String(p);

5 5 de 9 25/08/ :04 This code will display the text Pos: 11 in the label component. Again, remember that the string index is 1-based, not 0-based. SetLength() You can use the SetLength() function to set the length of the buffer holding the string object's text. Its syntax is as follows: void SetLength(int newlength); Normally, the String class dynamically allocates space for the text in the string as needed, so there's rarely any reason to set the length yourself. For example: // length is 0 s = "test"; // length is 4 s.setlength(50); // length is 50 s = "new string"; // length is 10 If you set the length to a value less than the current length of the text in the string, then the string will be truncated. StringOfChar() The StringOfChar() function has the following syntax: AnsiString StringOfChar(char ch, int count); This function creates a string filled with the character ch, with a length of count. For example, the following code fills a string with 20 spaces: s = s.stringofchar(' ', 20); SubString() The SubString() function uses this syntax: AnsiString SubString(int index, int count); It returns a substring of the string object starting at index and having a length of count. For example, if you execute the code String s = "My name is BillyBob"; String sub = s.substring(12, 8); then the value of sub will be BillyBob. ToDouble() The ToDouble() function converts a string to a double and returns the result. The syntax is simply double ToDouble(); If the string can't be converted to a double, then VCL throws an EConvertError exception. Here's an example: String s = " "; double d = s.todouble(); d *=.3; In order for the string to be converted, it must contain a valid floating-point number and no alpha characters. ToInt()

6 6 de 9 25/08/ :04 The ToInt() function converts a string to an integer value and returns the result. The syntax is as follows: int ToInt(); For example, String s = "123"; int i = s.toint(); If the string can't be converted, then VCL throws an EConvertError exception. ToIntDef() The ToIntDef() function converts a string to an integer value and returns the result. Its syntax is int ToIntDef(int defaultvalue); If the string can't be converted, then the function returns the value supplied in defaultvalue; VCL doesn't throw an exception. Trim(), TrimLeft(), TrimRight() The three Trim functions trim blank spaces from a string object and return the result, using the following syntaxes: AnsiString Trim(); AnsiString TrimLeft(); AnsiString TrimRight(); The Trim() function removes both leading and trailing spaces, TrimLeft() removes leading spaces, and TrimRight() removes only trailing spaces. These functions don't modify the text in the string object. In this example, String edittext = Edit1->Text; edittext = edittext.trim(); Properties such as Caption, Title, and Text are String properties. As a result, you can condense the example code to a single line: String edittext = Edit1->Text.Trim(); UpperCase() The UpperCase() function converts a string to upper- case and returns the result. In the syntax AnsiString UpperCase(); the string's text isn't modified as in the example s = s.uppercase(); Other functions Now, let's take a look at some useful utility functions. You may use these functions from time to time in your work with the String class. CurrToStr() The CurrToStr() function converts a currency value to a string and returns the string. The function uses

7 7 de 9 25/08/ :04 the syntax AnsiString CurrToStr(Currency value); which doesn't change the string object for which you call it, but rather formats and returns a string. For example, the lines Currency c(19.95); c *= 1.06; // add sales tax Label1->Caption = s.currtostr(c); won't modify the String object s. CurrToStrF() The CurrToStrF() function uses this syntax: AnsiString CurrToStrF(Currency value, TStringFloatFormat format, int digits); It converts a currency value to a string, applies formatting, and returns the string. The lines Currency c(19.95); c *= 1.06; // add sales tax Label1->Caption = s.currtostrf(c, AnsiString::sffCurrency, 2); display the text $21.15 in the label. The text is formatted for currency, including the dollar sign and two decimal places. FloatToStrF() The FloatToStrF() function formats and returns a floating point number, using this syntax: AnsiString FloatToStrF( long double value, TStringFloatFormat format, int precision, int digits); The resulting string is based on the format, precision, and digits parameters. For example, the lines float f = ; Label1->Caption = s.floattostrf(f, AnsiString::sffExponent, 8, 0); format the given floating point value in scientific notation using eight-digit precision. Format() The Format() function builds a string using a format string and the supplied arguments. The syntax is as follows: AnsiString Format(const AnsiString& format,const TVarRec *args, int size); This function works in a way similar to the sprintf() and wsprintf() functions in C++. For example, the lines String s = Format("%s%d, %d", OPENARRAY(TVarRec, ("Values: ", 10, 20))); Label1->Caption = s; display the string Values: 10, 20 in the label. Notice that the OPENARRAY macro passes the values to the Format() function. This process is necessary because C++ doesn't have open arrays--however,

8 8 de 9 25/08/ :04 AnsiString::Format() calls the VCL version of Format(), which takes an open array as a parameter. The Format() function is one of the few cases in which Pascal and C++ just don't work well together. You can use one of two alternative methods for building the string from the previous example: // example 1 char buff[20]; sprintf(buff, "Value: %d, %d", 10, 20); String s = buff; // example 2 String s = "Value: " + String(10) + ", " + String(20); In my humble opinion, these methods are easier to use than the Format() function. FmtLoadStr() The FmtLoadStr() function is a combination of the Format() and LoadStr() functions. The syntax AnsiString FmtLoadStr(int ident, const TVarRec *args, int size); loads the string resource ident and formats it according to the value of the args parameter. See the descriptions of Format() and LoadStr() for more information. IsDelimiter() The IsDelimiter() function uses this syntax: bool IsDelimiter(const AnsiString& delimiters, int index); The function returns true if the character at index matches one of the characters in the string delimiters. Here's an example: String s = "c:\\myprog\\data.txt"; if (s.isdelimiter("\\", 3)) DoSomething(); The delimiters parameter can be a string of several delimiters you want to test against. For instance, if you wanted to check to see whether the character at position 10 was a space, a minus sign, a plus sign, a comma, or a dollar sign, you'd use this code: String s = "This is a string"; bool test = s.isdelimiter(" -+,$", 10); LoadStr() The LoadStr() function loads a string resource using the following syntax: AnsiString LoadStr(int ident); Of course, you must have a string table resource bound to your executable program, as does the following example: s = s.loadstr(100); //load string with ID of 100 Poor class design? Note that the CurrToStr(), CurrToStrF(), FloatToStrF(), Format(), FormatFloat(), FrmtLoadString(), and LoadStr() functions really don't belong in the AnsiString class at all. They don't operate on the string object for which you call them, nor do they use the string object in any way. In addition, these functions are provided as standalone functions in the VCL Sysutils unit. It's always easier to use the standalone version of these functions rather than the AnsiString version, as shown in these two examples:

9 9 de 9 25/08/ :04 // method 1 s = s.loadstr(id_string1); // method 2 String s = LoadStr(ID_STRING1); I suspect that Borland put these functions into the AnsiString class at some early point in C++Builder's development, then forgot them later, when they were no longer necessary. Regardless, they're part of the AnsiString class and they're useful functions, so we've listed them here. Kent Reisdorph is a editor of the C++Builder Developer's Journal as well as director of systems and services at TurboPower Software Company, and a member of TeamB, Borland's volunteer online support group. He's the author of Teach Yourself C++Builder in 21 Days and Teach Yourself C++Builder in 14 Days. You can contact Kent at editor@bridgespublishing.com.

AnsiString. Document :: AnsiString Version 4 Author :: Jon Jenkinson, (With much credit to Kent Reisdorph for his advice and clarification)

AnsiString. Document :: AnsiString Version 4 Author :: Jon Jenkinson, (With much credit to Kent Reisdorph for his advice and clarification) Document :: AnsiString Version 4 Author :: Jon Jenkinson, (With much credit to Kent Reisdorph for his advice and clarification) From "The Bits" Website http://www.cbuilder.dthomas.co.uk email : jon.jenkinson@mcmail.com

More information

static CS106L Spring 2009 Handout #21 May 12, 2009 Introduction

static CS106L Spring 2009 Handout #21 May 12, 2009 Introduction CS106L Spring 2009 Handout #21 May 12, 2009 static Introduction Most of the time, you'll design classes so that any two instances of that class are independent. That is, if you have two objects one and

More information

Slide 1 CS 170 Java Programming 1 More on Strings Duration: 00:00:47 Advance mode: Auto

Slide 1 CS 170 Java Programming 1 More on Strings Duration: 00:00:47 Advance mode: Auto CS 170 Java Programming 1 More on Strings Working with the String class Slide 1 CS 170 Java Programming 1 More on Strings Duration: 00:00:47 What are Strings in Java? Immutable sequences of 0 n characters

More information

ANSI C Reserved Words. Abstract Data Type: complex. Arrays. Multimedia Programming Lecture 5

ANSI C Reserved Words. Abstract Data Type: complex. Arrays. Multimedia Programming Lecture 5 Multimedia Programming 2004 Lecture 5 Erwin M. Bakker Joachim Rijsdam ANSI C Reserved Words auto, break, case, char, const, continue, default, do, double, else, enum, extern, float, for, goto, if, int,

More information

std::string Quick Reference Card Last Revised: August 18, 2013 Copyright 2013 by Peter Chapin

std::string Quick Reference Card Last Revised: August 18, 2013 Copyright 2013 by Peter Chapin std::string Quick Reference Card Last Revised: August 18, 2013 Copyright 2013 by Peter Chapin Permission is granted to copy and distribute freely, for any purpose, provided the copyright notice above is

More information

Programming Language 2 (PL2)

Programming Language 2 (PL2) Programming Language 2 (PL2) 337.1.1 - Explain rules for constructing various variable types of language 337.1.2 Identify the use of arithmetical and logical operators 337.1.3 Explain the rules of language

More information

Pointers, Dynamic Data, and Reference Types

Pointers, Dynamic Data, and Reference Types Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation The new operator The delete operator Dynamic Memory Allocation for Arrays 1 C++ Data Types simple

More information

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Object oriented programming. Instructor: Masoud Asghari Web page:   Ch: 3 Object oriented programming Instructor: Masoud Asghari Web page: http://www.masses.ir/lectures/oops2017sut Ch: 3 1 In this slide We follow: https://docs.oracle.com/javase/tutorial/index.html Trail: Learning

More information

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University ITC213: STRUCTURED PROGRAMMING Bhaskar Shrestha National College of Computer Studies Tribhuvan University Lecture 07: Data Input and Output Readings: Chapter 4 Input /Output Operations A program needs

More information

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

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program Objectives Chapter 2: Basic Elements of C++ In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

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

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction Chapter 2: Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 1 Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

Dynamic arrays / C Strings

Dynamic arrays / C Strings Dynamic arrays / C Strings Dynamic arrays syntax why use C strings Ex: command line arguments Call-by-pointer Dynamic Arrays / C Strings [Bono] 1 Announcements Final exam: Tue, 5/8, 8 10 am SGM 124 and

More information

Chapter 2 THE STRUCTURE OF C LANGUAGE

Chapter 2 THE STRUCTURE OF C LANGUAGE Lecture # 5 Chapter 2 THE STRUCTURE OF C LANGUAGE 1 Compiled by SIA CHEE KIONG DEPARTMENT OF MATERIAL AND DESIGN ENGINEERING FACULTY OF MECHANICAL AND MANUFACTURING ENGINEERING Contents Introduction to

More information

Chapter 1 Getting Started

Chapter 1 Getting Started Chapter 1 Getting Started The C# class Just like all object oriented programming languages, C# supports the concept of a class. A class is a little like a data structure in that it aggregates different

More information

STRINGS AND STRINGBUILDERS. Spring 2019

STRINGS AND STRINGBUILDERS. Spring 2019 STRINGS AND STRINGBUILDERS Spring 2019 STRING BASICS In Java, a string is an object. Three important pre-built classes used in string processing: the String class used to create and store immutable strings

More information

Python Working with files. May 4, 2017

Python Working with files. May 4, 2017 Python Working with files May 4, 2017 So far, everything we have done in Python was using in-memory operations. After closing the Python interpreter or after the script was done, all our input and output

More information

Deep Dive: Pronto Transformations Reference

Deep Dive: Pronto Transformations Reference Deep Dive: Pronto Transformations Reference Available Transformations and Their Icons Transform Description Menu Icon Add Column on page 2 Important: Not available in Trial. Upgrade to Pro Edition! Add

More information

Documentation Requirements Computer Science 2334 Spring 2016

Documentation Requirements Computer Science 2334 Spring 2016 Overview: Documentation Requirements Computer Science 2334 Spring 2016 These requirements are based on official Java coding conventions but have been adapted to be more appropriate to an academic environment.

More information

Embedding Python in Your C Programs

Embedding Python in Your C Programs 1 of 7 6/18/2006 9:05 PM Embedding Python in Your C Programs William Nagel Abstract C, meet Python. Python, this is C. With surprisingly little effort, the Python interpreter can be integrated into your

More information

Annotation Annotation or block comments Provide high-level description and documentation of section of code More detail than simple comments

Annotation Annotation or block comments Provide high-level description and documentation of section of code More detail than simple comments Variables, Data Types, and More Introduction In this lesson will introduce and study C annotation and comments C variables Identifiers C data types First thoughts on good coding style Declarations vs.

More information

Slide 1 CS 170 Java Programming 1 Real Numbers Duration: 00:00:54 Advance mode: Auto

Slide 1 CS 170 Java Programming 1 Real Numbers Duration: 00:00:54 Advance mode: Auto CS 170 Java Programming 1 Real Numbers Understanding Java's Floating Point Primitive Types Slide 1 CS 170 Java Programming 1 Real Numbers Duration: 00:00:54 Floating-point types Can hold a fractional amount

More information

INTRODUCTION 1 AND REVIEW

INTRODUCTION 1 AND REVIEW INTRODUTION 1 AND REVIEW hapter SYS-ED/ OMPUTER EDUATION TEHNIQUES, IN. Programming: Advanced Objectives You will learn: Program structure. Program statements. Datatypes. Pointers. Arrays. Structures.

More information

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved. Java How to Program, 10/e Education, Inc. All Rights Reserved. Each class you create becomes a new type that can be used to declare variables and create objects. You can declare new classes as needed;

More information

Working with Strings. Lecture 2. Hartmut Kaiser. hkaiser/spring_2015/csc1254.html

Working with Strings. Lecture 2. Hartmut Kaiser.  hkaiser/spring_2015/csc1254.html Working with Strings Lecture 2 Hartmut Kaiser hkaiser@cct.lsu.edu http://www.cct.lsu.edu/ hkaiser/spring_2015/csc1254.html Abstract This lecture will look at strings. What are strings? How can we input/output

More information

Strings. Strings and their methods. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

Strings. Strings and their methods. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics Strings Strings and their methods Produced by: Mairead Meagher Dr. Siobhán Drohan Department of Computing and Mathematics http://www.wit.ie/ Topics list Primitive Types: char Object Types: String Primitive

More information

LECTURE 15. String I/O and cstring library

LECTURE 15. String I/O and cstring library LECTURE 15 String I/O and cstring library RECAP Recall that a C-style string is a character array that ends with the null character. Character literals in single quotes: 'a', '\n', '$ String literals in

More information

Object Oriented Programming COP3330 / CGS5409

Object Oriented Programming COP3330 / CGS5409 Object Oriented Programming COP3330 / CGS5409 Dynamic Allocation in Classes Review of CStrings Allocate dynamic space with operator new, which returns address of the allocated item. Store in a pointer:

More information

https://www.eskimo.com/~scs/cclass/notes/sx8.html

https://www.eskimo.com/~scs/cclass/notes/sx8.html 1 de 6 20-10-2015 10:41 Chapter 8: Strings Strings in C are represented by arrays of characters. The end of the string is marked with a special character, the null character, which is simply the character

More information

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays C Arrays This handout was written by Nick Parlante and Julie Zelenski. As you recall, a C array is formed by laying out all the elements

More information

Here's how you declare a function that returns a pointer to a character:

Here's how you declare a function that returns a pointer to a character: 23 of 40 3/28/2013 10:35 PM Violets are blue Roses are red C has been around, But it is new to you! ANALYSIS: Lines 32 and 33 in main() prompt the user for the desired sort order. The value entered is

More information

Assignment 5: MyString COP3330 Fall 2017

Assignment 5: MyString COP3330 Fall 2017 Assignment 5: MyString COP3330 Fall 2017 Due: Wednesday, November 15, 2017 at 11:59 PM Objective This assignment will provide experience in managing dynamic memory allocation inside a class as well as

More information

Intro. Classes & Inheritance

Intro. Classes & Inheritance Intro Functions are useful, but they're not always intuitive. Today we're going to learn about a different way of programming, where instead of functions we will deal primarily with objects. This school

More information

A couple of decent C++ web resources you might want to bookmark:

A couple of decent C++ web resources you might want to bookmark: CS106X Handout 10 Autumn 2012 September 28 th, 2012 C++ and CS106 Library Reference Written by Julie Zelenski and revised by Jerry. A couple of decent C++ web resources you might want to bookmark: http://www.cppreference.com

More information

Unit 2: The string class

Unit 2: The string class : class Programming 2 2015-2016 Index 1 characters in C 2 Input / output 3 Conversion between arrays of characters and strings 4 Comparison with arrays of characters 5 characters in C characters in C have

More information

printf( Please enter another number: ); scanf( %d, &num2);

printf( Please enter another number: ); scanf( %d, &num2); CIT 593 Intro to Computer Systems Lecture #13 (11/1/12) Now that we've looked at how an assembly language program runs on a computer, we're ready to move up a level and start working with more powerful

More information

BASIC ELEMENTS OF A COMPUTER PROGRAM

BASIC ELEMENTS OF A COMPUTER PROGRAM BASIC ELEMENTS OF A COMPUTER PROGRAM CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING LOGO Contents 1 Identifier 2 3 Rules for naming and declaring data variables Basic data types 4 Arithmetic operators

More information

Objectives. In this chapter, you will:

Objectives. In this chapter, you will: Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates arithmetic expressions Learn about

More information

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. C How to Program, 6/e 1992-2010 by Pearson Education, Inc. An important part of the solution to any problem is the presentation of the results. In this chapter, we discuss in depth the formatting features

More information

Working with Strings. Husni. "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc.

Working with Strings. Husni. The Practice of Computing Using Python, Punch & Enbody, Copyright 2013 Pearson Education, Inc. Working with Strings Husni "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc. Sequence of characters We've talked about strings being a sequence of characters.

More information

CS193D Handout 10 Winter 2005/2006 January 23, 2006 Pimp Your Classes

CS193D Handout 10 Winter 2005/2006 January 23, 2006 Pimp Your Classes CS193D Handout 10 Winter 2005/2006 January 23, 2006 Pimp Your Classes See also: The middle part of Chapter 9 (194-208), Chapter 12 Pretty much any Object-Oriented Language lets you create data members

More information

Summer Final Exam Review Session August 5, 2009

Summer Final Exam Review Session August 5, 2009 15-111 Summer 2 2009 Final Exam Review Session August 5, 2009 Exam Notes The exam is from 10:30 to 1:30 PM in Wean Hall 5419A. The exam will be primarily conceptual. The major emphasis is on understanding

More information

Watch the video below to learn more about number formats in Excel. *Video removed from printing pages. Why use number formats?

Watch the video below to learn more about number formats in Excel. *Video removed from printing pages. Why use number formats? Excel 2016 Understanding Number Formats What are number formats? Whenever you're working with a spreadsheet, it's a good idea to use appropriate number formats for your data. Number formats tell your spreadsheet

More information

More on variables and methods

More on variables and methods More on variables and methods Robots Learning to Program with Java Byron Weber Becker chapter 7 Announcements (Oct 12) Reading for Monday Ch 7.4-7.5 Program#5 out Character Data String is a java class

More information

The C++ Language. Arizona State University 1

The C++ Language. Arizona State University 1 The C++ Language CSE100 Principles of Programming with C++ (based off Chapter 2 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/ Arizona State University

More information

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic BIT 3383 Java Programming Sem 1 Session 2011/12 Chapter 2 JAVA basic Objective: After this lesson, you should be able to: declare, initialize and use variables according to Java programming language guidelines

More information

CSE 143. Linked Lists. Linked Lists. Manipulating Nodes (1) Creating Nodes. Manipulating Nodes (3) Manipulating Nodes (2) CSE 143 1

CSE 143. Linked Lists. Linked Lists. Manipulating Nodes (1) Creating Nodes. Manipulating Nodes (3) Manipulating Nodes (2) CSE 143 1 CSE 143 Linked Lists [Chapter 4; Chapter 6, pp. 265-271] Linked Lists A linked list is a collection of dynamically allocated nodes Each node contains at least one member (field) that points to another

More information

Lecture 10. Command line arguments Character handling library void* String manipulation (copying, searching, etc.)

Lecture 10. Command line arguments Character handling library void* String manipulation (copying, searching, etc.) Lecture 10 Class string Namespaces Preprocessor directives Macros Conditional compilation Command line arguments Character handling library void* TNCG18(C++): Lec 10 1 Class string Template class

More information

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

CSCI 6610: Intermediate Programming / C Chapter 12 Strings ... 1/26 CSCI 6610: Intermediate Programming / C Chapter 12 Alice E. Fischer February 10, 2016 ... 2/26 Outline The C String Library String Processing in C Compare and Search in C C++ String Functions

More information

Professor Peter Cheung EEE, Imperial College

Professor Peter Cheung EEE, Imperial College 1/1 1/2 Professor Peter Cheung EEE, Imperial College In this lecture, we take an overview of the course, and briefly review the programming language. The rough guide is not very complete. You should use

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

Array. Prepared By - Rifat Shahriyar

Array. Prepared By - Rifat Shahriyar Java More Details Array 2 Arrays A group of variables containing values that all have the same type Arrays are fixed length entities In Java, arrays are objects, so they are considered reference types

More information

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will: Chapter 8 Arrays and Strings Objectives In this chapter, you will: Learn about arrays Declare and manipulate data into arrays Learn about array index out of bounds Learn about the restrictions on array

More information

Functions. Calculating Expressions with the Evaluate function. Passing Parameters to Functions and Procedures. Exiting from a Procedure

Functions. Calculating Expressions with the Evaluate function. Passing Parameters to Functions and Procedures. Exiting from a Procedure Functions Old Content - visit altium.com/documentation Modified by Rob Evans on 15-Feb-2017 Parent page: DelphiScript The common function statements used by the DelphiScript language are covered below.

More information

1 Shyam sir JAVA Notes

1 Shyam sir JAVA Notes 1 Shyam sir JAVA Notes 1. What is the most important feature of Java? Java is a platform independent language. 2. What do you mean by platform independence? Platform independence means that we can write

More information

Strings. Strings and their methods. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

Strings. Strings and their methods. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics Strings Strings and their methods Produced by: Dr. Siobhán Drohan Department of Computing and Mathematics http://www.wit.ie/ Topics list Primitive Types: char Object Types: String Primitive vs Object Types

More information

Dynamic Allocation in C

Dynamic Allocation in C Dynamic Allocation in C C Pointers and Arrays 1 The previous examples involved only targets that were declared as local variables. For serious development, we must also be able to create variables dynamically,

More information

A Fast Review of C Essentials Part I

A Fast Review of C Essentials Part I A Fast Review of C Essentials Part I Structural Programming by Z. Cihan TAYSI Outline Program development C Essentials Functions Variables & constants Names Formatting Comments Preprocessor Data types

More information

BTE2313. Chapter 2: Introduction to C++ Programming

BTE2313. Chapter 2: Introduction to C++ Programming For updated version, please click on http://ocw.ump.edu.my BTE2313 Chapter 2: Introduction to C++ Programming by Sulastri Abdul Manap Faculty of Engineering Technology sulastri@ump.edu.my Objectives In

More information

ITP 342 Mobile App Dev. Strings

ITP 342 Mobile App Dev. Strings ITP 342 Mobile App Dev Strings Strings You can include predefined String values within your code as string literals. A string literal is a sequence of characters surrounded by double quotation marks (").

More information

Java in 21 minutes. Hello world. hello world. exceptions. basic data types. constructors. classes & objects I/O. program structure.

Java in 21 minutes. Hello world. hello world. exceptions. basic data types. constructors. classes & objects I/O. program structure. Java in 21 minutes hello world basic data types classes & objects program structure constructors garbage collection I/O exceptions Strings Hello world import java.io.*; public class hello { public static

More information

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto CS 170 Java Programming 1 The Switch Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Menu-Style Code With ladder-style if-else else-if, you might sometimes find yourself writing menu-style

More information

x = 3 * y + 1; // x becomes 3 * y + 1 a = b = 0; // multiple assignment: a and b both get the value 0

x = 3 * y + 1; // x becomes 3 * y + 1 a = b = 0; // multiple assignment: a and b both get the value 0 6 Statements 43 6 Statements The statements of C# do not differ very much from those of other programming languages. In addition to assignments and method calls there are various sorts of selections and

More information

Introduction to string

Introduction to string 1 Introduction to string String is a sequence of characters enclosed in double quotes. Normally, it is used for storing data like name, address, city etc. ASCII code is internally used to represent string

More information

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */ Overview Language Basics This chapter describes the basic elements of Rexx. It discusses the simple components that make up the language. These include script structure, elements of the language, operators,

More information

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure C Overview Basic C Program Structure C OVERVIEW BASIC C PROGRAM STRUCTURE Goals The function main( )is found in every C program and is where every C program begins speed execution portability C uses braces

More information

DECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA.

DECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA. DECLARATIONS Character Set, Keywords, Identifiers, Constants, Variables Character Set C uses the uppercase letters A to Z. C uses the lowercase letters a to z. C uses digits 0 to 9. C uses certain Special

More information

Strings. Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects.

Strings. Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects. Strings Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects. The Java platform provides the String class to create and

More information

CS 231 Data Structures and Algorithms, Fall 2016

CS 231 Data Structures and Algorithms, Fall 2016 CS 231 Data Structures and Algorithms, Fall 2016 Dr. Bruce A. Maxwell Department of Computer Science Colby College Course Description Focuses on the common structures used to store data and the standard

More information

Lab 14 & 15: String Handling

Lab 14 & 15: String Handling Lab 14 & 15: String Handling Prof. Navrati Saxena TA: Rochak Sachan String Handling 9/11/2012 22 String Handling Java implements strings as objects of type String. Once a String object has been created,

More information

Lecture 2 Tao Wang 1

Lecture 2 Tao Wang 1 Lecture 2 Tao Wang 1 Objectives In this chapter, you will learn about: Modular programs Programming style Data types Arithmetic operations Variables and declaration statements Common programming errors

More information

Blitz2D Newbies: Definitive Guide to Types by MutteringGoblin

Blitz2D Newbies: Definitive Guide to Types by MutteringGoblin Blitz2D Newbies: Definitive Guide to Types by MutteringGoblin Types are probably the hardest thing to understand about Blitz Basic. If you're using types for the first time, you've probably got an uneasy

More information

MITOCW watch?v=0jljzrnhwoi

MITOCW watch?v=0jljzrnhwoi MITOCW watch?v=0jljzrnhwoi The following content is provided under a Creative Commons license. Your support will help MIT OpenCourseWare continue to offer high quality educational resources for free. To

More information

Where to Start? Looking at this naively, you'll need to. You'll need to do this for each line of input

Where to Start? Looking at this naively, you'll need to. You'll need to do this for each line of input Parsing Where to Start? Looking at this naively, you'll need to Setup Print prompt Read in user input Transform it to commands, files, and symbols Match to a pattern Execute command Print results Cleanup

More information

Advanced Systems Programming

Advanced Systems Programming Advanced Systems Programming Introduction to C++ Martin Küttler September 19, 2017 1 / 18 About this presentation This presentation is not about learning programming or every C++ feature. It is a short

More information

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG 1 Notice Assignments Reading Assignment: Chapter 3: Introduction to Parameters and Objects The Class 10 Exercise

More information

TEST (MODULE:- 1 and 2)

TEST (MODULE:- 1 and 2) TEST (MODULE:- 1 and 2) What are command line arguments? Write a program in JAVA to print Fibonacci series using command line arguments? [10] Create a class employee with data members empid, empname, designation

More information

CS107 Spring 2019, Lecture 4 C Strings

CS107 Spring 2019, Lecture 4 C Strings CS107 Spring 2019, Lecture 4 C Strings Reading: K&R (1.9, 5.5, Appendix B3) or Essential C section 3 This document is copyright (C) Stanford Computer Science and Nick Troccoli, licensed under Creative

More information

Slide 1 CS 170 Java Programming 1 Multidimensional Arrays Duration: 00:00:39 Advance mode: Auto

Slide 1 CS 170 Java Programming 1 Multidimensional Arrays Duration: 00:00:39 Advance mode: Auto CS 170 Java Programming 1 Working with Rows and Columns Slide 1 CS 170 Java Programming 1 Duration: 00:00:39 Create a multidimensional array with multiple brackets int[ ] d1 = new int[5]; int[ ][ ] d2;

More information

Computer Science E-119 Fall Problem Set 3. Due before lecture on Wednesday, October 31

Computer Science E-119 Fall Problem Set 3. Due before lecture on Wednesday, October 31 Due before lecture on Wednesday, October 31 Getting Started To get the files that you will need for this problem set, log into nice.harvard.edu and enter the following command: gethw 3 This will create

More information

COMP-202: Foundations of Programming. Lecture 5: Arrays, Reference Type, and Methods Sandeep Manjanna, Summer 2015

COMP-202: Foundations of Programming. Lecture 5: Arrays, Reference Type, and Methods Sandeep Manjanna, Summer 2015 COMP-202: Foundations of Programming Lecture 5: Arrays, Reference Type, and Methods Sandeep Manjanna, Summer 2015 Announcements Assignment 2 posted and due on 30 th of May (23:30). Extra class tomorrow

More information

Programming with Haiku

Programming with Haiku Programming with Haiku Lesson 2 Written by DarkWyrm All material 2010 DarkWyrm In our first lesson, we learned about how to generalize type handling using templates and some of the incredibly flexible

More information

LECTURE 02 INTRODUCTION TO C++

LECTURE 02 INTRODUCTION TO C++ PowerPoint Slides adapted from *Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* Copyright 2012 Pearson Education Inc. COMPUTER PROGRAMMING LECTURE 02 INTRODUCTION

More information

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

SYSC 2006 C Winter String Processing in C. D.L. Bailey, Systems and Computer Engineering, Carleton University SYSC 2006 C Winter 2012 String Processing in C D.L. Bailey, Systems and Computer Engineering, Carleton University References Hanly & Koffman, Chapter 9 Some examples adapted from code in The C Programming

More information

Linked Lists. What is a Linked List?

Linked Lists. What is a Linked List? Linked Lists Along with arrays, linked lists form the basis for pretty much every other data stucture out there. This makes learning and understand linked lists very important. They are also usually the

More information

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find CS1622 Lecture 15 Semantic Analysis CS 1622 Lecture 15 1 Semantic Analysis How to build symbol tables How to use them to find multiply-declared and undeclared variables. How to perform type checking CS

More information

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

On a 64-bit CPU. Size/Range vary by CPU model and Word size. On a 64-bit CPU. Size/Range vary by CPU model and Word size. unsigned short x; //range 0 to 65553 signed short x; //range ± 32767 short x; //assumed signed There are (usually) no unsigned floats or doubles.

More information

Topic 6: A Quick Intro To C

Topic 6: A Quick Intro To C Topic 6: A Quick Intro To C Assumption: All of you know Java. Much of C syntax is the same. Also: Many of you have used C or C++. Goal for this topic: you can write & run a simple C program basic functions

More information

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols. EEE-117 COMPUTER PROGRAMMING Basic Elements of C++ Objectives General Questions Become familiar with the basic components of a C++ program functions, special symbols, and identifiers Data types Arithmetic

More information

Slide 1 CS 170 Java Programming 1 Testing Karel

Slide 1 CS 170 Java Programming 1 Testing Karel CS 170 Java Programming 1 Testing Karel Introducing Unit Tests to Karel's World Slide 1 CS 170 Java Programming 1 Testing Karel Hi Everybody. This is the CS 170, Java Programming 1 lecture, Testing Karel.

More information

IDM 232. Scripting for Interactive Digital Media II. IDM 232: Scripting for IDM II 1

IDM 232. Scripting for Interactive Digital Media II. IDM 232: Scripting for IDM II 1 IDM 232 Scripting for Interactive Digital Media II IDM 232: Scripting for IDM II 1 PHP HTML-embedded scripting language IDM 232: Scripting for IDM II 2 Before we dive into code, it's important to understand

More information

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018 CSE 1001 Fundamentals of Software Development 1 Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018 Identifiers, Variables and Data Types Reserved Words Identifiers in C Variables and Values

More information

AIMS Embedded Systems Programming MT 2017

AIMS Embedded Systems Programming MT 2017 AIMS Embedded Systems Programming MT 2017 Object-Oriented Programming with C++ Daniel Kroening University of Oxford, Computer Science Department Version 1.0, 2014 Outline Classes and Objects Constructors

More information

CS107, Lecture 4 C Strings

CS107, Lecture 4 C Strings CS107, Lecture 4 C Strings Reading: K&R (1.9, 5.5, Appendix B3) or Essential C section 3 This document is copyright (C) Stanford Computer Science and Nick Troccoli, licensed under Creative Commons Attribution

More information

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7 Strings and Clases BIL104E: Introduction to Scientific and Engineering Computing Lecture 7 Manipulating Strings Scope and Storage Classes in C Strings Declaring a string The length of a string Copying

More information

Object oriented programming C++

Object oriented programming C++ http://uranchimeg.com Object oriented programming C++ T.Uranchimeg Prof. Dr. Email uranchimeg@must.edu.mn Power Engineering School M.EC203* -- OOP (C++) -- Lecture 06 Subjects Functions Functions with

More information

Basic data types. Building blocks of computation

Basic data types. Building blocks of computation Basic data types Building blocks of computation Goals By the end of this lesson you will be able to: Understand the commonly used basic data types of C++ including Characters Integers Floating-point values

More information

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6 CMSC 313 Introduction to Computer Systems Lecture 8 Pointers, cont. Alan Sussman als@cs.umd.edu Administrivia Project 2 posted, due October 6 public tests s posted Quiz on Wed. in discussion up to pointers

More information

C strings. (Reek, Ch. 9) 1 CS 3090: Safety Critical Programming in C

C strings. (Reek, Ch. 9) 1 CS 3090: Safety Critical Programming in C C strings (Reek, Ch. 9) 1 Review of strings Sequence of zero or more characters, terminated by NUL (literally, the integer value 0) NUL terminates a string, but isn t part of it important for strlen()

More information