Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Lecture Slides on Declaring Strings | CS 23021, Assignments of Computer Science

Material Type: Assignment; Professor: Volkert; Class: CS I PROG/PROBLEM SOLVING; Subject: Computer Science; University: Kent State University; Term: Unknown 1989;

Typology: Assignments

2009/2010

Uploaded on 02/24/2010

koofers-user-3sn-2
koofers-user-3sn-2 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Strings
z
Strings are specific constructs that are geared towards processing
sequences of characters
z
before using include string header: #include <string>
don’t use string.h
z
string declaration: string mystr;
z
value assignment at declaration:
string mystr(“Hello”);
string mystr2=“Hello”;
z
assignments from other types are not permitted:
string error1 = ‘c’; // error
string error2 = 22; // error
z
can assign a character with assignment operator: mystr=‘n’;
Declaring strings
z
String can be output as any other type:
string s=“hello world”;
cout << s << endl;
z
two ways to input strings:
using extraction operator - strips white space and assigns the first
“word” to the string:
cin >> s;
hello wold\n input assigns only hello to s
using getline function - assigns all characters to string up to
newline (not included):
getline(cin, s);
hello wold\n input assigns hello world to s
do not mix one and the other way of input!
String I/O
z
use assignment operator as with other types:
string s1, s2, s3;
s1=“Intro”;
s2=“OOP”;
z
plus “+” is used for string concatenation: s3=s1 + “to” + s2;
z
compound concatenation allowed: s1 += “duction”;
z
characters can be concatenated with strings:
s2=“Hell” + ‘o’;
s2+=‘o’;
z
no other types can be assigned to strings or concatenated with
strings:
s2= 42.54; // error
s2=“Catch” + 22; // error
String assignment and
concatenation
z
Comparison operators (>, <, >=, <=, ==, !=) are applicable to strings
z
strings are compared lexicographically:
string s1=“accept”, s2=“access”, s3=“acceptance”;
s1 is less than s2 and s1 is less than s3
z
the order of symbols is determined by the symbol table (ASCII)
letters in the alphabet are in the increasing order
longer word (with the same characters) is greater than shorter
word
relying on other ASCII properties (e.g. capital letters are less than
small letters) is bad style
z
comparisons to string constants and named constants are also legal:
const string myname=“John Doe”;
string hername=“Jane Doe”;
if ((myname==hername)||(myname==“Jake Doe”))
cout << “found him\n”;
Comparing strings
z
A number of standard functions are defined for strings. Usual syntax:
string_name.function_name(arguments)
z
Useful functions return string paremeters:
size() - current string size (number of characters currently
stored in string
length() - same as size
max_size() - maximum number of characters in string allowed
on this computer
empty() - true if string is empt y
z
example:
string s=“Hello”;
cout << s.size();
outputs 5
String functions, string
characteristics
pf2

Partial preview of the text

Download Lecture Slides on Declaring Strings | CS 23021 and more Assignments Computer Science in PDF only on Docsity!

Strings

z Strings are specific constructs that are geared towards processing sequences of characters z before using include string header: #include don’t use string.h z string declaration: string mystr; z value assignment at declaration: string mystr(“Hello”); string mystr2=“Hello”; z assignments from other types are not permitted: string error1 = ‘c’; // error string error2 = 22; // error z can assign a character with assignment operator: mystr=‘n’;

Declaring strings

z String can be output as any other type: string s=“hello world”; cout << s << endl; z two ways to input strings: „ using extraction operator - strips white space and assigns the first “word” to the string: cin >> s; hello wold\n input assigns only hello to s „ using getline function - assigns all characters to string up to newline (not included): getline(cin, s); hello wold\n input assigns hello world to s „ do not mix one and the other way of input!

String I/O

z use assignment operator as with other types: string s1, s2, s3; s1=“Intro”; s2=“OOP”; z plus “+” is used for string concatenation: s3=s1 + “to” + s2; z compound concatenation allowed: s1 += “duction”; z characters can be concatenated with strings: s2=“Hell” + ‘o’; s2+=‘o’; z no other types can be assigned to strings or concatenated with strings: s2= 42.54; // error s2=“Catch” + 22; // error

String assignment and

concatenation

z Comparison operators (>, <, >=, <=, ==, !=) are applicable to strings z strings are compared lexicographically : string s1=“accept”, s2=“access”, s3=“acceptance”; s1 is less than s2 and s1 is less than s z the order of symbols is determined by the symbol table (ASCII) „ letters in the alphabet are in the increasing order „ longer word (with the same characters) is greater than shorter word „ relying on other ASCII properties ( e.g. capital letters are less than small letters) is bad style z comparisons to string constants and named constants are also legal: const string myname=“John Doe”; string hername=“Jane Doe”; if ((myname==hername)||(myname==“Jake Doe”)) cout << “found him\n”;

Comparing strings

z A number of standard functions are defined for strings. Usual syntax: string_name.function_name(arguments) z Useful functions return string paremeters: „ size() - current string size (number of characters currently stored in string „ length() - same as size „ max_size() - maximum number of characters in string allowed on this computer „ empty() - true if string is empty z example: string s=“Hello”; cout << s.size(); outputs 5

String functions, string

characteristics

z Similar to arrays a character in a string can be accessed and assigned to using its index (start from 0) cout << str[3];

z it is an error to access an element beyond the size of the string: string s=“Hello”; // size is 5 cout << s[6]; // error z the type of the element of the string is character, assigning integers, strings and other types are not allowed s[3] = “hi”; // error s[3] = 22; // error

Accessing elements of strings

z substr - function that returns a stubstring of a string: substr(start, numb) - start - index of the first character, numb - number of characters string s=“Hello”; // size is 5 cout << s.substr(3,2); // outputs “lo” z find family of functions return position of substring found, if not found return global constant string::npos defined in string header „ find(substr) - returns the postion of the first character of substring substr in the string „ rfind(substr) - same as find, search in reverse „ find_first_of(substr) - find first occurrence of any character of substr in the string „ find_last_of(substr) - find last occurrence of any character of substr in the string z all functions work with individual characters as well: cout << s.find(‘l’);

Substrings, finding

z insert(start, substring)- inserts substring starting from position start string s=“place”; cout << s.insert(1, “a”); cout << s; // outputs “palace” z erase(start, number)- removes number of characters starting from start string s=“Hello”; cout << s.erase(0,4); cout s; // outputs “lo” z replace (start, number, substring)- replaces number of characters starting from start with substring the number of characters replaced need not be the same string s=“Hello”; s.replace(1,4, “i, there”); cout << s; // outputs “Hi, there”

Replacing, erasing, inserting

z strings can be passed as parameters: „ by value: void myfunc(string s); „ by reference: void myfunc(string &s); if a string is not modified by the function use const: void myfunc(const string &s); z strings (unlike arrays) can be returned: „ string myfunc(int, int); z note, that passing strings by value and returning strings is less efficient than passing them by reference z however efficiency should in most cases be sacrificed for clarity

Passing strings as parameters

returning strings

z String is a complex structure, displaying a string can be confusing (gdb) display mystr 1: mystr = {static npos = 4294967295, static nilRep = {len = 0, res = 0, ref = 1, selfish = false}, dat = 0x3e7e0 "Doe"} z displaying just the value of the string is easier to read (gdb) display mystr.dat 2: mystr.dat = 0x3e7e0 "Doe” z caveat: sometimes (for efficiency of implementation) the string may appear to contain unspecified characters: 3: mystr.dat = 0x3e7e0 "Doe6%8^E1” „ use size function to determine the actual size of the string: (gdb) p mystr.size() $1 = 3

Displaying string values in GDB

z A copy of a string is created when it is passed by value and returned z a specific library function is called implicitly to create a copy z you cannot trace through this function since there is no source code available z how to trace through a function calling strings by value (returning strings? „ type (s)tep to get into the function you need to trace - the debugger tries to get into unavailable copy-function „ type finish to execute this function to completion without tracing „ type (s)tep again to get into the function

Tracing through a parameter call in

GDB