Lab 13
Sign in to lab!
- Strings: character arrays (aka
cstrings
) vs. the standardstring
- Substring function (first example of function overloading)
- Lexicographical comparison using string
- String size function
Tracing
Exercises
Walk through these exercises and complete them as a group (if time allows):
cpp/cs102/practice13/stringTF
cpp/cs102/practice9/forCapitalization
cpp/cs102/practice13/secretAgent
cpp/cs102/practice12/letterReplace
Tracing Exercise
Assume the user types abcdef
when prompted.
#include <iostream>
using namespace std;
void f1(char str[], int i, int j);
int main()
{
// 6 locations + 1 for the null character
char input_str[7];
cout << "Enter a 6 character string: " << endl;
cin >> input_str;
for(int i = 0; i < 6; i++){
f1( input_str, i, (i+1)%6 )
}
for(int i = 0; i < 6; i++)
{
cout << input_str[i];
}
cout << endl;
return 0;
}
void f1(char str[], int i, int j)
{
char temp;
temp = str[j];
str[j] = str[i] + 1;
str[i] = temp;
return;
}
#include <iostream>
#include <string>
using namespace std;
bool isVowel(char input);
bool isConsonant(int input);
bool isNumeral(int input);
int main()
{
string str1 = "2019 is finally the year of the Clippers";
string str2 = "";
for (int i = 0; i < str1.size(); i++)
{
if( isVowel( (char) tolower(str1[i])) )
{
str2 = str2 + "_";
} else if ( isConsonant( (char) tolower(str1[i])) )
{
str2 = str2 + str1[i];
} else if ( isNumeral( str1[i]) )
{
str2 = str2 + "#";
} else
{
str2 = str2 + " ";
}
}
cout << str2 << endl;
return 0;
}
bool isVowel(char input)
{
return (input == 'a') || (input == 'e') || (input == 'i') || (input == 'o') || (input == 'u');
}
bool isConsonant(char input)
{
return (input > 'a') && ( input <= 'z') && ( input != 'e') && ( input != 'i') && ( input != 'o') && (input != 'u');
}
bool isNumeral(char input)
{
// what would be a more readable way to express this?
return (input >= '0' ) && (input <= '9');
}
HW Check In and Explanation
Check in as a group to see if everyone understand the basic premise of the homework and what you are expected to do. Take care to do your own work, but the TA/CPs can help you individually.