C++ Program to check string is strictly alphabetical or not

C++Server Side ProgrammingProgramming

<p>Suppose we have a string S with n letters in lowercase. A string is strictly alphabetical string, if it follows the following rule &minus;</p><ul class="list"><li><p>Write an empty string to T</p></li><li><p>Then perform the next step n times;</p></li><li><p>At the i-th step take i-th lowercase letter of the Latin alphabet and insert it either to the left of the string T or to the right of the string T (c is the i-th letter of the Latin alphabet).</p></li></ul><p>We have to check whether S is strictly alphabetical string or not.</p><h2>Problem Category</h2><p>To solve this problem, we need to manipulate strings. Strings in a programming language are a stream of characters that are stored in a particular array-like data type. Several languages specify strings as a specific data type (eg. Java, C++, Python); and several other languages specify strings as a character array (eg. C). Strings are instrumental in programming as they often are the preferred data type in various applications and are used as the datatype for input and output. There are various string operations, such as string searching, substring generation, string stripping operations, string translation operations, string replacement operations, string reverse operations, and much more. Check out the links below to understand how strings can be used in C/C++.</p><p><a href="https://www.tutorialspoint.com/cplusplus/cpp_strings.htm" rel="nofollow" target="_blank">https://www.tutorialspoint.com/cplusplus/cpp_strings.htm</a></p><p><a href="https://www.tutorialspoint.com/cprogramming/c_strings.htm" rel="nofollow" target="_blank">https://www.tutorialspoint.com/cprogramming/c_strings.htm</a></p><p>So, if the input of our problem is like S = &quot;ihfcbadeg&quot;, then the output will be True.</p><h2>Steps</h2><p>To solve this, we will follow these steps &minus;</p><pre class="just-code notranslate language-cpp" data-lang="cpp">len := size of S for initialize i := len, when i &gt;= 1, update (decrease i by 1), do: &nbsp; &nbsp;if S[l] is the i th character, then: &nbsp; &nbsp; &nbsp; (increase l by 1) &nbsp; &nbsp;otherwise when S[r] is the ith character, then: &nbsp; &nbsp; &nbsp; (decrease r by 1) &nbsp; &nbsp;Otherwise &nbsp; &nbsp; &nbsp; Come out from the loop if i is same as 0, then: &nbsp; &nbsp;return true Otherwise &nbsp; &nbsp;return false</pre><h2>Example</h2><p>Let us see the following implementation to get better understanding &minus;</p><pre class="demo-code notranslate language-cpp" data-lang="cpp">#include &lt;bits/stdc++.h&gt; using namespace std; bool solve(string S){ &nbsp; &nbsp;int len = S.size(), l = 0, r = len - 1, i; &nbsp; &nbsp;for (i = len; i &gt;= 1; i--){ &nbsp; &nbsp; &nbsp; if (S[l] - &#39;a&#39; + 1 == i) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;l++; &nbsp; &nbsp; &nbsp; else if (S[r] - &#39;a&#39; + 1 == i) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;r--; &nbsp; &nbsp; &nbsp; else &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break; &nbsp; &nbsp;} &nbsp; &nbsp;if (i == 0) &nbsp; &nbsp; &nbsp; return true; &nbsp; &nbsp;else &nbsp; &nbsp; &nbsp; return false; } int main(){ &nbsp; &nbsp;string S = &quot;ihfcbadeg&quot;; &nbsp; &nbsp;cout &lt;&lt; solve(S) &lt;&lt; endl; }</pre><h2>Input</h2><pre class="result notranslate">&quot;ihfcbadeg&quot;</pre><h2>Output</h2><pre class="result notranslate">1</pre>
raja
Updated on 08-Apr-2022 08:33:51

Advertisements