Schedule‎ > ‎

Lab 10: Strings and Overloaded Functions


  • Use for-statements
  • Code more complex decisions
  • Use functions in a program.
  • Use String variables in a program.
  • Work with resistor color codes.

^ top

Academic Honesty

Read the Scholastic Honesty Policy and Assignment Integrity policies of the syllabus. Here are some clarifications for this particular assignment:

  • You are expected to work by yourself.
  • You must breadboard the circuits yourself.
  • You may NOT look at another student's code until you complete and submit this assignment.
  • You may get help from people if you get stuck, but only if they do not show or tell you the code to type.

^ top

Project A: Counting Characters in a String

In this project we search a string for individual characters.


  1. Start with a new sketch and save the project using the name vowels.
  2. Write a function with the following signature:
    int countVowels(String str)
  3. The name of your function must be either countVowels() or count_vowels() with the return type and parameter type specified above.
  4. The function must return the count of all the vowels in the parameter String str.

    Vowels are the letters a, e, i, o, u and their uppercase variants.

  5. You must use do-while loops in your countVowels function to search for and count each vowel.
  6. You can use any of the String functions here:
  7. Use the trim() function to remove newline characters from a String variable.
  8. In your README.txt show the operation of your program for the following phrases and at least two original phrases, with the program name as a heading.
    Mary had a little lamb.
  9. Submit your vowels.ino file as part of this assignment.

Example Output

Enter a string:
"Mary had a little lamb" contains 6 vowels.

^ top

Project B: Absolute Value

In this project you will write two overloaded functions to compute the absolute value of an integer vs. float number.


1.  Start with a new sketch and save the sketch using the name absolute_value

2.  Start with this code in setup():

void setup() {
    int intParm = -7;
    float floatParm = 7.5; 
    cout << "Absolute value of " << intParm << " = " << absolute(intParm) << endl;
    cout << "Absolute value of " << floatParm << " = " << absolute(floatParm);
3.  You will write the definitions for two overloaded functions called absolute.  One version will take an integer paramenter,  the other will take a float parameter.  These are the prototypes for the functions:
int absolute(int parmInt);
float absolute(float parmFloat);

4.  Test your code to make sure it works properly.  The output should look like this:

Absolute value of -7 = 7
Absolute value of 7.50 = 7.50

Submit your file absolute_value.ino to Canvas with your other files for this Lab.

Extra Credit (2 pts):

Add code to absolute_value.ino to get the integer and float values from the user.  Modify the code so that the user can enter additional pairs of values to keep getting absolute values after the first two values.

Save this file as absolute_value_Extra_Credit.ino and submit to Canvas with your other files.

^ top

Style Requirements

Remember to follow all the style rules from previous assignments, as well as the new rules we recently covered, including:

  1. Function naming conventions (See: Function Names)
  2. Indentation in functions and placement of curly braces (See: Indentation)
  3. Function comment blocks See: Function Comment Block)
  4. Use named constants instead of magic numbers (see: Limit Magic Numbers)
  5. Proper use of spaces around operators (see: Spacing Around Operators
^ top

^ top

Grading Criteria (20pts) + Extra Credit (2pts)

Note that function headers must have the @param and @return comments filled in.

2 Parts: 

Each Part:  10 points  (Header 1pt, Function Headers 1pt, Compiles 1 pts, Proper Formatting 1 pt,  Works as Specified 6 pts)

Extra Credit:  2 points

^ top