Topical Information

This lab should provide you with practice with string manipulation, looping, function design, and perhaps even libraries.

Program Information

Write a function that allows case-insensitive comparison (aka ordering) of two strings. The strings should not be altered at all. The return value should be analogous to that from string::compare():

     s1 <  s2      return something <  0
     s1 == s2      return something == 0
     s1 >  s2      return something >  0

(Hint: These return values can be short integers.) (You are NOT allowed to use the library functions strcasecmp or stricmp as they are not standard or portable. You are tasked with writing your own version here!)

Just remember that the comparisons are to be done in a manner that ignores the case of the letters in the strings! For efficiency, you cannot use any local string variables in your function — just the two arguments. Neither string argument may be altered.

Even more, you may not alter the two string arguments before you send them to the function, either!

Call your function something useful (strcmp_ncase?).

Write a driver program to test your function.

Thought Provoking Questions

  1. How do you compare two character values without regard to case? Does this require you to destroy the character variable(s) contents?

  2. How can you access a single character from within a string?

  3. How can you range an integer-typed variable through a known sequence of values...say from 0 to not quite my_string_var.size() but stop early if a special event happens?

  4. How can you compare two strings in a case-insensitive way without destroying their contents? (Remember: You should not change the strings in order to compare them!)

  5. What kind of arguments should your string comparison function take? (I.E. value, reference, constant?)

  6. How do you get that weird return value for your function? Is it always -1, 0, 1? Or is there a reason it was defined as simply less than 0, 0, or greater than 0? (Hint: recall the inherited C propensity to view characters as ASCII codes — aka integers.)

  7. How many times will you need to call your function to test it thoroughly? How many times should you have to run the driver to do this testing? Maybe a driver should employ some form of yes/no looping mechanism?

    (Tip: Would a yes/no helper function come in handy to simplify this looping code and remove most of those details from your driver...helping you to focus on simply testing the comparison function?)

This assignment is (Level 2.5).

Options