<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>strings Archives - Helpmestudybro</title>
	<atom:link href="https://www.helpmestudybro.com/tag/strings/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.helpmestudybro.com/tag/strings/</link>
	<description>Learn Computer Science </description>
	<lastBuildDate>Sat, 31 Oct 2020 11:01:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.5.8</generator>

<image>
	<url>https://www.helpmestudybro.com/wp-content/uploads/2020/07/cropped-HSB-Logo-32x32.png</url>
	<title>strings Archives - Helpmestudybro</title>
	<link>https://www.helpmestudybro.com/tag/strings/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Reverse a String Using Stack</title>
		<link>https://www.helpmestudybro.com/reverse-a-string-using-stack/</link>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Mon, 29 Jun 2020 18:46:12 +0000</pubDate>
				<category><![CDATA[data structure]]></category>
		<category><![CDATA[Stacks]]></category>
		<category><![CDATA[strings]]></category>
		<category><![CDATA[stacks]]></category>
		<guid isPermaLink="false">https://www.helpmestudybro.com/?p=615</guid>

					<description><![CDATA[<p>“Reverse a String Using Stack” is a basic programming exercise problem based on stack data structure. Here, in this problem, we are given a string and our task is to reverse a given string using stack data structure. Example: String: Helpmestudybro Reversed String: orbydutsempleH &#160; String: Programming is cool Reverse&#46;&#46;&#46;</p>
<p>The post <a href="https://www.helpmestudybro.com/reverse-a-string-using-stack/">Reverse a String Using Stack</a> appeared first on <a href="https://www.helpmestudybro.com">Helpmestudybro</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>“<strong><em>Reverse a String Using Stack</em></strong>” is a basic programming exercise problem based on stack data structure. Here, in this problem, we are given a string and our task is to reverse a given string using stack data structure.</p>



<h5 class="wp-block-heading"><strong>Example:</strong></h5>



<pre class="wp-block-preformatted">String: Helpmestudybro
Reversed String: orbydutsempleH
&nbsp;
String: Programming is cool
Reverse String: looc si gnimmargorP</pre>



<p>The steps required to Reverse a String using stack are as follows:</p>



<ol type="1"><li>Create an empty character stack.</li><li>Push all the characters of the string into the stack.</li><li>Nullify the given string.</li><li>Pop and concatenate all the character from the stack to given string.</li><li>Return and print the result.</li></ol>



<p>The time complexity of this solution is O(n) and it is using O(n) extra space.</p>



<h4 class="wp-block-heading"><strong>C++ Program to Reverse a String Using Stack is as follows:</strong></h4>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
/* C++ Program to Reverse a String Using String */
#include&lt;bits/stdc++.h&gt;
using namespace std;

/* Function to Reverse a Stack */
string reverseUsingStack(string str)
{
    /* Create an empty character Stack */
    stack&lt;char&gt; st;
    
    /* Push all the character of the string into stack */
    for(int i = 0; i &lt; str.length(); i++)
    {
        st.push(str&#x5B;i]);
    }
    
    /* Nullify a string */
    str = &quot;&quot;;
    
    /* Pop and concatenate all characters of the stack */
    while(!st.empty())
    {
        str = str + st.top();
        st.pop();
    }
    
    /* Return the reverse string */
    return str;
}
int main()
{
    string str = &quot;helpmestudybro is an emerging website&quot;;
    string reverse_string = reverseUsingStack(str);
    cout&lt;&lt;&quot;The Reversed String is:\n&quot;&lt;&lt;reverse_string;
}
</pre></div>


<pre class="wp-block-preformatted"><strong><em>OUTPUT:</em></strong>
The Reversed String is:
etisbew gnigreme na si orbydutsempleh</pre>



<hr class="wp-block-separator is-style-wide"/>



<p class="has-medium-font-size"><strong>Related Posts:</strong></p>



<ol type="1"><li><a href="https://www.helpmestudybro.com/implement-two-stacks-in-a-single-array/">Implement two stacks in a single array</a></li><li><a href="https://www.helpmestudybro.com/print-bracket-number/">Print Bracket Number</a></li><li><a href="https://www.helpmestudybro.com/next-greater-frequency-element/">Next Greater Frequency Element</a></li><li><a href="https://www.helpmestudybro.com/sort-a-stack-using-temporary-stack/">Sort a Stack using Temporary Stack</a></li><li><a href="https://www.helpmestudybro.com/program-to-implement-infix-to-postfix-conversion/">Infix to Postfix Conversion</a></li><li><a href="https://www.helpmestudybro.com/program-to-implement-infix-to-prefix-conver/">Infix to Prefix Conversion</a></li><li><a href="https://www.helpmestudybro.com/program-to-convert-prefix-to-infix-using-stack/">Prefix to Infix Conversion</a></li><li><a href="https://www.helpmestudybro.com/program-to-implement-prefix-to-postfix-conversion/">Prefix to Postfix Conversion</a></li><li><a href="https://www.helpmestudybro.com/program-to-implement-postfix-to-infix-conversion/">Postfix to Infix Conversion</a></li><li><a href="https://www.helpmestudybro.com/program-to-implement-postfix-to-prefix-conversion/">Postfix to Prefix Conversion</a></li><li><a href="https://www.helpmestudybro.com/program-to-check-for-balanced-parentheses/">Check whether given Parentheses String are Balanced Parentheses or Not.</a></li><li><a href="https://www.helpmestudybro.com/program-to-find-next-greater-element/">Next Greater Element</a></li><li><a href="https://www.helpmestudybro.com/minimum-number-of-bracket-reversals-needed-to-make-an-expression-balanced/">Find Minimum number of bracket reversals required to make an expression balanced.</a></li><li><a href="https://www.helpmestudybro.com/program-to-implement-queue-using-stacks/">Implement Queue Using Two Stacks.</a></li><li><a href="https://www.helpmestudybro.com/find-first-non-repeating-character-of-the-string/">Find First Non-Repeating Character of the String.</a></li><li><a href="https://www.helpmestudybro.com/find-most-frequent-element-in-an-array/">Find Most Frequent Element of the Array.</a></li><li><a href="https://www.helpmestudybro.com/program-to-merge-overlapping-intervals/">Merge Overlapping Intervals using Stacks</a></li><li><a href="https://www.helpmestudybro.com/implement-stack-using-linked-list/">Implement Stack Using Linked List</a></li><li><a href="https://www.helpmestudybro.com/largest-rectangular-area-in-histogram/">Largest Rectangular Area in Histogram</a></li><li><a href="https://www.helpmestudybro.com/length-of-longest-valid-substring/">Length of Longest Valid Substring</a></li></ol>



<hr class="wp-block-separator is-style-wide"/>
<p>The post <a href="https://www.helpmestudybro.com/reverse-a-string-using-stack/">Reverse a String Using Stack</a> appeared first on <a href="https://www.helpmestudybro.com">Helpmestudybro</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Find First Non-Repeating Character of the String</title>
		<link>https://www.helpmestudybro.com/find-first-non-repeating-character-of-the-string/</link>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sun, 14 Jun 2020 10:50:51 +0000</pubDate>
				<category><![CDATA[data structure]]></category>
		<category><![CDATA[hashing]]></category>
		<category><![CDATA[strings]]></category>
		<guid isPermaLink="false">https://www.helpmestudybro.com/?p=230</guid>

					<description><![CDATA[<p>“Find First Non-Repeating Character of the String” is a very important and popular problem asked in many technical interviews of product-based companies.&#160; Here, we are given a string of length ‘n’ and our task is to find the first non-repeating character of the string. Example: Input: COMPUTERSCIENCE Output: The first&#46;&#46;&#46;</p>
<p>The post <a href="https://www.helpmestudybro.com/find-first-non-repeating-character-of-the-string/">Find First Non-Repeating Character of the String</a> appeared first on <a href="https://www.helpmestudybro.com">Helpmestudybro</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>“<strong><em>Find First Non-Repeating Character of the String</em></strong>” is a very important and popular problem asked in many technical interviews of product-based companies.&nbsp;</p>



<p>Here, we are given a string of length ‘n’ and our task is to find the first non-repeating character of the string.</p>



<p class="has-medium-font-size"><strong>Example:</strong></p>



<pre class="wp-block-preformatted"><strong><em>Input:</em></strong>
COMPUTERSCIENCE
<strong><em>Output:</em></strong>
The first non-repeating character of given string is O</pre>



<p>There are various methods to find the first non-repeating character of the string. Some of them are:</p>



<h4 class="wp-block-heading"><strong>METHOD 1: Brute-Force Approach to find first non-repeating character of the string</strong></h4>



<p>The Brute-Force solution of this problem is to iterate over complete string, and for every character encountered, again iterate over complete string in nested loop and find the frequency of each character of the string.&nbsp;</p>



<p>While finding the frequency of each character, if the frequency of the current character is 1, then that character is the first non-repeating character of the given string.</p>



<p>If for any character, the frequency is not 1, then there is no non-repeating character present in the string.</p>



<h4 class="wp-block-heading"><strong>C++ Program to find first non-repeating character of the string is as follows:</strong></h4>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
/* C++ Program to find first non-repeating character of the string */
#include&lt;bits/stdc++.h&gt;
using namespace std;
int main()
{
  string str;
    
  /* Scan the String */
  cout&lt;&lt;&quot;Enter a string:\n&quot;;  
  getline(cin,str); 
  
  /* Count the frequency of each character of the string,
    and the first characrter encountered with frequency 1 
    will be result */
    char res;
    int flag = 0;
    for(int i = 0; i &lt; str.length(); i++)
    {
        int count = 0;
        for(int j = 0; j &lt; str.length(); j++)
        {
            if(str&#x5B;i] == str&#x5B;j])
            count++;
            if(count &gt; 1)
            break;
        }
        if(count == 1)
        {
            res = str&#x5B;i];
            flag = 1;
            break;
        }
    }
    /* Printing the Result */
    if(flag == 1)
    cout&lt;&lt;&quot;The first non-repeating character of the given string is &quot;&lt;&lt;res;
    else
    cout&lt;&lt;&quot;There is no non-repeating character present in the string&quot;;
}
</pre></div>


<pre class="wp-block-preformatted"><strong><em>Output:</em></strong>
Enter a string: COMPUTERSCIENCE
The first non-repeating character of the given string is O
<strong><em>Time Complexity</em></strong>:
O(n^2), where n is the length of the string.</pre>



<h4 class="wp-block-heading"><strong>METHOD 2: Hashing Method to find first non-repeating character of the string</strong></h4>



<p>The more time efficient method to find the first non-repeating character of the string is hashed based.&nbsp;</p>



<p>Create a hash map with key of char type and value of integer type. Iterate over the string and increment the value of every character.</p>



<p>Again, scan the string and for every character, if in the map, the character key-pair value is 1, then break the loop and print the character.</p>



<h4 class="wp-block-heading"><strong>C++ Program to find first non-repeating character of the string is as follows:</strong></h4>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
/*C++ Program to find first non-repeating character of the string*/  
#include&lt;bits/stdc++.h&gt;  
#define size 1000  
using namespace std;  
int main()  
{  
    string str;  
    cout&lt;&lt;&quot;Enter a string:&quot;;  
    getline(cin,str);  
    int flag1 = 0;  
    /* Create a Hash Map */  
    map&lt;char,int&gt;mp;  
    /* For every character, increment the  
       key-pair value for that character */  
    for(int i = 0; i &lt;  str.length(); i++)  
    {  
        mp&#x5B;str&#x5B;i]]++;  
    }  
    /* Check if key-pair valu1 of any character is 1 */  
    for(int i = 0; i &lt; str.length(); i++)  
    {  
        /* If key-pair value of any character is 1, then  
        that characte is first non-repeating character*/  
        if(mp&#x5B;str&#x5B;i]] == 1)  
        {  
            flag1 = 1;  
            cout&lt;&lt;&quot;\nThe first non-repeating character is &quot;&lt;&lt;str&#x5B;i];  
            break;  
        }  
    }  
    if(flag1 == 0)  
    {  
        cout&lt;&lt;&quot;\nAll the characters in the string are repeating characters!!&quot;;  
    }  
}  
</pre></div>


<pre class="wp-block-preformatted"><strong><em>Output</em></strong>:
Enter a string: COMPUTERSCIENCE
The first non-repeating character is O
<strong><em>Time Complexity:</em></strong>
O(nlogn), where n is the length of the string, and ‘logn’ is for insertion and searching operation in map.</pre>



<hr class="wp-block-separator is-style-wide"/>



<p class="has-medium-font-size"><strong>Related Posts:</strong></p>



<ol type="1"><li><a href="https://www.helpmestudybro.com/find-most-frequent-character-in-string/">Find Most Frequent Character of the String.</a></li><li><a href="https://www.helpmestudybro.com/check-whether-two-strings-are-anagram-of-each-other/">Check Anagram Strings.</a></li><li><a href="https://www.helpmestudybro.com/check-whether-string-is-palindromic-anagram-or-not/">Check Whether Given String is Palindromic Anagram or Not.</a></li><li><a href="https://www.helpmestudybro.com/program-to-check-pangram-string/">Check Whether Given String is Panagram or Not.</a></li><li><a href="https://www.helpmestudybro.com/find-most-frequent-element-in-an-array/">Find Most Frequent Element of the Array.</a></li><li><a href="https://www.helpmestudybro.com/program-to-find-pair-in-an-array-with-given-sum/">Find Pair in an Array with Given Sum.</a></li><li><a href="https://www.helpmestudybro.com/find-first-repeating-element-in-an-array/">Find First Repeating Element of an Array.</a></li><li><a href="https://www.helpmestudybro.com/program-to-find-majority-element-of-an-array/">Find Majority Element of an Array.</a></li><li><a href="https://www.helpmestudybro.com/program-to-check-for-balanced-parentheses/">Check whether given Parentheses String are Balanced Parentheses or Not.</a></li><li><a href="https://www.helpmestudybro.com/program-to-find-next-greater-element/">Next Greater Element</a></li><li><a href="https://www.helpmestudybro.com/minimum-number-of-bracket-reversals-needed-to-make-an-expression-balanced/">Find Minimum number of bracket reversals required to make an expression balanced.</a></li><li><a href="https://www.helpmestudybro.com/program-to-implement-queue-using-stacks/">Implement Queue Using Two Stacks.</a></li><li><a href="https://www.helpmestudybro.com/program-to-merge-overlapping-intervals/">Merge Overlapping Intervals using Stacks</a></li><li><a href="https://www.helpmestudybro.com/implement-stack-using-linked-list/">Implement Stack Using Linked List</a></li><li><a href="https://www.helpmestudybro.com/largest-rectangular-area-in-histogram/">Largest Rectangular Area in Histogram</a></li><li><a href="https://www.helpmestudybro.com/length-of-longest-valid-substring/">Length of Longest Valid Substring</a></li><li><a href="https://www.helpmestudybro.com/reverse-a-string-using-stack/">Reverse a String using Stack</a></li><li><a href="https://www.helpmestudybro.com/implement-two-stacks-in-a-single-array/">Implement two stacks in a single array</a></li><li><a href="https://www.helpmestudybro.com/print-bracket-number/">Print Bracket Number</a></li><li><a href="https://www.helpmestudybro.com/next-greater-frequency-element/">Next Greater Frequency Element</a></li></ol>



<hr class="wp-block-separator is-style-wide"/>



<p></p>
<p>The post <a href="https://www.helpmestudybro.com/find-first-non-repeating-character-of-the-string/">Find First Non-Repeating Character of the String</a> appeared first on <a href="https://www.helpmestudybro.com">Helpmestudybro</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Check Pangram String</title>
		<link>https://www.helpmestudybro.com/program-to-check-pangram-string/</link>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sun, 14 Jun 2020 09:36:28 +0000</pubDate>
				<category><![CDATA[data structure]]></category>
		<category><![CDATA[hashing]]></category>
		<category><![CDATA[strings]]></category>
		<guid isPermaLink="false">https://www.helpmestudybro.com/?p=226</guid>

					<description><![CDATA[<p>“Check Pangram String” is a very popular and basic problem of string data structure, asked in many technical interviews. Here, we are given a string of length ‘n’ and our task is to check whether given string is panagram string or not. Panagram String: A string is called Panagram String&#46;&#46;&#46;</p>
<p>The post <a href="https://www.helpmestudybro.com/program-to-check-pangram-string/">Check Pangram String</a> appeared first on <a href="https://www.helpmestudybro.com">Helpmestudybro</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>“<strong><em>Check Pangram String</em></strong>” is a very popular and basic problem of string data structure, asked in many technical interviews. Here, we are given a string of length ‘n’ and our task is to check whether given string is panagram string or not.</p>



<p><strong><em>Panagram String</em></strong>: A string is called Panagram String when it contains all the alphabets, irrespective of capitalization.</p>



<h4 class="wp-block-heading"><strong>Example (Check Pangram String):</strong></h4>



<pre class="wp-block-preformatted"><strong><em>INPUT:</em></strong>
Qwertyuiopasdfghjklzxcvbnm
<strong><em>OUTPUT:</em></strong>
The given string is a panagram string.</pre>



<p>The steps required to check pangram string or not are as follows:</p>



<ol type="1"><li>Create an hash array of size 123 and initialize it with 0. ( 123 size is taken because the ascii value of A-Z is from 65 to 90 and ascii value of a-z is from 97 to 122).</li><li>For every alphabet encountered, increment the hash array value for both small and capital alphabet ascii value index.</li><li>Finally, iterate over hash array from 65 to 90, if any value is zero, then string is not pangram, else string is pangram.</li></ol>



<p>The time complexity of this solution is O(n), for scanning the length of string.</p>



<h4 class="wp-block-heading"><strong>C++ Program to check pangram string is as follows:</strong></h4>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
/*C++ Program to check panagram string*/  
#include&lt;bits/stdc++.h&gt;  
#define size 1000  
using namespace std;  
int main()  
{  
    string str;  
    cout&lt;&lt;&quot;Enter a string:\n&quot;;  
    getline(cin,str);  
    int flag1 = 0;  
    /* Create a Hash Array */  
    int hash&#x5B;123] = {0};  
    /* For every alphabet, increment the  
       hash-array value for that character */  
    for(int i = 0; i &lt;  str.length(); i++)  
    {  
        if(str&#x5B;i]&gt;=&#039;a&#039; &amp;&amp; str&#x5B;i]&lt;=&#039;z&#039;)  
        {  
            hash&#x5B;str&#x5B;i]]++; /* Increment for small alphabet character */  
            hash&#x5B;str&#x5B;i]-32]++; /* Increment for capital alphabet character */  
        }  
        else if(str&#x5B;i]&gt;=&#039;A&#039; &amp;&amp; str&#x5B;i]&lt;=&#039;Z&#039;)  
        {  
            hash&#x5B;str&#x5B;i]]++; /* Increment for Capital alphabet character */  
            hash&#x5B;str&#x5B;i]+32]++; /* Increment for small alphabet character */  
        }  
    }  
    /* Check if any hash array value from 65 to 90 is 0 or not */  
    for(int i = 65; i &lt;= 90; i++)  
    {  
        /* If any hash-array value is 0, then  
        that string is not panagram*/  
        if(hash&#x5B;i] == 0 || hash&#x5B;i+32] == 0)  
        {  
            flag1 = 1;  
            cout&lt;&lt;&quot;\nThe given string is not pangram&quot;;  
            break;  
        }  
    }  
    if(flag1 == 0)  
    {  
        cout&lt;&lt;&quot;\nThe given string is pangram&quot;;  
    }  
}  
</pre></div>


<pre class="wp-block-preformatted"><strong><em>OUTPUT:</em></strong>
Enter a string: qwertyuiopasdfghjklzxcvbnm
The given string is panagram</pre>



<hr class="wp-block-separator is-style-wide"/>



<p class="has-medium-font-size"><strong>Related Posts:</strong></p>



<ol type="1"><li><a href="https://www.helpmestudybro.com/find-first-non-repeating-character-of-the-string/">Find First Non-Repeating Character of the String.</a></li><li><a href="https://www.helpmestudybro.com/find-most-frequent-element-in-an-array/">Find Most Frequent Element of the Array.</a></li><li><a href="https://www.helpmestudybro.com/program-to-find-pair-in-an-array-with-given-sum/">Find Pair in an Array with Given Sum.</a></li><li><a href="https://www.helpmestudybro.com/find-first-repeating-element-in-an-array/">Find First Repeating Element of an Array.</a></li><li><a href="https://www.helpmestudybro.com/program-to-find-majority-element-of-an-array/">Find Majority Element of an Array.</a></li><li><a href="https://www.helpmestudybro.com/find-most-frequent-character-in-string/">Find Most Frequent Character of the String.</a></li><li><a href="https://www.helpmestudybro.com/check-whether-two-strings-are-anagram-of-each-other/">Check Anagram Strings.</a></li><li><a href="https://www.helpmestudybro.com/check-whether-string-is-palindromic-anagram-or-not/">Check Whether Given String is Palindromic Anagram or Not.</a></li><li><a href="https://www.helpmestudybro.com/program-to-count-all-occurrences-of-an-element-in-an-array/">Program to count occurrences of a particular element in an array.</a></li><li><a href="https://www.helpmestudybro.com/program-to-implement-linear-search/">Program to search an element in an Array (Linear Search).</a></li><li><a href="https://www.helpmestudybro.com/program-to-rotate-array-elements/">Program to Rotate Array Elements by ‘D’ positions.</a></li><li><a href="https://www.helpmestudybro.com/find-most-frequent-element-in-an-array/">Program to find most frequent element in an array.</a></li><li><a href="https://www.helpmestudybro.com/program-to-find-pair-in-an-array-with-given-sum/">Program to find pair in an array with given sum.</a></li><li><a href="https://www.helpmestudybro.com/find-first-repeating-element-in-an-array/">Program to find first repeating element of an array.</a></li><li><a href="https://www.helpmestudybro.com/program-to-merge-two-sorted-arrays/">Program to merge two sorted arrays.</a></li><li><a href="https://www.helpmestudybro.com/find-missing-smallest-positive-number/">Program to find missing number in an array.</a></li><li><a href="https://www.helpmestudybro.com/program-to-check-if-array-is-sorted-or-not/">Program to sort if array is sorted.</a></li><li><a href="https://www.helpmestudybro.com/program-to-print-alternate-elements-of-an-array/">Program to print Alternate Elements of an Array.</a></li><li><a href="https://www.helpmestudybro.com/program-to-print-alternate-elements-of-an-array/">Program to swap kth element from beginning to kth elem</a><a href="https://www.helpmestudybro.com/program-to-swap-kth-element-from-beginning-with-kth-element-from-end/">ent from end in an Array.</a></li><li><a href="https://www.helpmestudybro.com/program-to-print-all-subarray-of-given-array/">Program to print all possible subarrays of the given array.</a></li></ol>



<hr class="wp-block-separator is-style-wide"/>
<p>The post <a href="https://www.helpmestudybro.com/program-to-check-pangram-string/">Check Pangram String</a> appeared first on <a href="https://www.helpmestudybro.com">Helpmestudybro</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Print all Palindromic Substrings of the given string &#8211; SET 1</title>
		<link>https://www.helpmestudybro.com/print-all-palindromic-substrings-of-the-given-string-set-1/</link>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sun, 14 Jun 2020 09:21:19 +0000</pubDate>
				<category><![CDATA[data structure]]></category>
		<category><![CDATA[strings]]></category>
		<guid isPermaLink="false">https://www.helpmestudybro.com/?p=223</guid>

					<description><![CDATA[<p>“Print all Palindromic Substrings of the given string” is a basic problem of string data structure asked in many technical interviews. Here, we are given a string of length ‘n’ and our task is to write a program to print all palindromic substrings of the given string. Palindromic Substring are&#46;&#46;&#46;</p>
<p>The post <a href="https://www.helpmestudybro.com/print-all-palindromic-substrings-of-the-given-string-set-1/">Print all Palindromic Substrings of the given string &#8211; SET 1</a> appeared first on <a href="https://www.helpmestudybro.com">Helpmestudybro</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>“<strong><em>Print all Palindromic Substrings of the given string</em></strong>” is a basic problem of string data structure asked in many technical interviews. Here, we are given a string of length ‘n’ and our task is to write a program to print all palindromic substrings of the given string.</p>



<p>Palindromic Substring are those substrings of given string, which are also palindrome.</p>



<p class="has-medium-font-size"><strong>Example:</strong></p>



<pre class="wp-block-preformatted"><strong><em>INPUT:</em></strong>
ABAPXP
<strong><em>OUTPUT:</em></strong>
A
ABA
B
A
P
PXP
X
P
P</pre>



<p>The steps required to print all palindromic substrings of the given string are as follows:</p>



<ol type="1"><li>Three loops will be taken.</li><li>The outer loop will be used for starting character of palindromic substring.</li><li>The middle loop will be used for ending character of the palindromic substring.</li><li>The inner most loop will be used for creating the substring from starting character (outer loop) to ending character (middle loop).</li><li>For every substring created in inner loop, we will check whether it is palindromic string or not. If it is palindromic string, we will print it.</li></ol>



<p>The time complexity of this solution is O(n<sup>4</sup>), where &#8216;n&#8217; is the length of the string.</p>



<h4 class="wp-block-heading"><strong>C++ Program to print all palindromic substrings of the given string is as follows:</strong></h4>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
/* C++ Program to print all palindromic substrings of the given string */  
#include&lt;bits/stdc++.h&gt;  
using namespace std;  
/* Function to check whether string is palindrome or not */  
bool palindromic(string str)  
{  
    string temp = str;  
    reverse(temp.begin(),temp.end());  
    if(str == temp)  
    return true;  
    return false;  
}  
int main()  
{  
    string str;  
    cout&lt;&lt;&quot;Enter a string:\n&quot;;  
    cin&gt;&gt;str;  
    cout&lt;&lt;&quot;The Palindromic substring are:\n&quot;;  
    int size = str.length();  
    /* Outer loop for starting character of substring */  
    for(int i = 0; i &lt; size; i++)  
    {  
        /* Middle loop for ending character of substring */  
        for(int j = i; j &lt; size; j++)  
        {  
            string substring = &quot;&quot;;  
            /* Inner loop for creating substring */  
            for(int k = i; k &lt;= j; k++)  
            {  
                substring = substring + str&#x5B;k];  
            }  
            /* Function to check whether string is palindrome or not */  
            if(palindromic(substring))  
            cout&lt;&lt;substring&lt;&lt;&quot;\n&quot;;  
        }  
    }  
}  

</pre></div>


<pre class="wp-block-preformatted"><strong><em>OUTPUT:</em></strong>
Enter a string:&nbsp;
ABAPXP
The Palindromic substring are:
A
ABA
B
A
P
PXP
X
P</pre>



<hr class="wp-block-separator is-style-wide"/>



<p class="has-medium-font-size"><strong>Related Posts:</strong></p>



<ol type="1"><li><a href="https://www.helpmestudybro.com/program-to-check-whether-given-string-is-panagram-string-or-not/"><strong>Program to check Panagram String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-find-first-non-repeating-character-of-the-string/"><strong>Program to find first non-repeating character of the String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-reverse-a-string/"><strong>Program to Reverse a String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-count-number-of-characters-of-the-character-array/"><strong>Program to Count Number of Characters in a String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-count-frequency-of-particular-character-of-the-string/"><strong>Program to Count frequency of particular character of the String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-program-to-count-number-of-vowels-and-consonants-in-the-string/"><strong>Program to Count Number of Vowels and Number of Consonants in a String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-checking-type-of-string/"><strong>Program to Check if String is alphabetic or numeric or alpha-numeric.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-copy-one-character-array-to-another-character-array/"><strong>Program to copy one String to Another.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-concatenate-two-character-arrays/"><strong>Program to Concatenate Two Strings.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-check-palindrome-string/"><strong>Program to Check whether given String is Palindrome or not.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-convert-lower-case-alphabets-to-upper-case-and-vice-versa-in-string/"><strong>Program to convert lower-case alphabet to upper-case alphabet and vice-versa.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-count-total-number-of-words-in-the-string/"><strong>Program to count total number of words in a string.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-find-smallest-and-largest-word-of-the-string/"><strong>Program to find smallest and largest word of the String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-find-most-frequent-character-of-the-string/"><strong>Program to find Most Frequent Character of the String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-remove-all-blank-spaces-from-the-string/"><strong>Program to Remove all the blank spaces from the String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-check-isogram-string-or-not/"><strong>Program to check if String is isogram or not.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-reverse-each-word-of-the-string/"><strong>Program to Reverse Each word of the String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-print-all-substring-of-the-given-string/"><strong>Program to Print All the Substring of the Given String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-find-longest-palindromic-substring-set-1/"><strong>Program to find longest palindromic Substring.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-check-anagram-string/"><strong>Program to check Anagram Strings.</strong></a></li></ol>



<hr class="wp-block-separator is-style-wide"/>



<p></p>
<p>The post <a href="https://www.helpmestudybro.com/print-all-palindromic-substrings-of-the-given-string-set-1/">Print all Palindromic Substrings of the given string &#8211; SET 1</a> appeared first on <a href="https://www.helpmestudybro.com">Helpmestudybro</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Check Whether String is Palindromic Anagram or Not</title>
		<link>https://www.helpmestudybro.com/check-whether-string-is-palindromic-anagram-or-not/</link>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sat, 13 Jun 2020 10:58:13 +0000</pubDate>
				<category><![CDATA[data structure]]></category>
		<category><![CDATA[hashing]]></category>
		<category><![CDATA[strings]]></category>
		<guid isPermaLink="false">https://www.helpmestudybro.com/?p=218</guid>

					<description><![CDATA[<p>“Check Whether String is Palindromic Anagram or Not” is popular interview problem asked in many technical and algorithmic interviews. Here, we are given a string and our task is to write a program to check whether string is Palindromic Anagram or not.&#160; A&#160;palindrome Anagram string&#160;is a string which can be&#46;&#46;&#46;</p>
<p>The post <a href="https://www.helpmestudybro.com/check-whether-string-is-palindromic-anagram-or-not/">Check Whether String is Palindromic Anagram or Not</a> appeared first on <a href="https://www.helpmestudybro.com">Helpmestudybro</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>“<strong><em>Check Whether String is Palindromic Anagram or Not</em></strong>” is popular interview problem asked in many technical and algorithmic interviews. Here, we are given a string and our task is to write a program to check whether string is Palindromic Anagram or not.&nbsp;</p>



<p>A&nbsp;<em><strong>palindrome Anagram string</strong></em>&nbsp;is a string which can be rearranged to form a palindrome. In technical language, any permutation of given string is palindrome.</p>



<p class="has-medium-font-size"><strong>Example:</strong></p>



<pre class="wp-block-preformatted"><strong><em>INPUT:</em></strong>
AABBC
<strong><em>OUTPUT:</em></strong>
AABBC is palindromic Anagram
<strong><em>Explanation:</em></strong>
AABBC can be rearranged into ABCBA which is an palindrome.</pre>



<p class="has-medium-font-size"><strong>Properties of Palindromic string</strong></p>



<p>1. A&nbsp;<em>palindromic string</em>&nbsp;is a string that can read same from forward or backward. Examples are: &#8220;MADAM&#8221;, &#8220;NITIN&#8221;.</p>



<p>2. If the string is even length string, then the frequency of all the characters of the string must be even, then only it can be palindromic anagram.</p>



<p>3. If the string is odd length string, then the frequency of all the characters of the string must be even, except one character, which will have odd frequency, then only it can be palindromic anagram.</p>



<p class="has-medium-font-size"><strong>SOLUTION:</strong><strong></strong></p>



<p>The steps required to check whether string is palindromic anagram or not are as follows:</p>



<ol type="1"><li>Check whether the string is odd length of even length.</li><li>Store the frequency of every character of string in the hash map.</li><li>If the string is even length string, traverse the hashmap and check whether frequency of all the character is even or not. If frequency of each character in the string is even, then the given string is palindromic anagram string. Else, the given string is not palindromic anagram.</li><li>If the string is odd length string, traverse the hashmap and check whether frequency of all the character is even or not,&nbsp;except one character which will have odd frequency. If this condition holds,&nbsp;then the given string is palindromic anagram string. Else, the given string is not palindromic anagram.</li></ol>



<p>The time complexity of this solution is O(nlogn), where &#8216;n&#8217; is the length of the string and &#8216;logn&#8217; for inserting and searching in the map.</p>



<h4 class="wp-block-heading"><strong>C++ Program to check whether string is Palindromic Anagram or not is as follows:&nbsp;</strong></h4>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
/* C++ Program to check whether string is Palindromic Anagram or not */
#include&lt;bits/stdc++.h&gt;
using namespace std;
void checkPalindrome(string str)
{
    int len = str.length();
    int i;
    map&lt;int,int&gt;mp;
    map&lt;int,int&gt; :: iterator itr;
    for(i=0;i&lt;len;i++)
    mp&#x5B;str&#x5B;i]]++;
    // If the length of string is even, then
    // to form a palindrome all the frequency
    // of characters must be even
    if(len % 2 == 0)
    {
        for(itr = mp.begin() ; itr!=mp.end() ; ++itr)
        {
            if(itr -&gt; second % 2 != 0)
            {
                cout&lt;&lt;&quot;The string is not palindrome Anagram\n&quot;;
                return;
            }
        }
        cout&lt;&lt;&quot;The string is palindrome Anagram\n&quot;;
    }
    // Else,to form a palindrome all the frequency
    // of characters must be even except frequency
    // of one character
    else
    {
        int flag = 0;
        for(itr = mp.begin() ; itr!=mp.end() ; ++itr)
        {
            if(itr -&gt; second % 2 != 0)
            {
                flag++;
            }
        }
        // only one character with odd frequency
        if(flag == 1)
            cout&lt;&lt;&quot;The string is palindrome Anagram\n&quot;;
        else
            cout&lt;&lt;&quot;The string is not palindrome Anagram\n&quot;;
    }
}
int main()
{
    string str;
    /* Scan String */
    cout&lt;&lt;&quot;Enter a string:\n&quot;;
    cin&gt;&gt;str;
    /* Check if string is Palindromic Anagram */
    checkPalindrome(str);
}

</pre></div>


<pre class="wp-block-preformatted"><strong><em>Output:</em></strong>
Enter a string:
AABBCCD
The string is palindrome Anagram</pre>



<hr class="wp-block-separator is-style-wide"/>



<p class="has-medium-font-size"><strong>Related Posts:</strong></p>



<ol type="1"><li><a href="https://www.helpmestudybro.com/program-to-check-pangram-string/">Check Whether Given String is Panagram or Not.</a></li><li><a href="https://www.helpmestudybro.com/find-first-non-repeating-character-of-the-string/">Find First Non-Repeating Character of the String.</a></li><li><a href="https://www.helpmestudybro.com/find-most-frequent-element-in-an-array/">Find Most Frequent Element of the Array.</a></li><li><a href="https://www.helpmestudybro.com/program-to-find-pair-in-an-array-with-given-sum/">Find Pair in an Array with Given Sum.</a></li><li><a href="https://www.helpmestudybro.com/find-first-repeating-element-in-an-array/">Find First Repeating Element of an Array.</a></li><li><a href="https://www.helpmestudybro.com/program-to-find-majority-element-of-an-array/">Find Majority Element of an Array.</a></li><li><a href="https://www.helpmestudybro.com/find-most-frequent-character-in-string/">Find Most Frequent Character of the String.</a></li><li><a href="https://www.helpmestudybro.com/check-whether-two-strings-are-anagram-of-each-other/">Check Anagram Strings.</a></li><li><a href="https://www.helpmestudybro.com/minimum-number-of-bracket-reversals-needed-to-make-an-expression-balanced/">Find Minimum number of bracket reversals required to make an expression balanced.</a></li><li><a href="https://www.helpmestudybro.com/program-to-implement-queue-using-stacks/">Implement Queue Using Two Stacks.</a></li><li><a href="https://www.helpmestudybro.com/program-to-merge-overlapping-intervals/">Merge Overlapping Intervals using Stacks</a></li><li><a href="https://www.helpmestudybro.com/implement-stack-using-linked-list/">Implement Stack Using Linked List</a></li><li><a href="https://www.helpmestudybro.com/largest-rectangular-area-in-histogram/">Largest Rectangular Area in Histogram</a></li><li><a href="https://www.helpmestudybro.com/length-of-longest-valid-substring/">Length of Longest Valid Substring</a></li><li><a href="https://www.helpmestudybro.com/reverse-a-string-using-stack/">Reverse a String using Stack</a></li><li><a href="https://www.helpmestudybro.com/implement-two-stacks-in-a-single-array/">Implement two stacks in a single array</a></li><li><a href="https://www.helpmestudybro.com/print-bracket-number/">Print Bracket Number</a></li><li><a href="https://www.helpmestudybro.com/program-to-compute-lcm-of-two-numbers/">Program to find LCM of two numbers.</a></li><li><a href="https://www.helpmestudybro.com/next-greater-frequency-element/">Next Greater Frequency Element</a></li><li><a href="https://www.helpmestudybro.com/sort-a-stack-using-temporary-stack/">Sort a Stack using Temporary Stack</a></li></ol>



<hr class="wp-block-separator is-style-wide"/>
<p>The post <a href="https://www.helpmestudybro.com/check-whether-string-is-palindromic-anagram-or-not/">Check Whether String is Palindromic Anagram or Not</a> appeared first on <a href="https://www.helpmestudybro.com">Helpmestudybro</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Check if Two Strings are Rotations of each other</title>
		<link>https://www.helpmestudybro.com/check-if-two-strings-are-rotations-of-each-other/</link>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sat, 13 Jun 2020 10:41:30 +0000</pubDate>
				<category><![CDATA[data structure]]></category>
		<category><![CDATA[strings]]></category>
		<guid isPermaLink="false">https://www.helpmestudybro.com/?p=215</guid>

					<description><![CDATA[<p>“check if two strings are rotations of each other” is a popular interview problem based on string data structure. Here, we are given two strings str1 and str2 and our task is to check whether these two strings are in rotation of each other or not. Example: INPUT: String 1:&#46;&#46;&#46;</p>
<p>The post <a href="https://www.helpmestudybro.com/check-if-two-strings-are-rotations-of-each-other/">Check if Two Strings are Rotations of each other</a> appeared first on <a href="https://www.helpmestudybro.com">Helpmestudybro</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>“<strong><em>check if two strings are rotations of each other</em></strong>” is a popular interview problem based on string data structure. Here, we are given two strings str1 and str2 and our task is to check whether these two strings are in rotation of each other or not.</p>



<p class="has-medium-font-size"><strong>Example:</strong></p>



<pre class="wp-block-preformatted"><strong><em>INPUT:</em></strong>
String 1: “abcd”
String 2: “bcda”
<strong><em>OUTPUT:</em></strong>
The strings are rotation of each other
<strong><em>INPUT:</em></strong>
String 1: “abcd”
String 2: “bacd”
<strong><em>OUTPUT:</em></strong>
The strings are not in rotation of each other.</pre>



<p>There are various methods to check if two strings are rotations of each other. Some of them are:</p>



<h4 class="wp-block-heading"><strong>METHOD 1: Brute-Force Approach to check if two strings are rotations of each other</strong></h4>



<p>The brute-force solution of the given problem is to generate all possible rotation of first string and compare each rotation of the first string with second string. If any rotation of first string is equal to second string, then the given strings are rotation of each other. If no possible rotation of first string is equal to second string, then the given strings are not rotation of each other.The time complexity of this solution is O(n<sup>2</sup>), where ‘n’ is the length of the string.</p>



<h4 class="wp-block-heading"><strong>C++ Program to check if two strings are rotations of each other is as follows:</strong></h4>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
/* C++ Program to check if two strings are rotations of each other */
#include&lt;bits/stdc++.h&gt;
using namespace std;
void checkStringRotation(string str1,string str2)
{
    /* Check String length */
    if(str1.length()!=str2.length())
    cout&lt;&lt;&quot;Strings are not in rotation of each other\n&quot;;


    for(int i = 0 ; i&lt; str1.length() ; i++)
    {
        char ch = str1&#x5B;0];


        // Erase first character of string str1
        str1.erase(str1.begin()+0);


        // Push it back at the end of string
        str1.push_back(ch);

        /* If any rotation is equal to string2, 
           then it is rotation of each other */
        if(str1 == str2)
        {
            cout&lt;&lt;&quot;Strings are in rotation of each other\n&quot;;
            return;
        }
    }
    cout&lt;&lt;&quot;Strings are not in rotation of each other\n&quot;;
}
int main()
{
    string str1,str2;
    
    /* Scan strings */
    cout&lt;&lt;&quot;Enter string 1:\n&quot;;
    cin&gt;&gt;str1;
    cout&lt;&lt;&quot;Enter string 2:\n&quot;;
    cin&gt;&gt;str2;
    
    /* Function to check string rotation */
    checkStringRotation(str1,str2);
}

</pre></div>


<pre class="wp-block-preformatted"><strong><em>OUTPUT:</em></strong>
Enter string 1:&nbsp;
ABBA
Enter string 2:
BBAA
Strings are in rotation of each other</pre>



<h4 class="wp-block-heading"><strong>METHOD 2: Efficient Method to check if two strings are rotations of each other</strong></h4>



<p>The efficient solution of the given problem is to concatenate first string with itself and called it as final_string. Then, search second in final_string. If second string is present in final_string as substring, then, the given strings are rotation of each other. Else, the given strings are not rotation of each other.</p>



<p>The time complexity of this solution is O(n^2), where ‘n’ is&nbsp;the length of the string.</p>



<h4 class="wp-block-heading"><strong>C++ Program to check if two strings are rotations of each other is as follows:</strong></h4>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
/* C++ Program to check if two strings are rotations of each other */
#include&lt;bits/stdc++.h&gt;
using namespace std;
void checkStringRotation(string str1,string str2)
{
    // If the length of the string are not same,
    // The answer is false.
    
     if(str1.length()!=str2.length())
        cout&lt;&lt;&quot;Strings are not in rotation of each other\n&quot;;
    // Concatenate the string with itself
    string final_string = str1 + str1;
    
    if(final_string.find(str2) != string::npos)
        cout&lt;&lt;&quot;Strings are in rotation of each other\n&quot;;
    else
        cout&lt;&lt;&quot;Strings are not in rotation of each other\n&quot;;
}
int main()
{
    string str1,str2;
    
    /* Scan String */
    cout&lt;&lt;&quot;Enter String1:\n&quot;;
    cin&gt;&gt;str1;
    cout&lt;&lt;&quot;Enter String2:\n&quot;;
    cin&gt;&gt;str2;
    /* Function to check if strings are rotation of each other */
    checkStringRotation(str1,str2);
}

</pre></div>


<pre class="wp-block-preformatted"><strong><em>OUTPUT:</em></strong>
Enter string 1:&nbsp;
ABBA
Enter string 2:
BBAA
Strings are in rotation of each other
&nbsp;</pre>



<hr class="wp-block-separator is-style-wide"/>



<p class="has-medium-font-size"><strong>Related Posts:</strong></p>



<ol type="1"><li><a href="https://www.helpmestudybro.com/program-to-check-whether-string-is-palindromic-anagram-or-not/"><strong>Program to check Palindromic Anagram.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-print-all-palindromic-substrings-of-the-string-set-1/"><strong>Program to print all the Palindromic Substring of the String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-check-whether-given-string-is-panagram-string-or-not/"><strong>Program to check Panagram String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-find-first-non-repeating-character-of-the-string/"><strong>Program to find first non-repeating character of the String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-reverse-a-string/"><strong>Program to Reverse a String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-count-number-of-characters-of-the-character-array/"><strong>Program to Count Number of Characters in a String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-count-frequency-of-particular-character-of-the-string/"><strong>Program to Count frequency of particular character of the String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-program-to-count-number-of-vowels-and-consonants-in-the-string/"><strong>Program to Count Number of Vowels and Number of Consonants in a String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-checking-type-of-string/"><strong>Program to Check if String is alphabetic or numeric or alpha-numeric.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-copy-one-character-array-to-another-character-array/"><strong>Program to copy one String to Another.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-concatenate-two-character-arrays/"><strong>Program to Concatenate Two Strings.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-check-palindrome-string/"><strong>Program to Check whether given String is Palindrome or not.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-convert-lower-case-alphabets-to-upper-case-and-vice-versa-in-string/"><strong>Program to convert lower-case alphabet to upper-case alphabet and vice-versa.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-count-total-number-of-words-in-the-string/"><strong>Program to count total number of words in a string.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-find-smallest-and-largest-word-of-the-string/"><strong>Program to find smallest and largest word of the String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-find-most-frequent-character-of-the-string/"><strong>Program to find Most Frequent Character of the String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-remove-all-blank-spaces-from-the-string/"><strong>Program to Remove all the blank spaces from the String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-check-isogram-string-or-not/"><strong>Program to check if String is isogram or not.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-reverse-each-word-of-the-string/"><strong>Program to Reverse Each word of the String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-print-all-substring-of-the-given-string/"><strong>Program to Print All the Substring of the Given String.</strong></a></li></ol>



<hr class="wp-block-separator is-style-wide"/>
<p>The post <a href="https://www.helpmestudybro.com/check-if-two-strings-are-rotations-of-each-other/">Check if Two Strings are Rotations of each other</a> appeared first on <a href="https://www.helpmestudybro.com">Helpmestudybro</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Check whether two strings are anagram of each other</title>
		<link>https://www.helpmestudybro.com/check-whether-two-strings-are-anagram-of-each-other/</link>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sat, 13 Jun 2020 10:28:22 +0000</pubDate>
				<category><![CDATA[data structure]]></category>
		<category><![CDATA[hashing]]></category>
		<category><![CDATA[strings]]></category>
		<guid isPermaLink="false">https://www.helpmestudybro.com/?p=212</guid>

					<description><![CDATA[<p>“Check whether two strings are anagram of each other” is a very important and one of the most asked technical interview problems based on string data structure.&#160; Here, we are given two strings and our task is to check whether two strings are anagram of each other or not.&#160; Two&#46;&#46;&#46;</p>
<p>The post <a href="https://www.helpmestudybro.com/check-whether-two-strings-are-anagram-of-each-other/">Check whether two strings are anagram of each other</a> appeared first on <a href="https://www.helpmestudybro.com">Helpmestudybro</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>“<strong><em>Check whether two strings are anagram of each other</em></strong>” is a very important and one of the most asked technical interview problems based on string data structure.&nbsp;</p>



<p>Here, we are given two strings and our task is to check whether two strings are anagram of each other or not.&nbsp;</p>



<p>Two strings are called&nbsp;<strong><em>anagram</em></strong>&nbsp;of each other if the characters and number of characters of the strings are same, but the order of characters of the strings does not matter. In Technical language, two strings are called anagram of each other if they are permutation of each other.</p>



<p class="has-medium-font-size"><strong>Example:</strong></p>



<pre class="wp-block-preformatted"><strong><em>INPUT:</em></strong>
Str1 = “abcdfr”
Str2 = “frdcba”
<strong><em>OUTPUT:</em></strong>
The strings are anagram of each other
<strong><em>Explanation:</em></strong>
“abcdfr” can be rearranged to form string “frdcba”.
<strong><em>INPUT:</em></strong>
Str1 = “abcdfr”
Str2 = “frdcbx”
<strong><em>OUTPUT:</em></strong>
The strings are anagram of each other
<strong><em>Explanation:</em></strong>
“abcdfr” can be rearranged to form string “frdcbx”</pre>



<hr class="wp-block-separator"/>



<p>There are various ways to&nbsp;check whether two strings are anagram of each other. Some of them are:</p>



<h4 class="wp-block-heading"><strong>METHOD&nbsp; 1: Sort and compare Technique to check whether two strings are anagram of each other</strong></h4>



<p>The brute-force solution of the given problem is to sort the given strings and then check whether both the strings are equal or not after sorting. If they are equal, then the strings are anagram of each other. Else,&nbsp;strings are not anagram of each other.</p>



<p>The time complexity of this solution is O(nlogn), based on type of sorting algorithm used.</p>



<h4 class="wp-block-heading"><strong>C++ Program to check whether two strings are anagram of each other is as follows:</strong></h4>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
/* C++ Program to check whether two strings are anagram of each other */
#include&lt;bits/stdc++.h&gt;
using namespace std;
void checkAnagram(string str1,string str2)
{
    /* Sort the Strings */
    sort(str1.begin(),str1.end());
    sort(str2.begin(),str2.end());
    
    /* If Str1 is equal to Str2, then 
       strings are anagram of each other*/
    if(str1 == str2)
        cout&lt;&lt;&quot;Strings are Anagram of each other\n&quot;;
    else
        cout&lt;&lt;&quot;Strings are not Anagram of each other\n&quot;;
}
int main()
{
    string str1,str2;
    
    /* Scan the string */ 
    cout&lt;&lt;&quot;Enter first string:\n&quot;;
    cin&gt;&gt;str1;
    cout&lt;&lt;&quot;Enter second string:\n&quot;;
    cin&gt;&gt;str2;
    
    /* Function to check Anagram Strings */
    checkAnagram(str1,str2);
}
</pre></div>


<pre class="wp-block-preformatted"><strong><em>OUTPUT:</em></strong>
Enter first string:
ABBCD
Enter second string:
BBCAD
Strings are Anagram of each other</pre>



<h4 class="wp-block-heading"><strong>METHOD 2: Hashing Based Approach to check whether two strings are anagram of each other</strong></h4>



<p>Hashing based solution is a more time efficient approach.&nbsp;</p>



<p>As we know, we have 256 ASCII character set(0-255).</p>



<p>We will create an integer array of size 256 and initialize it with zero.</p>



<p>We will traverse the first string and for every character encountered in first string, we will&nbsp;<strong><u>increment</u></strong>&nbsp;the ascii position value in the array.</p>



<p>Now, we will traverse the second string and for every character encountered in the second string, we will&nbsp;<strong><u>decrement</u></strong>&nbsp;the ascii position value in the array.</p>



<p>Finally, we will check whether all the values of the array are zero or not. If they are zero, then the given two strings are anagram of each other, else they are not anagram of each other.</p>



<p>The time complexity of Hash Based solution is O(n).</p>



<h4 class="wp-block-heading"><strong><strong>C++ Program to check whether two strings are anagram of each other is as follows:</strong></strong></h4>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
/* C++ Program to check whether two strings are anagram of each other */
#include&lt;bits/stdc++.h&gt;
using namespace std;
void checkAnagram(string str1,string str2)
{
   int arr&#x5B;256] = {0};
   int i;
   for(i = 0; i &lt; str1.length(); i++)
   {
        arr&#x5B;str1&#x5B;i]]++;
   }
   for(i = 0; i &lt; str2.length(); i++)
   {
        arr&#x5B;str2&#x5B;i]]--;
   }
   for(i = 0; i &lt; 256; i++)
   {
       if(arr&#x5B;i] != 0)
       {
            cout&lt;&lt;&quot;The strings are not anagram of each other\n&quot;;
           return;
       }
   }
    cout&lt;&lt;&quot;The strings are anagram of each other\n&quot;;
}
int main()
{
    string str1,str2;
    
    /* Scan the string */ 
    cout&lt;&lt;&quot;Enter first string:\n&quot;;
    cin&gt;&gt;str1;
    cout&lt;&lt;&quot;Enter second string:\n&quot;;
    cin&gt;&gt;str2;
    
    /* Function to check Anagram Strings */
    checkAnagram(str1,str2);
}
</pre></div>


<pre class="wp-block-preformatted"><strong><em>OUTPUT:</em></strong>
Enter first string:
ABBCD
Enter second string:
BBCAD
Strings are Anagram of each other</pre>



<hr class="wp-block-separator is-style-wide"/>



<p class="has-medium-font-size"><strong>Related Posts:</strong></p>



<ol type="1"><li><a href="https://www.helpmestudybro.com/check-whether-string-is-palindromic-anagram-or-not/">Check Whether Given String is Palindromic Anagram or Not.</a></li><li><a href="https://www.helpmestudybro.com/program-to-check-pangram-string/">Check Whether Given String is Panagram or Not.</a></li><li><a href="https://www.helpmestudybro.com/find-first-non-repeating-character-of-the-string/">Find First Non-Repeating Character of the String.</a></li><li><a href="https://www.helpmestudybro.com/find-most-frequent-element-in-an-array/">Find Most Frequent Element of the Array.</a></li><li><a href="https://www.helpmestudybro.com/program-to-find-pair-in-an-array-with-given-sum/">Find Pair in an Array with Given Sum.</a></li><li><a href="https://www.helpmestudybro.com/find-first-repeating-element-in-an-array/">Find First Repeating Element of an Array.</a></li><li><a href="https://www.helpmestudybro.com/program-to-find-majority-element-of-an-array/">Find Majority Element of an Array.</a></li><li><a href="https://www.helpmestudybro.com/find-most-frequent-character-in-string/">Find Most Frequent Character of the String.</a></li><li><a href="https://www.helpmestudybro.com/find-first-repeating-element-in-an-array/">Program to find first repeating element of an array.</a></li><li><a href="https://www.helpmestudybro.com/program-to-merge-two-sorted-arrays/">Program to merge two sorted arrays.</a></li><li><a href="https://www.helpmestudybro.com/find-missing-smallest-positive-number/">Program to find missing number in an array.</a></li><li><a href="https://www.helpmestudybro.com/program-to-check-if-array-is-sorted-or-not/">Program to sort if array is sorted.</a></li><li><a href="https://www.helpmestudybro.com/program-to-print-alternate-elements-of-an-array/">Program to print Alternate Elements of an Array.</a></li><li><a href="https://www.helpmestudybro.com/program-to-print-alternate-elements-of-an-array/">Program to swap kth element from beginning to kth elem</a><a href="https://www.helpmestudybro.com/program-to-swap-kth-element-from-beginning-with-kth-element-from-end/">ent from end in an Array.</a></li><li><a href="https://www.helpmestudybro.com/program-to-print-all-subarray-of-given-array/">Program to print all possible subarrays of the given array.</a></li><li><a href="https://www.helpmestudybro.com/program-to-print-kth-smallest-and-kth-largest-element-of-an-array/">Program to print kth smallest and kth largest element of an Array.</a></li><li><a href="https://www.helpmestudybro.com/program-to-find-equilibrium-index-of-an-array/">Program to find equilibrium index of an Array.</a></li><li><a href="https://www.helpmestudybro.com/program-to-find-majority-element-of-an-array/">Program to find majority element of an Array.</a></li><li><a href="https://www.helpmestudybro.com/program-to-find-mean-of-an-array/">Program to find mean of the Array.</a></li><li><a href="https://www.helpmestudybro.com/program-to-check-idempotent-matrix/">Program to check Idempotent Matrix.</a></li></ol>



<hr class="wp-block-separator is-style-wide"/>
<p>The post <a href="https://www.helpmestudybro.com/check-whether-two-strings-are-anagram-of-each-other/">Check whether two strings are anagram of each other</a> appeared first on <a href="https://www.helpmestudybro.com">Helpmestudybro</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Find Longest Palindromic Substring &#8211; SET 1</title>
		<link>https://www.helpmestudybro.com/find-longest-palindromic-substring-set-1/</link>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sat, 13 Jun 2020 09:58:54 +0000</pubDate>
				<category><![CDATA[data structure]]></category>
		<category><![CDATA[strings]]></category>
		<guid isPermaLink="false">https://www.helpmestudybro.com/?p=209</guid>

					<description><![CDATA[<p>“Find Longest Palindromic Substring” is an important and one of the interview favourite problem of string data structure. Here, we are given a string and our task is to write a program to find the longest palindromic substring in the given string.  Longest Palindromic Substring: Longest Palindromic Substring of the given string&#46;&#46;&#46;</p>
<p>The post <a href="https://www.helpmestudybro.com/find-longest-palindromic-substring-set-1/">Find Longest Palindromic Substring &#8211; SET 1</a> appeared first on <a href="https://www.helpmestudybro.com">Helpmestudybro</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>“<strong><em>Find Longest Palindromic Substring</em></strong>” is an important and one of the interview favourite problem of string data structure. Here, we are given a string and our task is to write a program to find the longest palindromic substring in the given string. </p>



<p><strong>Longest Palindromic Substring</strong>: Longest Palindromic Substring of the given string is a substring of the string which is also palindrome. We need to find the largest possible substring of such kind.</p>



<p>If there is no palindromic substring of length greater than 1 present in the string, simple return first character of the string.</p>



<p class="has-medium-font-size"><strong>Example:</strong></p>



<pre class="wp-block-preformatted"><strong><em>INPUT:</em></strong>
abcba
<strong><em>OUTPUT:</em></strong>
Longest Palindromic Substring: abcba
<strong><em>Explanation:</em></strong>
“abcba” is itself palindromic string.
<strong><em>INPUT:</em></strong>
abcded
<strong><em>OUTPUT:</em></strong>
Longest Palindromic Substring: ded
<strong><em>Explanation:</em></strong>
“ded” is the longest substring in the string “abcded” which is palindromic.</pre>



<p>In this post, we will discuss the brute-force approach to get the solution. The steps required to find longest palindromic substring are:</p>



<ol type="1"><li>Initialize an empty resultant  string.</li><li>Generate all possible substring of the given string.</li><li>For every substring generated, check whether it is palindrome or not.</li><li>If it is palindrome, check its length with resultant string length.</li><li>If it’s greater, assign the resultant string with generated palindromic substring.</li></ol>



<p>The time complexity of this solution is O(n<sup>4</sup>) for generating all substring and check palindrome.</p>



<h4 class="wp-block-heading"><strong>C++ Program to Find Longest Palindromic Substring in a given string is as follows:</strong></h4>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
/* C++ Program to Find Longest Palindromic Substring in a given string */
#include&lt;bits/stdc++.h&gt;
using namespace std;
bool isPalindrome(string str)
{
    string temp = str;
    reverse(temp.begin(),temp.end());
    return (temp==str) ? true : false ;
}
void longestPalindrome(string str)
{
    // Single characterb is itself a palindrome
    string res = str.substr(0,1);
    //Generate all possible substrings of a given string
    for(int i = 0 ; i &lt; str.length() ; i++)
    {
        for(int j = i ; j &lt; str.length() ; j++)
        {
            string substring = &quot;&quot;;
            for(int k = i ; k &lt;= j ; k++)
            {
                substring = substring + str&#x5B;k];
            }
            // check whether generated substring is palindrome or not
            bool checkPalindrome = isPalindrome(substring);
            // If palindrome, compare its length with resultant palindrome
            if(checkPalindrome)
            {
                if(substring.length() &gt; res.length())
                res = substring;
            }
        }
    }
    cout&lt;&lt;&quot;Longest Palindromic Substring: &quot;&lt;&lt;res&lt;&lt;&quot;\n&quot;;
}
int main()
{
    string str;
    
    /* Scan String */
    cout&lt;&lt;&quot;Enter a string:\n&quot;;
    cin&gt;&gt;str;
    
    /* Function to find Longest Palindromic Substring */
    longestPalindrome(str);
}

</pre></div>


<pre class="wp-block-preformatted"><strong><em>OUTPUT:</em></strong>
Enter a string:
abcded
Longest Palindromic Substring: ded</pre>



<hr class="wp-block-separator is-style-wide"/>



<p class="has-medium-font-size"><strong>Related Posts:</strong></p>



<ol type="1"><li><a href="https://www.helpmestudybro.com/program-to-check-anagram-string/"><strong>Program to check Anagram Strings.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-check-whether-two-strings-are-rotation-of-each-other-or-not/"><strong>Program to check whether two Strings are Rotation of each other or not.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-check-whether-string-is-palindromic-anagram-or-not/"><strong>Program to check Palindromic Anagram.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-print-all-palindromic-substrings-of-the-string-set-1/"><strong>Program to print all the Palindromic Substring of the String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-check-whether-given-string-is-panagram-string-or-not/"><strong>Program to check Panagram String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-find-first-non-repeating-character-of-the-string/"><strong>Program to find first non-repeating character of the String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-reverse-a-string/"><strong>Program to Reverse a String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-count-number-of-characters-of-the-character-array/"><strong>Program to Count Number of Characters in a String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-count-frequency-of-particular-character-of-the-string/"><strong>Program to Count frequency of particular character of the String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-program-to-count-number-of-vowels-and-consonants-in-the-string/"><strong>Program to Count Number of Vowels and Number of Consonants in a String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-checking-type-of-string/"><strong>Program to Check if String is alphabetic or numeric or alpha-numeric.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-copy-one-character-array-to-another-character-array/"><strong>Program to copy one String to Another.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-concatenate-two-character-arrays/"><strong>Program to Concatenate Two Strings.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-check-palindrome-string/"><strong>Program to Check whether given String is Palindrome or not.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-convert-lower-case-alphabets-to-upper-case-and-vice-versa-in-string/"><strong>Program to convert lower-case alphabet to upper-case alphabet and vice-versa.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-count-total-number-of-words-in-the-string/"><strong>Program to count total number of words in a string.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-find-smallest-and-largest-word-of-the-string/"><strong>Program to find smallest and largest word of the String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-find-most-frequent-character-of-the-string/"><strong>Program to find Most Frequent Character of the String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-remove-all-blank-spaces-from-the-string/"><strong>Program to Remove all the blank spaces from the String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-check-isogram-string-or-not/"><strong>Program to check if String is isogram or not.</strong></a></li></ol>



<hr class="wp-block-separator is-style-wide"/>
<p>The post <a href="https://www.helpmestudybro.com/find-longest-palindromic-substring-set-1/">Find Longest Palindromic Substring &#8211; SET 1</a> appeared first on <a href="https://www.helpmestudybro.com">Helpmestudybro</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Print All Substrings of a String</title>
		<link>https://www.helpmestudybro.com/print-all-substrings-of-a-string/</link>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sat, 13 Jun 2020 09:46:08 +0000</pubDate>
				<category><![CDATA[data structure]]></category>
		<category><![CDATA[strings]]></category>
		<guid isPermaLink="false">https://www.helpmestudybro.com/?p=204</guid>

					<description><![CDATA[<p>“Print all substrings of a String” is a popular technical interview problem asked in many interviews. Here, we are given a string of length ‘n’ and our task is write a program to count and print all substrings of a string.  Example: INPUT: ABCD OUTPUT: No of substring: 10 Substrings:&#46;&#46;&#46;</p>
<p>The post <a href="https://www.helpmestudybro.com/print-all-substrings-of-a-string/">Print All Substrings of a String</a> appeared first on <a href="https://www.helpmestudybro.com">Helpmestudybro</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>“<strong><em>Print all substrings of a String</em></strong>” is a popular technical interview problem asked in many interviews. Here, we are given a string of length ‘n’ and our task is write a program to count and print all substrings of a string. </p>



<p class="has-medium-font-size"><strong>Example:</strong></p>



<pre class="wp-block-preformatted"><strong><em>INPUT:</em></strong>
ABCD
<strong><em>OUTPUT:</em></strong>
No of substring: 10
Substrings: A, B, C, D, AB, BC, CD, ABC, BCD, ABCD</pre>



<p>The steps required to count and print all substrings of a string are as follows:</p>



<ol type="1"><li>Set count = 0.</li><li>Three nested loops will be taken.</li><li>The outer loop will be used to take the starting character of the substring.</li><li>The middle loop will be used to take the last character of the substring.</li><li>The inner loop will be used to print the substring from starting character (from outer loop) to ending character (from middle loop). Increment count by 1.</li></ol>



<h4 class="wp-block-heading"><strong>C++ Program to count and print all substrings of a string are as follows:</strong></h4>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
/* C++ Program to count and print all substrings of a string */
#include&lt;bits/stdc++.h&gt;  
using namespace std;  
int main()  
{  
    string str;  
    cout&lt;&lt;&quot;Enter a string:\n&quot;;  
    cin&gt;&gt;str;  
    int count = 0;  
    cout&lt;&lt;&quot;The substring are:\n&quot;;  
    int size = str.length();  
    /* Outer loop for starting character of substring */  
    for(int i = 0; i &lt; size; i++)  
    {  
        /* Middle loop for ending character of substring */  
        for(int j = i; j &lt; size; j++)  
        {  
            string substring = &quot;&quot;;  
            /* Inner loop for creating substring */  
            for(int k = i; k &lt;= j; k++)  
            {  
                substring = substring + str&#x5B;k];  
            }  
            count++;  
            cout&lt;&lt;substring&lt;&lt;&quot;\n&quot;;  
        }  
    }  
    cout&lt;&lt;&quot;The number of substrings are &quot;&lt;&lt;count;  
}  

</pre></div>


<pre class="wp-block-preformatted"><strong><em>OUTPUT:</em></strong>
Enter a string: ABCD
The substring are:
A
AB
ABC
ABCD
B
BC
BCD
C
CD
D
The number of substrings are 10
<strong><em>Time Complexity:</em></strong>
O(n^3), where n is the length of the string</pre>



<hr class="wp-block-separator is-style-wide"/>



<p class="has-medium-font-size"><strong>Related Posts:</strong></p>



<ol type="1"><li><a href="https://www.helpmestudybro.com/program-to-find-longest-palindromic-substring-set-1/"><strong>Program to find longest palindromic Substring.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-check-anagram-string/"><strong>Program to check Anagram Strings.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-check-whether-two-strings-are-rotation-of-each-other-or-not/"><strong>Program to check whether two Strings are Rotation of each other or not.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-check-whether-string-is-palindromic-anagram-or-not/"><strong>Program to check Palindromic Anagram.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-print-all-palindromic-substrings-of-the-string-set-1/"><strong>Program to print all the Palindromic Substring of the String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-check-whether-given-string-is-panagram-string-or-not/"><strong>Program to check Panagram String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-find-first-non-repeating-character-of-the-string/"><strong>Program to find first non-repeating character of the String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-print-all-subarray-of-given-array/"><strong>Program to print all possible subarrays of the given array.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-print-kth-smallest-and-kth-largest-element-of-an-array/"><strong>Program to print kth smallest and kth largest element of an Array.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-find-the-equilibrium-index-of-the-array/"><strong>Program to find equilibrium index of an Array.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-find-majority-element-of-an-array/"><strong>Program to find majority element of an Array.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-find-mean-of-an-array/"><strong>Program to find mean of the Array.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-sort-an-array-of-0s-and-1s/"><strong>Program to sort an Array of 0s and 1s.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-find-first-repeating-element-of-an-array/"><strong>Program to find first repeating element of an array.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-merge-two-sorted-array/"><strong>Program to merge two sorted arrays.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-find-the-missing-smallest-positive-number/"><strong>Program to find missing number in an array.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-check-if-array-is-sorted-or-not/"><strong>Program to sort if array is sorted.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-print-alternate-elements-of-an-array/"><strong>Program to print Alternate Elements of an Array.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-swap-kth-element-from-beginning-with-kth-element-from-end/"><strong>Program to swap kth element from beginning to kth element from end in an Array.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-print-all-subarray-of-given-array/"><strong>Program to print all possible subarrays of the given array.</strong></a></li></ol>



<hr class="wp-block-separator is-style-wide"/>
<p>The post <a href="https://www.helpmestudybro.com/print-all-substrings-of-a-string/">Print All Substrings of a String</a> appeared first on <a href="https://www.helpmestudybro.com">Helpmestudybro</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Reverse Each Word in a String</title>
		<link>https://www.helpmestudybro.com/reverse-each-word-in-a-string/</link>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sat, 13 Jun 2020 09:40:37 +0000</pubDate>
				<category><![CDATA[data structure]]></category>
		<category><![CDATA[strings]]></category>
		<guid isPermaLink="false">https://www.helpmestudybro.com/?p=201</guid>

					<description><![CDATA[<p>“Reverse Each Word in a String” is a popular problem of string data structure. Here, we are given a string of words and our task is to write a program to reverse each word in a string. Example: INPUT: HelpMeStudyBro is an Emerging Website OUTPUT: orBydutSeMpleH si na gnigremE etisbeW&#160;&#46;&#46;&#46;</p>
<p>The post <a href="https://www.helpmestudybro.com/reverse-each-word-in-a-string/">Reverse Each Word in a String</a> appeared first on <a href="https://www.helpmestudybro.com">Helpmestudybro</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>“<strong><em>Reverse Each Word in a String</em></strong>” is a popular problem of string data structure. Here, we are given a string of words and our task is to write a program to reverse each word in a string.</p>



<p class="has-medium-font-size"><strong>Example:</strong></p>



<pre class="wp-block-preformatted"><strong><em>INPUT:</em></strong>
HelpMeStudyBro is an Emerging Website
<strong><em>OUTPUT:</em></strong>
orBydutSeMpleH si na gnigremE etisbeW&nbsp;</pre>



<p>This problem can be easily solved using stack. The steps required to reverse each word in a string is as follows:</p>



<ol type="1"><li>Create an empty character stack and initialize an empty resultant string.</li><li>Start traversing a string.</li><li>Push characters of the string in the stack until ‘space character’ appears.</li><li>Once space character is appeared.  Pop all characters from the stack and concatenate them with resultant string.</li><li>Concatenate a ‘space character’ afterwards.</li><li>Repeat steps from 3 to 5 until complete string is scanned.</li><li>In last, concatenate the remaining word to resultant string from stack.</li><li>Return the resultant string.</li></ol>



<h4 class="wp-block-heading"><strong>C++ Program to reverse each word in a string is as follows:</strong></h4>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
/* Program to reverse each word in a string */
#include&lt;bits/stdc++.h&gt;  
using namespace std;  
string reverseWords(string str)
{
    stack&lt;char&gt;st;
    string res = &quot;&quot;;
    for(int i = 0 ; i &lt; str.length() ; i++)
    {
        // Push the complete word in the string
        if(str&#x5B;i] != &#039; &#039;)
        {
            st.push(str&#x5B;i]);
        }
        else
        {
            // reverse the word and concatneate to resultant string
            while(!st.empty())
            {
                
                char ch = st.top();
                res=res+ch;
                st.pop();
            }
            res=res+&quot; &quot;;
        }
    }
    // concatenate the last reversed word to string
    while(!st.empty())
    {
        char ch = st.top();
        res=res+ch;
        st.pop();
    }
    return res;
}
int main()  
{
    string str;
    cout&lt;&lt;&quot;Enter a string: &quot;;
    getline(cin,str);
    string res = reverseWords(str);
    cout&lt;&lt;&quot;\nResultant string:\n&quot;&lt;&lt;res&lt;&lt;&quot;\n&quot;;
}

</pre></div>


<pre class="wp-block-preformatted"><strong><em>OUTPUT:</em></strong>
Enter a string:&nbsp;
HelpMeStudyBro is an Emerging Website
Resultant string:
orBydutSeMpleH si na gnigremE etisbeW</pre>



<hr class="wp-block-separator is-style-wide"/>



<p class="has-medium-font-size"><strong>Related Posts:</strong></p>



<ol type="1"><li><a href="https://www.helpmestudybro.com/program-to-print-all-substring-of-the-given-string/"><strong>Program to Print All the Substring of the Given String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-find-longest-palindromic-substring-set-1/"><strong>Program to find longest palindromic Substring.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-check-anagram-string/"><strong>Program to check Anagram Strings.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-check-whether-two-strings-are-rotation-of-each-other-or-not/"><strong>Program to check whether two Strings are Rotation of each other or not.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-check-whether-string-is-palindromic-anagram-or-not/"><strong>Program to check Palindromic Anagram.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-print-all-palindromic-substrings-of-the-string-set-1/"><strong>Program to print all the Palindromic Substring of the String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-check-whether-given-string-is-panagram-string-or-not/"><strong>Program to check Panagram String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-find-first-non-repeating-character-of-the-string/"><strong>Program to find first non-repeating character of the String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-reverse-a-string/"><strong>Program to Reverse a String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-count-number-of-characters-of-the-character-array/"><strong>Program to Count Number of Characters in a String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-count-frequency-of-particular-character-of-the-string/"><strong>Program to Count frequency of particular character of the String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-program-to-count-number-of-vowels-and-consonants-in-the-string/"><strong>Program to Count Number of Vowels and Number of Consonants in a String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-checking-type-of-string/"><strong>Program to Check if String is alphabetic or numeric or alpha-numeric.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-copy-one-character-array-to-another-character-array/"><strong>Program to copy one String to Another.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-concatenate-two-character-arrays/"><strong>Program to Concatenate Two Strings.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-check-palindrome-string/"><strong>Program to Check whether given String is Palindrome or not.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-convert-lower-case-alphabets-to-upper-case-and-vice-versa-in-string/"><strong>Program to convert lower-case alphabet to upper-case alphabet and vice-versa.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-count-total-number-of-words-in-the-string/"><strong>Program to count total number of words in a string.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-find-smallest-and-largest-word-of-the-string/"><strong>Program to find smallest and largest word of the String.</strong></a></li><li><a href="https://www.helpmestudybro.com/program-to-print-alternate-elements-of-an-array/"><strong>Program to print Alternate Elements of an Array.</strong></a></li></ol>



<hr class="wp-block-separator is-style-wide"/>
<p>The post <a href="https://www.helpmestudybro.com/reverse-each-word-in-a-string/">Reverse Each Word in a String</a> appeared first on <a href="https://www.helpmestudybro.com">Helpmestudybro</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
