CSC 270 - Survey of Programming Languages: C++ Lecture 2 - Strings Credited To Dr. Robert Siegfried
CSC 270 - Survey of Programming Languages: C++ Lecture 2 - Strings Credited To Dr. Robert Siegfried
Programming Languages
C++ Lecture 2 – Strings
Credited to Dr. Robert Siegfried
Predefined Functions in <cstring>
void newLine(void);
// Discards all the input remaining on the current
// input line.
// Also discards the '\n' at the end of the line.
getInt(n);
cout << "Final value read in == " << n << "\n"
<< "End of demonstation." << endl;
return(0);
}
// Uses iostream:
void newLine(void)
{
char symbol;
do {
cin .get(symbol);
} while (symbol != '\n');
}
do {
cout << "Enter input number: ";
cin >> number;
cout << "You entered " << number
<< " Is that correct(yes/no): ";
cin >> ans;
newLine();
} while ((ans == 'N') || (ans == 'n'));
}
put()
return 0;
}
peek()
// istream peek
#include <iostream>
using namespace std;
int main () {
char c;
int n;
char str[256];
return 0;
}
ignore()
// istream ignore
#include <iostream>
using namespace std;
int main () {
char first, last;
first=cin.get();
cin.ignore(256,' ');
last=cin.get();
cout << "Your initials are " << first << last;
return 0;
}
Character-manipulating Functions
• There are several operations that you may need
for basic text manipulation and are most
commonly performed character by character.
• These functions have their prototypes in the
cctype header file.
• Using these methods requires that
#include <cctype>
be included in the program using them
Functions in <cctype>
Function Description Example
toupper(c) Returns the upper case c = toupper(‘a’);
version of the character
tolower(c) Returns the lower case c = tolower(‘A’);
version of the character
isupper(c) Returns true if c is an if (isupper(c))
upper case letter cout << ‘upper case’;
int main(void)
{
string phrase; //uninitialized
return 0;
}
Output
I love fried ants!
Bon appetit
I/O with string
• You can use the insertion operator >> and
cout to print string objects just as you would
do with any other data item.
• You can use the extraction operator << and
cin to read string objects, but << will skip
initial whitespace and then read only until the
next whitespace character.
• If you wish to read input including the
whitespace, you need to use the method
cin.get()
motto.cpp
void newLine();
int main(void)
{
string firstName, lastName, recordName;
string motto
= "Your records are our records.";
cout << "Enter your first and last name:";
cin >> firstName >> lastName;
newLine();
return(0);
}
// Uses iostream
void newLine(void)
{
char nextChar;
do {
cin.get(nextChar);
} while (nextChar != '\n');
}
more Versions of getline
• getline(cin, line); will read until the
newline character.
• getline(cin, line, '?'); will read until
the '?'.
• getline(cin, s1) >> s2;
will read a line of characters into s1 and then
store the next string (up to the next
whitespace) in s2.
Mixing cin << variable with getline
• Consider
int n;
string line;
cin >> n;
getline(cin, line);
will read a value into n but nothing in line because it
is holding the remainder of the line from which n’s
value comes for the next use of cin.
String Processing with string
• The string class lets you use the same
operations that C-string allow and then some.
• E.g.
string s1;
s1.length - returns the length of the string s1.
1astName[i] is the ith character in the string.
NameArray.cpp
int main(void)
{
string firstName, lastName;
return(0);
}
Output
Enter your first and last name:
Robert Siegfried
Your last name is spelled:
S i e g f r i e d
- - - - - - - - -
Good day, Robert
Member Functions of the string class
Example Remarks
Constructors
string str Default constructor – creates empty string object str
string str("string"); Creates a string object with data "string"
string str(aString); Creates a string object that is a copy of aString,
(which is a string object)
Element Access
str[i] Returns read/write reference to character in str at index
i
str.at(i) Returns read/write reference to character in str at index
i
str.substr(position, Return the substring of the calling object starting at
length) position and having length characters
Member Functions of the string class
Example Remarks
Assignment/Modifiers
string str1 = str2; Allocates space and initializes it to str1’s data,
releases memory allocated to str1 and sets str1's size
to that of str2.
str1 += str2; Character data of str2 is concatenated to the end of
str1; the size is set appropriately
str.empty(); Returns true if str is an empty string; returns false
otherwise
str1 + str2 Returns a string that has str2’s data concatenated to
the end of str1’s data. The size is set appropriately
str.insert(pos, str2) Inserts str2 into str beginning at position pos
str.remove(pos, Removes a substring of size length beginning at
length) position pos
Member Functions of the string class
Example Remarks
Comparisons
str1 == str2 Compare for equality or inequality; returns a
str1 != str2; Boolean value.
str1 < str2 str1 > str2 Four comparisons. All are lexicographical
str1 >= str2 str1 <= str2; comparisons
str.find(str1) Returns index of the first occurrence of str1 in
str.
str.find(str1, pos) Returns index of the first occurrence of str1 in
str; the search starts at position pos.
str.find_first_of(str1, pos) Returns index of the first instance of any
character in str1; the search starts at position
pos.
str.find_first_not_of(pos, Returns index of the first instance of any
length) character not in str1; the search starts at
position pos
palindrome.cpp
// Test for palindrome property
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
if (isPal(str))
cout << "\"" << str
+ "\" is a palindrome." << endl;
else
cout << "\"" << str
+ "\" is not a palindrome." << endl;
cin >> str;
return(0);
}
return temp;
}
string removePunct(const string &s,
const string &punct) {
string noPunct; //Initialized to empty string
int sLength = s.length();
int punctLength = punct.length();
//ILLEGAL
aCString = stringVariable;
Strcpy(ACString, stringVariable);
//Legal
Strcpy(aCString, stringVariable.c_str());
//ILLEGAL
aCString = stringVAriable.c_str();