<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Rohan Rai on Medium]]></title>
        <description><![CDATA[Stories by Rohan Rai on Medium]]></description>
        <link>https://medium.com/@supernerdd7?source=rss-f27da00eb25c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*yb-dD94ECWIa-5_9</url>
            <title>Stories by Rohan Rai on Medium</title>
            <link>https://medium.com/@supernerdd7?source=rss-f27da00eb25c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 20:52:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@supernerdd7/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Comparing strings in C++]]></title>
            <link>https://medium.com/@supernerdd7/comparing-strings-in-c-857539988e51?source=rss-f27da00eb25c------2</link>
            <guid isPermaLink="false">https://medium.com/p/857539988e51</guid>
            <category><![CDATA[abstract-class-in-cpp]]></category>
            <category><![CDATA[cpp]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[c-programming]]></category>
            <dc:creator><![CDATA[Rohan Rai]]></dc:creator>
            <pubDate>Sun, 13 Mar 2022 20:12:09 GMT</pubDate>
            <atom:updated>2022-03-13T20:12:09.156Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*msd_wRnNLuLZMLK66d3vgg.jpeg" /><figcaption><a href="https://www.freepik.com/">Source</a></figcaption></figure><h3>Abstract</h3><p>Comparing 2 strings nowadays is becoming a common phenomenon in competitive programming. Usually, string comparison algorithms compare strings character by character. However, C++ provides some in-built operators like == or != along with inbuilt functions like strcmp or compare</p><h3>Scope of article</h3><ul><li>This article provides different ways to compare strings in C++</li></ul><h3>Prerequisite</h3><ul><li>One should have a basic understanding of C++</li><li>The idea about string and string comparison algorithms</li></ul><h3>Introduction</h3><p>Most of us while typing an email address or typing a password have observed that the system recognizes the password and if correctly typed it allow you access. One may wonder how the system performs such an operation.</p><p>Here system uses the algorithm of string comparison where it compares the original password stored with the password typed by the user. If both strings equally match with each other, then</p><p>Voila !!!</p><p>Before we dive into understanding different methods present in C++ for comparison of 2 strings, Let us see different scenarios of string comparison with the help of some examples:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d026010e05dfff823dba22626876d5c5/href">https://medium.com/media/d026010e05dfff823dba22626876d5c5/href</a></iframe><p>From above, it is visible that for case 1, given strings are not equal to each other, while for case 2, given strings are equal to each other</p><h3>Different methods of comparing strings</h3><p>Here we going to see various methods in C++, through which given 2 strings can be compared.</p><ol><li><strong>Using relational operators for comparing strings</strong></li></ol><p>Most of us have the idea that relational operators in C++ like ‘<strong>==</strong>’ are used to compare two entities like integers or float variables.</p><p>There are 2 kinds of the relational operator that we wish to highlight</p><p>‘<strong>==</strong>’ denotes equals to the relational operator</p><p>‘<strong>!=</strong>’ denotes not equals to relation operator</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/51121f369953ca5293715d59bb7603af/href">https://medium.com/media/51121f369953ca5293715d59bb7603af/href</a></iframe><h4>Output</h4><p>Here it can be observed that since the given strings are not equal to each other i.e string a is “Scaler” and string b is “codeforce”. Therefore output would be that Scaler is not equal to codeforce.</p><p>2. <strong>Using compare function for comparing strings</strong></p><p>It is a defined function in the standard library of C++ to compare 2 strings.</p><p>While comparing 2 strings it produces 2 types of outputs</p><ul><li><strong>Equal to 0</strong><br>When given two strings are lexicographically equal</li><li><strong>Not equal to 0<br>&gt; </strong>If the value of compare output is greater than 0 (&gt; 0) then the first string is lexicographically greater than the second string<br>&gt; If the value of compare output is less than 0 (&lt; 0), then the first string is lexicographically smaller than the second string.</li></ul><p>Let’s understand compare function with the help of an example</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b09d6d694d1d801e5f3bcfec15c688ca/href">https://medium.com/media/b09d6d694d1d801e5f3bcfec15c688ca/href</a></iframe><p>Here it can be observed that for example 1, string b (“targeted”) is greater than string a (“target”)</p><p>For example 2, the output will be Both strings are equal.</p><p>Before we jump and look into 3rd method of string comparison, let us first look at some of the differences between relational operator and compare():</p><ul><li>compare() function returns an integer value while relational operator returns boolean value</li><li>It is easier to compare substring in compare() while it requires a long procedure where each word is compared with that of another string in relational operator.</li></ul><p>3. <strong>Using strcmp() function in C++ for comparing strings</strong></p><p>strcmp() is an inbuilt function available in C++ used to lexicographically compare two strings.</p><p>While comparing 2 strings, strcmp() produces 2 kinds of output</p><ul><li><strong>Equal to 0</strong><br>When the given strings are lexicographically equal to each other</li><li><strong>Not equal to 0<br>&gt; </strong>If the output of the strcmp function comes out to be greater than 0 , then string 1 is greater than string 2.<br>&gt; If the output of the strcmp function comes out to be less than 0, then string 2 is greater than string 1.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/71141b6884d4c29d846595422020f5ea/href">https://medium.com/media/71141b6884d4c29d846595422020f5ea/href</a></iframe><p>Here it can be observed that for example 1, string b (“targeted”) is greater than string a (“target”)</p><p>For example 2, the output will be Both strings are equal.</p><p>For example 3, string f(“Metallica”) is greater than string e(“Megadeth”)</p><p>However, the issue with strcmp() function is that it compares both strings to the null characters. Thus if strings passed in the argument of strcmp() are not terminated with the null character, the comparison continues till the system crashes.</p><p>To avoid this situation, strncmp() is used. In its functioning, it is more or less similar to strcmp() but it takes another argument as integer highlighting the maximum number of characters to be compared</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fe7ba6c2eece6c449837c1ef3d7baff0/href">https://medium.com/media/fe7ba6c2eece6c449837c1ef3d7baff0/href</a></iframe><p>Here it can be observed that since the number of characters to be compared is 6, thus the output of the program will be Both strings are equal.</p><h3>Conclusion</h3><p>In this article, we tried to understand the basics of string comparison and different methods which can be used to compare 2 strings i.e. using the relational operator and some inbuilt functions of C++ like compare(), strcmp ().</p><p>To further enhance your knowledge on understanding the basic algorithm of string comparison, the intuition behind how it works, and its usage in various competitive platforms like CodeChef with beautiful schematics, one may want to have a look at this curated article published on the <a href="https://www.scaler.com/topics/comparing-two-strings-in-cpp/">Comparing Two Strings in C++</a> on Scaler topics.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=857539988e51" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Factorial of a Number in C using Ternary Operator]]></title>
            <link>https://medium.com/@supernerdd7/factorial-of-a-number-in-c-using-ternary-operator-518ab6280292?source=rss-f27da00eb25c------2</link>
            <guid isPermaLink="false">https://medium.com/p/518ab6280292</guid>
            <category><![CDATA[cpp]]></category>
            <category><![CDATA[c-programming]]></category>
            <category><![CDATA[factorials]]></category>
            <dc:creator><![CDATA[Rohan Rai]]></dc:creator>
            <pubDate>Mon, 21 Feb 2022 07:51:54 GMT</pubDate>
            <atom:updated>2022-02-21T07:57:13.264Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="C Programming — Factorial of a Number in C using Ternary Operator" src="https://cdn-images-1.medium.com/max/1024/1*FztDyebODkO_Njn-s5SLyA.png" /><figcaption><a href="https://www.freepik.com/">Source</a></figcaption></figure><h3><strong>What is Factorial?</strong></h3><p>The <strong>factorial of a number</strong> is defined as the product of all positive integers &lt;=number. It is denoted by the symbol <strong>!</strong>. N! refers to <strong>N factorial</strong>.</p><p><strong>In mathematical terms</strong> :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/252ca950a3ad913d80f856dd851ac871/href">https://medium.com/media/252ca950a3ad913d80f856dd851ac871/href</a></iframe><p>Factorials always exist for <strong>non-negative integers </strong>and the factorial of 0 is considered as 1 =&gt; <strong>0!=1.</strong></p><p>N! = n* (n-1)* (n-2)* ……. *1</p><p><strong>Examples</strong>:</p><p>4! = 4*3*2*1 = 24<br>6! = 6*5*4*3*2*1 = 720</p><h3>Ternary Operator</h3><p>As the name suggests it is a type of <strong>conditional operator</strong> that requires three operands.</p><h4>Syntax</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d5abe3b7ffb6bf228470b6fb87ce0d4f/href">https://medium.com/media/d5abe3b7ffb6bf228470b6fb87ce0d4f/href</a></iframe><p>Here condition,value1, value 2 are the three operands that are required for the ternary operator.</p><h3>Understanding the Flow</h3><p>The syntax seems pretty difficult but understanding this is very easy. This can also be seen as an if-else<strong> condition.</strong></p><p>So if the condition evaluates to true, Value<strong> 1 gets assigned to the variable; otherwise,</strong> if the condition evaluates to false<strong> Value 2 gets assigned to the variable</strong>.</p><p>Alternatively, we can use an if-else block like this</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/30ed44f70f467a431fae066d711f5501/href">https://medium.com/media/30ed44f70f467a431fae066d711f5501/href</a></iframe><p>The above code will work exactly the same as the ternary operator; it’s just that writing using the ternary operator <strong>enhances the readability</strong> of the program.</p><h3>C program to find the factorial of the Number</h3><p>Now the code for the factorial<strong> program in C using a ternary operator </strong>will be very similar to what we have seen in <strong>factorial of a number using Recursion</strong>.</p><h4>Pseudo Code</h4><h4>Without Ternary Operator</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a4ce022f552b01f5edf25a0de7a9cf8b/href">https://medium.com/media/a4ce022f552b01f5edf25a0de7a9cf8b/href</a></iframe><h4><strong>With Ternary Operator</strong></h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2bae3bedfbd90db46fdf517e057e8b75/href">https://medium.com/media/2bae3bedfbd90db46fdf517e057e8b75/href</a></iframe><p>So the above code is checking for the value of n if it’s 0 then 1 is returned to the function call; otherwise another function call with the reduced value of n is pushed into the call stack as described in the <strong>Recursive factorial function in C</strong>.</p><h4><strong>Code</strong></h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/92752f47d2ed9ba71123a138a865b636/href">https://medium.com/media/92752f47d2ed9ba71123a138a865b636/href</a></iframe><h4>Output</h4><p><strong>Enter a number: Factorial of 5 is 120</strong></p><h3>Time and Space Complexity</h3><p>For every program, the most important thing is to check the time and space complexity of the code because that determines how efficient our code is for various inputs.</p><h3>Time Complexity</h3><p>Every time the same function is called with a reduced value of N so there can be almost all N function calls since N is reduced by 1 in every function call.</p><p>So the overall time complexity of our code for <strong>factorial of a number in c using</strong> <strong>ternary operator </strong>is no of times we are calling our recursive function ie N times</p><p><strong>Overall Time Complexity: O(N)</strong>.</p><h3>Space Complexity</h3><p>Inside the main function, we are just taking user input that takes <strong>O(1) space</strong>.</p><p>Inside the factorial function, we are not creating any variable or array for storing anything. So the overall Space complexity of our code is<strong> constant</strong>.</p><p><strong>Overall Space Complexity: O(1)</strong>.</p><h3>Key points and Conclusion</h3><ul><li>Time and the space complexity of the <strong>C program for the factorial</strong> of a <strong>number using a ternary operator</strong> is similar to using recursion.</li><li>Writing Code using the Ternary operator enhances the readability of code and also is efficient as compared to a nested if-else block.</li><li>It saves time because we have to write less code.</li><li>You can read this curated post published on <strong>factorial in C</strong> by <a href="https://www.scaler.com/topics/factorial-of-a-number-in-c/">Scaler Topics</a> for a more detailed understanding.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=518ab6280292" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>