{"id":4591,"date":"2023-01-02T09:52:11","date_gmt":"2023-01-02T09:52:11","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=4591"},"modified":"2023-01-02T09:52:13","modified_gmt":"2023-01-02T09:52:13","slug":"c-program-to-find-the-first-occurrence-of-a-character-in-the-given-string","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/c-program-to-find-the-first-occurrence-of-a-character-in-the-given-string\/","title":{"rendered":"c program to find the first occurrence of a character in the given string"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">c program to find the first occurrence of a character in the given string<\/h2>\n\n\n\n<p>In this program, we are going to discuss how to find the first occurrence of a character in the given string.<\/p>\n\n\n\n<p><strong>For example-<\/strong><\/p>\n\n\n\n<p><strong>Input-<\/strong><\/p>\n\n\n\n<p>So first step we are going to take input from the user suppose we enter a string as hello from gyani pandit<\/p>\n\n\n\n<p><strong>Logic-<\/strong><\/p>\n\n\n\n<p>So to check the first occurrence of a given character we are going to use for loop in it in that every character is checked and for that, we are going to use a condition statement in it.<\/p>\n\n\n\n<p>for(i=0; i&lt;strlen(str); i++)<\/p>\n\n\n\n<p>if(str[i] ==ch){<br>flag = i;<br>break;<\/p>\n\n\n\n<p><strong>Output<\/strong><br>according to our input string and search character, we get output as-<\/p>\n\n\n\n<p>Enter the string: hello from gyani pandit<br>Enter the character that you want to search:i<br>The first occurrence character is i and the position is 15<\/p>\n\n\n\n<p>\/\/ c program to find the first occurrence of a character in the given string<\/p>\n\n\n\n<p class=\"has-white-color has-black-background-color has-text-color has-background\">#include&lt;stdio.h&gt;<br>#include&lt;string.h&gt;<br>int main()<br>{<br>char str[100], ch;<br>int i, flag;<\/p>\n\n\n\n<p class=\"has-white-color has-black-background-color has-text-color has-background\">printf(&#8220;Enter the string :&#8221;);<br>scanf(&#8220;%[^n]&#8221;,str);<br>getchar();<\/p>\n\n\n\n<p class=\"has-white-color has-black-background-color has-text-color has-background\">printf(&#8220;Enter the character that you want to search :&#8221;);<br>scanf(&#8220;%c&#8221;, &amp;ch);<\/p>\n\n\n\n<p class=\"has-white-color has-black-background-color has-text-color has-background\">for(i=0; i&lt;strlen(str); i++)<br>{<br>if(str[i] ==ch){<br>flag = i;<br>break;<br>}<br>}<br>if(flag ==0){<br>printf(&#8220;sorry! we have not found the character %c&#8221;,ch);<br>}<br>else{<br>printf(&#8220;The first occurrence character is %c and position is %d&#8221;,ch,flag);<br>}<br>return 0;<br>}<\/p>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<p>Enter the string: hello from gyani pandit<br>Enter the character that you want to search:i<br>The first occurrence character is i and the position is 15<\/p>\n\n\n\n<p>Here are the steps to find out the last occurrence of a character from the string<\/p>\n\n\n\n<p><strong>Step 1:<\/strong> make an array of strings.<\/p>\n\n\n\n<p><strong>Step 2:<\/strong> make two integer-type variables.<\/p>\n\n\n\n<p><strong>Step 3:<\/strong> use printf() statement to send the message to the user. After that use scanf() statement to store the value of a variable.<\/p>\n\n\n\n<p><strong>Step 4:<\/strong> Use getchar() function ,<\/p>\n\n\n\n<p><strong>Step 5:<\/strong> Again use printf() function to get a character from the user, that they want to search from the given string. So to store the value of the character again use scanf() function, in that we use %c formate specifier because we get a character from the user.<\/p>\n\n\n\n<p><strong>Step 6:<\/strong> Apply the loop statement that is for loop to verify all the characters in the string.<\/p>\n\n\n\n<p><strong>Step 7:<\/strong> apply condition statement that is if statement to check whether the character entered by the user is in a string or not.<\/p>\n\n\n\n<p><strong>Step 8:<\/strong> if the character is found in the string then simply use the break statement. To print that character.<\/p>\n\n\n\n<p><strong>Step 9:<\/strong> If the character is not found in the string then we simply apply the if statement.<\/p>\n\n\n\n<p><strong>Step 10:<\/strong> If the search character is found in the string then we simply print that character and the position of that character by using printf() statement.<\/p>\n\n\n\n<p><strong>Step 11:<\/strong> Execute the program by using F9.<\/p>\n\n\n\n<p>In this way, we simply understand how to find out the last occurrence of the string without using a function. I hope this concept is clear to you, you can also explore this concept.<\/p>\n\n\n\n<p>Keep learning, and keep exploring!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>c program to find the first occurrence of a character in the given string In this program, we are going to discuss how to find the first occurrence of a character in the given string. For example- Input- So first step we are going to take input from the user suppose we enter a string [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30],"tags":[29,103],"class_list":{"0":"post-4591","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-c-program","7":"tag-c-program","8":"tag-c-program-to-find-the-first-occurrence-of-a-character-in-the-given-string"},"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/4591","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=4591"}],"version-history":[{"count":1,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/4591\/revisions"}],"predecessor-version":[{"id":4592,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/4591\/revisions\/4592"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=4591"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=4591"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=4591"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}