CSC3002 Tutorial 3
String Application
ZHANG, Chi
[email protected]
Outline
• Lecture conclusion
• String application examples:
• Palindromes
• Acronym
• Translating to Pig Latin
cctype library (ctype.h)
• Defines simple functions to simplify classification, conversion, etc. .
Inherited from C.
bool isdigit(char ch)
Determines if the specified character is a digit.
bool isalpha(char ch)
Determines if the specified character is a letter.
bool isalnum(char ch)
Determines if the specified character is a letter or a digit.
bool islower(char ch)
Determines if the specified character is a lowercase letter.
bool isupper(char ch)
Determines if the specified character is an uppercase letter.
bool isspace(char ch)
Determines if the specified character is whitespace (spaces and tabs).
char tolower(char ch)
Converts ch to its lowercase equivalent, if any. If not, ch is returned unchanged.
char toupper(char ch)
Converts ch to its uppercase equivalent, if any. If not, ch is returned unchanged.
cstring and string
Comparison:
• cstring: contents defined in string.h, essentially the C string
library to be used in C++.
• string: C++ standard library. Class and methods available in std::
namespace.
• string objects in C++ can be converted to C string literals via
c_str method.
• cstring functions:
• strlen(), strcpy(), strncpy(), strcat(), strncat(),
strcmp(), strncmp()
• string object-oriented methods:
• str.length(),str.at(index),str.find(ch, pos)
• Functions used for C string literals may also be available
String operations
str[i]
Returns the ith character of str. Assigning to str[i] changes that character.
s1 + s2
Returns a new string consisting of s1 concatenated with s2.
s1 = s2;
Copies the character string s2 into s1.
s1 += s2;
Appends s2 to the end of s1.
s1 == s2 (and similarly for <, <=, >, >=, and !=)
Compares to strings lexicographically.
str.c_str()
Returns a C-style character array containing the same characters as str.
Pay attention to your data type and availability of your operators!
Example - Palindrome
• Palindrome: a word/number/phrase that reads the same backwards
and forwards, e.g. ‘level’, ‘121’.
bool isPalindrome(string str) {
//Returns true if str is a palindrome
//Returns false if not
}
Example - Palindrome
• Solution 1:
bool isPalindrome(string str) {
int n = str.length();
for (int i = 0; i < n / 2; i++) {
if (str[i] != str[n - i - 1]) return
false;
}
return true;
}
• Solution 2:
bool isPalindrome(string str) {
return str == reverse(str);
}
Example - Acronym
• Acronym: a word formed by taking the first letter of each word in a
sequence
"self-contained underwater breathing apparatus“ -> “scuba”
string acronym(string str) {
string result = "";
return result;
}
• Hint:
bool inWord = false;
Example - Acronym
• Solution: string acronym(string str) {
string result = "";
bool inWord = false;
int nc = str.length();
for (int i = 0; i < nc; i++) {
char ch = str[i];
if (inWord) {
if (!isalpha(ch)) inWord = false;
} else {
if (isalpha(ch)) {
result += ch;
inWord = true;
}
}
}
return result;
}
Example – Translating to Pig Latin
• Translation rule:
• If the word contains no vowels, return the original word unchanged.
• If the word begins with a consonant, extract the set of consonants up to the
first vowel, move that set of consonants to the end of the word, and add "ay".
• If the word begins with a vowel, add "way" to the end of the word.
Example – Translating to Pig Latin
• Structure:
string lineToPigLatin(string line) { string wordToPigLatin(string word) {
string result; int vp = findFirstVowel(word);
int start = -1; // 3 cases according to the rules
// iterate through the line, if a complete if (vp == -1) {
word is detected: // TODO
// wordToPigLatin(line.substr(start, i - } else if (vp == 0) {
start)) // TODO
// mimic codes in example 2 } else {
return result; // TODO
} }
}
Example – Translating to Pig Latin
• Structure:
bool isVowel(char ch) {
switch (ch) {
case 'A': case 'E': case 'I': case 'O': case
int findFirstVowel(string word) { 'U':
// return idx of vowel found case 'a': case 'e': case 'i': case 'o': case
// else: 'u':
return -1; return true;
} default:
return false;
}
}
Upcoming event:
• Assignment 1: release on Sept. 19, due Oct. 8.
Thank you!