{"id":7576,"date":"2026-05-16T00:01:49","date_gmt":"2026-05-16T07:01:49","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7576"},"modified":"2026-05-02T13:10:55","modified_gmt":"2026-05-02T20:10:55","slug":"ctype-functions-isupper-and-islower","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7576","title":{"rendered":"Ctype Functions: <em>isupper()<\/em> and <em>islower()<\/em>"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/05\/ctype.png\" alt=\"\" width=\"400\" height=\"138\" class=\"alignnone size-full wp-image-7590\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/05\/ctype.png 400w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/05\/ctype-300x104.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><br \/>\nI wouldn&#8217;t say that the <em>isupper()<\/em> and <em>islower()<\/em> functions are the opposite of the <em>toupper()<\/em> and <em>tolower()<\/em> functions, though they do play well together. This week I continue my exploration of the <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7542\">ctype functions<\/a>.<br \/>\n<!--more--><br \/>\nThe <em>toupper()<\/em> function converts a lowercase letter to uppercase.<\/p>\n<p>The <em>tolower()<\/em> function converts an uppercase letter to lowercase.<\/p>\n<p>The following code demonstrates these two functions, but also includes the <em>islower()<\/em> and <em>isupper()<\/em> functions. The result is to convert characters in the sample string into their opposite case.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2026_05_16-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2026_05_16-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;ctype.h&gt;\r\n\r\nint main()\r\n{\r\n    char string[] = \"Nothing beats knowing the ASCII codes!\\n\";\r\n    char *s;\r\n\r\n    <span class=\"comments\">\/* output normal *\/<\/span>\r\n    printf(\"%s\",string);\r\n\r\n    <span class=\"comments\">\/* invert case *\/<\/span>\r\n    s = string;\r\n    while( *s != '\\0' )\r\n    {\r\n        if( islower(*s) )\r\n            putchar( toupper(*s) );\r\n        else if( isupper(*s) )\r\n            putchar( tolower(*s) );\r\n        else\r\n            putchar(*s);\r\n        s++;\r\n    }\r\n\r\n    return 0;\r\n}<\/pre>\n<p>A <em>printf()<\/em> statement outputs variable <code>string[]<\/code> as-is. Then a <em>while<\/em> loop uses pointer <code>s<\/code> to process the string one character at a time.<\/p>\n<p>If a character is lowercase: <code>if( islower(*s) )<\/code> it&#8217;s converted to uppercase and output: <code>putchar( toupper(*s) )<\/code><\/p>\n<p>Likewise, when a character is uppercase: <code>if( isupper(*s) )<\/code> it&#8217;s converted to lowercase and output: <code>putchar( tolower(*s) )<\/code><\/p>\n<p>All other characters are output as-is: <code>putchar(*s)<\/code><\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<pre>Nothing beats knowing the ASCII codes!\r\nnOTHING BEATS KNOWING THE ascii CODES!<\/pre>\n<p>As with <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7457\">last week&#8217;s example<\/a>, it&#8217;s possible to code your own ctype functions to work with upper- and lowercase letters. The following update to the code includes my own versions of the <em>islower()<\/em>, <em>toupper()<\/em>, <em>isupper()<\/em>, and <em>tolower(<\/em>) functions.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2026_05_16-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2026_05_16-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\n#define TRUE 1\r\n#define FALSE 0\r\n\r\n<span class=\"comments\">\/* return TRUE for uppercase char *\/<\/span>\r\nint isupper(int c)\r\n{\r\n    if( c&gt;='A' &amp;&amp; c&lt;='Z' )\r\n        return TRUE;\r\n    else\r\n        return FALSE;\r\n}\r\n\r\n<span class=\"comments\">\/* return TRUE for lowercase char *\/<\/span>\r\nint islower(int c)\r\n{\r\n    if( c&gt;='a' &amp;&amp; c&lt;='z' )\r\n        return TRUE;\r\n    else\r\n        return FALSE;\r\n}\r\n\r\n<span class=\"comments\">\/* convert to uppercase *\/<\/span>\r\nint toupper(int c)\r\n{\r\n    if( islower(c) )\r\n        c &amp;= 0xDF;\r\n\r\n    return(c);\r\n}\r\n\r\n<span class=\"comments\">\/* convert to lowercase *\/<\/span>\r\nint tolower(int c)\r\n{\r\n    if( c&gt;='A' &amp;&amp; c&lt;='Z' )\r\n        c |= 0x20;\r\n\r\n    return(c);\r\n}\r\n\r\nint main()\r\n{\r\n    char string[] = \"Nothing beats knowing the ASCII codes!\\n\";\r\n    char *s;\r\n\r\n    <span class=\"comments\">\/* output normal *\/<\/span>\r\n    printf(\"%s\",string);\r\n\r\n    <span class=\"comments\">\/* invert case *\/<\/span>\r\n    s = string;\r\n    while( *s != '\\0' )\r\n    {\r\n        if( islower(*s) )\r\n            putchar( toupper(*s) );\r\n        else if( isupper(*s) )\r\n            putchar( tolower(*s) );\r\n        else\r\n            putchar(*s);\r\n        s++;\r\n    }\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The code define TRUE as one and FALSE as zero. These are the return values used in my <em>isupper<\/em>() and <em>islower()<\/em> functions. Each function uses an <em>if-else<\/em> construction to weed out the upper- and lowercase ASCII characters, respectively.<\/p>\n<p>I also coded my own <em>toupper()<\/em> and <em>tolower()<\/em> functions. Unlike last week&#8217;s examples, this time I used logical operations to reset the fifth bit in the byte to make the conversion:<\/p>\n<p><code>c &amp;= 0xDF;<\/code> to convert a lowercase letter to uppercase<\/p>\n<p><code>c |= 0x20;<\/code> to convert an uppercase letter to lowercase<\/p>\n<p>The updated code&#8217;s output is the same as the original:<\/p>\n<pre>Nothing beats knowing the ASCII codes!\r\nnOTHING BEATS KNOWING THE ascii CODES!<\/pre>\n<p>For this example, I&#8217;d probably combine these functions as that&#8217;s how they&#8217;re used in the code. But by splitting them, I&#8217;m able to have the functions available should they be needed individually.<\/p>\n<p>For next week&#8217;s Lesson, I cover the <em>isalnum()<\/em>, <em>isalpha()<\/em>, and <em>isdigit()<\/em> ctype functions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>These ctype testing functions work with letters &#8216;A&#8217; through &#8216;Z&#8217; and &#8216;a&#8217; through &#8216;z&#8217; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7576\">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-7576","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\/7576","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=7576"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7576\/revisions"}],"predecessor-version":[{"id":7594,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7576\/revisions\/7594"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7576"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7576"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7576"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}