// chars.cpp -- hints on project 8
#include <iostream>
using namespace std;
int main()
{
cout << "Enter your text (or end): ";
string input; // declare variable
getline(cin, input); // get one line of text from user
// Example: print every other character
for(int i = 0; i < input.length(); i += 2)
{
cout << input[i];
}
cout << endl;
// Example: count occurrences of 'T' or 't'.
int count = 0;
for(int i = 0; i < input.length(); i++)
{
if(input[i] == 'T' || input[i] == 't' )
{
count++;
}
cout << "Char at " << i << " is "
<< input[i] << " count is "
<< count << endl;
}
cout << "There are " << count << " T's." << endl;
// Example: count occurrences of 'N' or 'n' using switch
count = 0;
for(int i = 0; i < input.length(); i++)
{
switch(input[i])
{
case 'N':
case 'n':
count++;
}
cout << "Char at " << i << " is "
<< input[i] << " count is "
<< count << endl;
}
cout << "There are " << count << " N's." << endl;
// Outline of a palindrome-testing loop
int i = 0;
int j = input.length() - 1;
while(i < j)
{
cout << "Checking #" << i << "=" << input[i]
<< " vs #" << j << "=" << input[j]
<< endl;
i++;
j--;
}
return 0;
}
// funcdemo.cpp
#include <iostream>
using namespace std;
// Prototypes, aka forward declarations, "promise"
double triangle(double base, double height);
int square(int);
int main()
{
cout << square(4) << endl;
cout << square(square(square(5))) << endl;
cout << triangle(5,9) << endl;
return 0;
}
double triangle(double base, double height)
{
return (base * height) / 2;
}
int square(int k)
{
return k * k;
}