string is not a fundamental C++ type,
but rather a struct defined in the string library.
As we have seen from time-to-time, there is another kind of
string in C++, the C-style string. C-style strings are also not
a fundamental type, but are also not structs. A C-style string
is simply an array of characters (type char*) with
the requirement that the "null character", string literal
'\0', occur in the array. The null character denotes
the end of the string represented by the array of characters,
though not necessarily the end of the array. Anything after the
'\0' is ignored when viewing the array as a string.
So, for example, the string literal "Hello World!" (string
literals are C-style strings) is actually the array
| H | e | l | l | o | W | o | r | l | d | ! | \0 |
| H | e | l | l | o | W | o | r | l | d | ! | \0 | X | % | 2 | g | * |
'\0' is ignored.
Now, given this discussion, you may get the idea that we could write:
char * s = "Hello World!";However, that ellicit's a stern warning from the compiler, because of some C/C++ ugliness that we haven't broached yet:
const. The presence
of const in a type declaration means that the
object is not allowed to be modified. So, for example,
if you make the declaration const int n = 12;,
you can write n*4 or cout << n,
but not n++, since that would be
modifying n. The characters in a string literal are
not allowed to be modified, and that fact needs to be part of
the declaration. Thus, we are supposed to write:
const char * s = "Hello World!";Don't get psyched out by the
const thing: a) you
can ignore it and ignore the compiler warnings, and b) it's
while string literals are const char *, in lots of
contexts we're not handling literals, so it doesn't matter.
Your job is to finish the program below by defining stringLength:
#include <iostream>
using namespace std;
int stringLength(const char * s);
int main()
{
const char * s = "Hello World!";
cout << "The length of \"" << s << "\" is "
<< stringLength(s) << endl;
char * r = reverse(s);
cout << "The reverse of \"" << s << "\" is \""
<< r << "\"" << endl;
return 0;
}
... and to define a function "reverse" so that if we add the
code
char * r = reverse(s);
cout << "The reverse of \"" << s << "\" is \""
<< r << "\"" << endl;
... to main(), we will also see the reverse of the string
printed.
time_t, but that's OK. Suffice it to say that
it's a type, and it's more or less just an int.
// time() returns the time as the number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).
// If t is non-NULL, the return value is also stored in the memory pointed to by t.
time_t time(time_t *t);
//The ctime() function takes an argument of data type time_t which represents calendar time.
//When interpreted as an absolute time value, it represents the number of seconds elapsed
//since the Epoch, 1970-01-01 00:00:00 +0000 (UTC). It converts the calendar time t into a
//null-terminated string of the form "Wed Jun 30 21:49:08 1993\n"
//The return value points to a statically allocated string which might be overwritten by
//subsequent calls to any of the date and time functions.
char *ctime(const time_t *timep);
Write a program that will print out information on
the current day in exactly this format:
Today is Thursday, 20 NovemberOf course it should print out the proper date for whatever the actual date is that the user runs the program. I.e., tomorrow it should print out
Today is Friday, 21 November.
I suggest you start off by simply printing the date/time string
produced by ctime for the current date and time, which you get
with the time function. I'm purposely not telling you how to
use these functions, and I don't want you to simply google for
examples: look at the prototypes and documentation and figure
out how to call these functions.
12/19/2014 @ 1532and your program should print out the date in the same format as above, i.e.
That date is Friday, 19 DecemberThe difficulty in doing that is to get a
time_t
representation of the date the user entered.
The function
time_t mktime(struct tm *tm);from the ctime library does this for us. Using this function, however, is not 100% trivial. First of all, notice that it involves the
struct tm. Here's the definition and
documentation of that struct.
struct tm {
int tm_sec; /* seconds */
int tm_min; /* minutes */
int tm_hour; /* hours */
int tm_mday; /* day of the month */
int tm_mon; /* month */
int tm_year; /* year */
int tm_wday; /* day of the week */
int tm_yday; /* day in the year */
int tm_isdst; /* daylight saving time */
};
The members of the tm structure are:
tm_sec The number of seconds after the minute, normally in the range 0 to 59, but can be up to 60 to allow for leap seconds.
tm_min The number of minutes after the hour, in the range 0 to 59.
tm_hour The number of hours past midnight, in the range 0 to 23.
tm_mday The day of the month, in the range 1 to 31.
tm_mon The number of months since January, in the range 0 to 11.
tm_year The number of years since 1900.
tm_wday The number of days since Sunday, in the range 0 to 6.
tm_yday The number of days since January 1, in the range 0 to 365.
tm_isdst A flag that indicates whether daylight saving time is in effect at the time described.
The value is positive if daylight saving time is in effect, zero if it is not, and nega‐
tive if the information is not available.
Second, here's the documentation for mktime:
The mktime() function converts a broken-down time structure, expressed as local time, to calendar
time representation. The function ignores the values supplied by the caller in the tm_wday and
tm_yday fields. The value specified in the tm_isdst field informs mktime() whether or not daylight
saving time (DST) is in effect for the time supplied in the tm structure: a positive value means DST
is in effect; zero means that DST is not in effect; and a negative value means that mktime() should
(use timezone information and system databases to) attempt to determine whether DST is in effect at
the specified time.
The mktime() function modifies the fields of the tm structure as follows: tm_wday and tm_yday are
set to values determined from the contents of the other fields; if structure members are outside
their valid interval, they will be normalized (so that, for example, 40 October is changed into 9
November); tm_isdst is set (regardless of its initial value) to a positive value or to 0, respec‐
tively, to indicate whether DST is or is not in effect at the specified time. Calling mktime() also
sets the external variable tzname with information about the current timezone.
12/19/2014 @ 1532 Fed the dog.
12/19/2014 @ 1547 Realized I don't have a dog. What's going on?
12/20/2014 @ 0701 Took cat to the vet.
... and allows the user a few basic commands:
add, delete, save, print, and quit.
add 10/18/2014 @ 1600 Ate a yummy donut.
delete 10/18/2014 @ 1600
print 10/14/2014 @ 1145 to 11/19/2014 @ 1700where dates are printed as in previous parts: like this:
Friday, 18 October @ 1600 Ate a yummy donut.