Week 11 In-Class Examples

Strings:

#include <iostream>
#include <string>
using namespace std; int main(void) { string a_name; int i; a_name = "Ed"; cout << "The string " << a_name << " contains "; cout << a_name.size() << " characters\n"; a_name = "Joe Smith"; cout << "The string " << a_name << " contains "; cout << a_name.size() << " characters\n"; cout << "The second character is " << a_name[1] << endl; for (i=0; i< a_name.size(); i++) { cout << a_name[i] << "\n"; } for (i=a_name.size(); i >= 0; i--) { cout << a_name[i]; } cout << endl; return 0; }

Strings:

#include <iostream>
#include <string>

using namespace std;

int main(void)
{
   string a_name;
   int i;

   cout << "Enter a name ";
   getline(cin,a_name,'\n');

   cout << "The string " << a_name << " contains ";
   cout << a_name.size() << " characters\n";

   for (i=0; i< a_name.size(); i++) {
       cout << a_name[i] << "\n";
   }

   for (i=a_name.size(); i >= 0; i--) {
       cout << a_name[i];
   }
   cout << endl;
   return 0;
}

Strings:

#include <iostream>
#include <string>

using namespace std;

int main(void)
{
   string some_data;
   int i;
   int num_chars;

   cout << "Enter some data ";
   getline(cin,some_data,'\n');

   cout << "The string " << some_data << " contains ";
   cout << some_data.size() << " characters\n";

   for (i=0; i< some_data.size(); i++) {
       if (isalpha(some_data[i])) {
          cout << some_data[i] << "\n";
          num_chars++;
       }
   }

   cout << "There are " << num_chars << " alphabetic chars in the data/n";
   return 0;
}