{"id":5479,"date":"2022-08-13T00:01:24","date_gmt":"2022-08-13T07:01:24","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5479"},"modified":"2022-08-06T11:12:32","modified_gmt":"2022-08-06T18:12:32","slug":"converting-month-strings-to-integers","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5479","title":{"rendered":"Converting Month Strings to Integers"},"content":{"rendered":"<p>I saw this problem on <a href=\"https:\/\/stackoverflow.com\/questions\/73171501\/how-to-parse-dates-in-c\" rel=\"noopener\" target=\"_blank\">stackoverflow<\/a>: How can you take a month string and convert it into an integer value, one through 12? The student submitting the query used a long <em>if-else if-else<\/em> construction. I have a better idea.<br \/>\n<!--more--><br \/>\nThe time functions in C use zero for January and 11 for December. But this assignment is to return values one through 12 for January through December. As the user on stackoverflow attempted, you could use a series of <em>strcmp()<\/em> statements in a complex <em>if-else if-else<\/em> structure, but I thought of a shortcut.<\/p>\n<p>Here are the twelve months:<\/p>\n<p>January<br \/>\nFebruary<br \/>\nMarch<br \/>\nApril<br \/>\nMay<br \/>\nJune<br \/>\nJuly<br \/>\nAugust<br \/>\nSeptember<br \/>\nOctober<br \/>\nNovember<br \/>\nDecember<\/p>\n<p>The month&#8217;s initial letters are J(3), F(1), M(2), A(2), S(1), O(1), and D(1).<\/p>\n<p>For the months of February, September, October, and December, you need only check the month&#8217;s first letter.<\/p>\n<p>For J months, you check the fourth letter: U is January, E is June, Y is July.<\/p>\n<p>For M months, check the third letter: R is March, Y is May<\/p>\n<p>For A months, check the second letter: P is April, U is August.<\/p>\n<p>A function to return the the proper values can use a <em>switch-case<\/em> structure to process the month names, as shown in the following code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_08_13-Lesson.c\" rel=\"noopener\" target=\"_blank\">2022_08_13-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;time.h&gt;\r\n\r\nint getMonthasInt(const char *monthString);\r\n\r\nint main()\r\n{\r\n    const char *month[] = {\r\n        \"January\", \"February\", \"March\", \"April\",\r\n        \"May\", \"June\", \"July\", \"August\", \"September\",\r\n        \"October\", \"November\", \"December\"\r\n    };\r\n    int count = 20;\r\n    int r,m;\r\n\r\n    <span class=\"comments\">\/* seed the randomizer *\/<\/span>\r\n    srand( (unsigned)time(NULL) );\r\n\r\n    <span class=\"comments\">\/* process 20 random month names *\/<\/span>\r\n    while(count)\r\n    {\r\n        r = rand() % 12;\r\n        m = getMonthasInt(month[r]);\r\n        printf(\"%02d %s\\n\",m,month[r]);\r\n        count--;\r\n    }\r\n\r\n    return(0);\r\n}\r\n\r\nint getMonthasInt(const char *monthString)\r\n{\r\n    switch( *(monthString) )\r\n    {\r\n        case 'J':\r\n            switch( *(monthString+3) )\r\n            {\r\n                case 'u':\r\n                    return(1);        <span class=\"comments\">\/* JanUary *\/<\/span>\r\n                case 'e':\r\n                    return(6);        <span class=\"comments\">\/* JunE *\/<\/span>\r\n                case 'y':\r\n                    return(7);        <span class=\"comments\">\/* JulY *\/<\/span>\r\n            }\r\n        case 'F':\r\n            return(2);                <span class=\"comments\">\/* February *\/<\/span>\r\n        case 'M':\r\n            switch( *(monthString+2) )\r\n            {\r\n                case 'r':\r\n                    return(3);        <span class=\"comments\">\/* MaRch *\/<\/span>\r\n                case 'y':\r\n                    return(5);        <span class=\"comments\">\/* MaY *\/<\/span>\r\n            }\r\n        case 'A':\r\n            switch( *(monthString+1) )\r\n            {\r\n                case 'p':\r\n                    return(4);        <span class=\"comments\">\/* APril *\/<\/span>\r\n                case 'u':\r\n                    return(8);        <span class=\"comments\">\/* AUgust *\/<\/span>\r\n            }\r\n        case 'S':\r\n            return(9);                <span class=\"comments\">\/* September *\/<\/span>\r\n        case 'O':\r\n            return(10);                <span class=\"comments\">\/* October *\/<\/span>\r\n        case 'N':\r\n            return(11);                <span class=\"comments\">\/* November *\/<\/span>\r\n        case 'D':\r\n            return(12);                <span class=\"comments\">\/* December *\/<\/span>\r\n    }\r\n    return(-1);\r\n}<\/pre>\n<p>In the <em>main()<\/em> function, a <em>const<\/em> array of strings (pointers) represents the months. The randomizer is seeded. Then a <em>while<\/em> loop spins 20 times, each time calling the <em>getMonthasInt()<\/em> function with a random month string. The function returns the month&#8217;s integer value, 1 through 12, which is output. All this action is setup for the <em>getMonthasInt()<\/em> function, the core of the problem.<\/p>\n<p>The <em>getMonthasInt()<\/em> function uses nested <em>switch-case<\/em> constructions to sift through the names. I don&#8217;t check case, and no <em>break<\/em> or <em>default<\/em> statements are used as success is immediately processed as a <em>return<\/em> value. If by some chance execution falls through, the final return value of -1 is generated.<\/p>\n<p>For an example, consider the nested <em>switch-case<\/em> for <code>'J'<\/code>: The fourth character in the string, <code>*(monthString+3)<\/code> is checked for <code>'u'<\/code>, <code>'e'<\/code>, and <code>'y'<\/code>. This technique sifts through the names without doing a full <em>strcmp()<\/em> comparison. In fact, only the necessary parts of this complex structure are executed for any given month, as opposed to all the processing and churning that takes place if you use <em>if-else<\/em> statements and <em>strcmp()<\/em>.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>01 January<br \/>\n08 August<br \/>\n05 May<br \/>\n03 March<br \/>\n09 September<br \/>\n06 June<br \/>\n11 November<br \/>\n11 November<br \/>\n09 September<br \/>\n06 June<br \/>\n02 February<br \/>\n05 May<br \/>\n12 December<br \/>\n10 October<br \/>\n01 January<br \/>\n08 August<br \/>\n09 September<br \/>\n07 July<br \/>\n11 November<br \/>\n05 May<\/code><\/p>\n<p>I suppose my insight into this approach stems from my proto-programmer days when processing muscle was weak and memory was limited. This type of sifting works given that the names are the same and capitalization is consistent. Even so, not much more coding is required to differentiate between letter cases. Regardless, it was a fun puzzle to solve.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When given a string such as &#8220;August,&#8221; how can you easily convert it into a value, such as 8? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5479\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-5479","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5479","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5479"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5479\/revisions"}],"predecessor-version":[{"id":5484,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5479\/revisions\/5484"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5479"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5479"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5479"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}