<?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" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ AYUSH MISHRA - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ AYUSH MISHRA - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Wed, 13 May 2026 20:27:58 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/author/Ayush01Mishra/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ What is the GitHub CLI? How to Use GitHub from the Command Line ]]>
                </title>
                <description>
                    <![CDATA[ The GitHub CLI (Command Line Interface) is a powerful tool developed by GitHub that allows developers to interact with GitHub directly from the terminal. It provides a simple way to perform many GitHub tasks without leaving the command line interface... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-use-github-from-the-command-line/</link>
                <guid isPermaLink="false">68d6c4873324dabb31fd7214</guid>
                
                    <category>
                        <![CDATA[ GitHub ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Git ]]>
                    </category>
                
                    <category>
                        <![CDATA[ terminal ]]>
                    </category>
                
                    <category>
                        <![CDATA[ command line ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ AYUSH MISHRA ]]>
                </dc:creator>
                <pubDate>Fri, 26 Sep 2025 16:51:19 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1758905411969/b1506cff-650a-4098-bd70-e8bb3b0bcb9a.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>The GitHub CLI (Command Line Interface) is a powerful tool developed by GitHub that allows developers to interact with GitHub directly from the terminal. It provides a simple way to perform many GitHub tasks without leaving the command line interface, such as managing repositories, handling pull requests and issues, working with GitHub Actions, and more.</p>
<p>In this tutorial, you’ll to learn what the GitHub CLI is, how to install and set it up, and how to use it for everyday tasks such as creating repositories, managing issues and pull requests, working with GitHub Actions, and automating tasks using custom aliases. You’ll learn how to replace some functionalities on GitHub’s web interface with quick commands in your terminal.</p>
<h2 id="heading-heres-what-well-cover"><strong>Here’s what we’ll cover:</strong></h2>
<ul>
<li><p><a class="post-section-overview" href="#heading-overview-of-github-cli">Overview of GitHub CLI</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-key-features-of-github-cli">Key Features</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-benefits-of-using-github-cli">Benefits of Using GitHub CLI</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-installation-and-setup">Installation and Setup</a></p>
</li>
<li><p><a class="post-section-overview" href="#authenticating-with-a-github-account">Authenticating with a GitHub Account</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-navigating-the-github-cli">Navigating the GitHub CLI</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-manage-repositories-with-the-github-cli">Managing Repositories</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-managing-branches-and-pull-requests">Working with Pull Requests and Issues</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-pushing-and-pulling-changes">Pushing and Pulling Changes</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-working-with-github-actions">Working with GitHub Actions</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-manage-gists-with-the-github-cli">Managing Gists</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-interacting-with-releases-and-tags">Releases and Tags</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-extend-the-github-cli-with-custom-scripts-and-aliases">Custom Scripts and Aliases</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-troubleshooting-common-issues">Troubleshooting</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-conclusion">Conclusion</a></p>
</li>
</ul>
<h2 id="heading-overview-of-github-cli">Overview of GitHub CLI</h2>
<p>You can use the GitHub CLI to bridge the gap between GitHub's web interface and your local environment. You can perform various tasks such as creating issues, managing repositories, or even checking the status of your GitHub Actions workflows using the CLI. Using the CLI, you can perform almost all the tasks that you might complete on the GitHub website.</p>
<h3 id="heading-key-features-of-github-cli">Key Features of GitHub CLI</h3>
<ul>
<li><p><strong>Repository management:</strong> Easily create, clone, view, and manage repositories.</p>
</li>
<li><p><strong>Pull requests and issues:</strong> Manage pull requests and issues directly from the terminal, including creating, merging, and listing them.</p>
</li>
<li><p><strong>GitHub Actions:</strong> Interact with workflows and manage workflow runs.</p>
</li>
<li><p><strong>Authentication:</strong> Provides a secure way to authenticate with your GitHub account, supporting SSH keys, tokens, and OAuth.</p>
</li>
<li><p><strong>Custom scripting:</strong> Lets you create custom scripts and aliases to automate repetitive tasks and streamline development processes.</p>
</li>
</ul>
<h3 id="heading-benefits-of-using-github-cli">Benefits of Using GitHub CLI</h3>
<p>Suppose you’re working on a project, and you need to create a new issue on GitHub. Normally, you would switch to your browser, log in to GitHub, navigate to the repository, click on the “Issues” tab, and then click “New Issue.” With GitHub CLI, you can do all of this by typing a single command, without ever leaving your terminal. This makes your workflow faster and saves time.</p>
<h2 id="heading-installation-and-setup">Installation and Setup</h2>
<p>To install GitHub CLI on Windows, you can use the winget package manager. Winget is a command-line tool that allows you to install software easily.</p>
<h3 id="heading-installing-github-cli-on-windows-macos-and-linux"><strong>Installing GitHub CLI on Windows, macOS, and Linux</strong></h3>
<h3 id="heading-windows"><strong>Windows:</strong></h3>
<p>Run the command given below:</p>
<pre><code class="lang-plaintext">winget install --id GitHub.cli
</code></pre>
<ul>
<li><p><code>winget install</code><strong>:</strong> Tells Windows to install a new software package.</p>
</li>
<li><p><code>--id GitHub.cli</code><strong>:</strong> Specifies the exact package ID for GitHub CLI.</p>
</li>
</ul>
<p>After running this command, GitHub CLI will be installed on your Windows system.</p>
<h3 id="heading-macos">macOS:</h3>
<p>You can use Homebrew to install GitHub CLI on macOS. Open your terminal and run:</p>
<pre><code class="lang-plaintext">brew install gh
</code></pre>
<h3 id="heading-linux">Linux:</h3>
<p>On Linux, you can use your package manager. For example, on Ubuntu, you can run:</p>
<pre><code class="lang-plaintext">sudo apt install gh
</code></pre>
<h3 id="heading-authenticating-with-a-github-account">Authenticating with a GitHub Account</h3>
<p>After installing GitHub CLI, the next step is to authenticate it with your GitHub account.</p>
<h4 id="heading-run-authentication-command">Run Authentication Command:</h4>
<p>Type <code>gh auth login</code> in the terminal and press Enter.</p>
<pre><code class="lang-plaintext">gh auth login
</code></pre>
<p>You’ll then be prompted to select an authentication method. The recommended option is to authenticate via a web browser.</p>
<p>If you select the browser method, GitHub CLI will open a link in your default browser, where you can log in to GitHub.</p>
<h4 id="heading-complete-authentication">Complete Authentication:</h4>
<p>After logging in, the browser will confirm that the GitHub CLI is connected to your account.</p>
<p>You can verify the authentication status by running:</p>
<pre><code class="lang-plaintext">gh auth status
</code></pre>
<h2 id="heading-navigating-the-github-cli">Navigating the GitHub CLI</h2>
<p>The GitHub CLI is easy to navigate, and its command structure is intuitive.</p>
<h3 id="heading-command-structure-and-syntax">Command Structure and Syntax</h3>
<p>GitHub CLI commands follow a simple and straightforward pattern:</p>
<pre><code class="lang-plaintext">gh [command] [subcommand] [flags]
</code></pre>
<ul>
<li><p><strong>Command:</strong> The main action you want to perform (for example, repo, issue, pr).</p>
</li>
<li><p><strong>Subcommand:</strong> A specific task within the command (for example, create, list, view).</p>
</li>
<li><p><strong>Flags:</strong> Optional parameters that modify the command's behavior (for example, --title, --body).</p>
</li>
</ul>
<h3 id="heading-commonly-used-commands-and-flags">Commonly Used Commands and Flags</h3>
<p>Here are some common GitHub CLI commands:</p>
<ul>
<li><p><strong>Creating a repository:</strong> <code>gh repo create</code></p>
</li>
<li><p><strong>Listing issues:</strong> <code>gh issue list</code></p>
</li>
<li><p><strong>Creating a pull request:</strong> <code>gh pr create</code></p>
</li>
<li><p><strong>Viewing a repository's details:</strong> <code>gh repo view</code></p>
</li>
</ul>
<p>To see all available commands and options, you can always run:</p>
<pre><code class="lang-plaintext">gh help
</code></pre>
<h2 id="heading-how-to-manage-repositories-with-the-github-cli">How to Manage Repositories with the GitHub CLI</h2>
<p>Let’s go through examples of some of the commands you’ll use the most often.</p>
<h3 id="heading-creating-and-cloning-repositories">Creating and Cloning Repositories</h3>
<p>To create a new GitHub repository directly from the terminal, just use the following command:</p>
<pre><code class="lang-plaintext">gh repo create my-repo-name
</code></pre>
<p>To clone an existing repository, use the following command:</p>
<pre><code class="lang-plaintext">gh repo clone owner/repo-name
</code></pre>
<h3 id="heading-managing-branches-and-pull-requests">Managing Branches and Pull Requests</h3>
<p>GitHub CLI allows you to handle issues and pull requests (PRs) without leaving the terminal.</p>
<p>Switching branches or creating pull requests is simple. To create a new branch:</p>
<pre><code class="lang-plaintext">git checkout -b new-branch-name
</code></pre>
<p>Then, to create a pull request:</p>
<pre><code class="lang-plaintext">gh pr create --title "Your PR Title" --body "Description of your PR"
</code></pre>
<h3 id="heading-pushing-and-pulling-changes">Pushing and Pulling Changes</h3>
<p>Push your changes to GitHub with this command:</p>
<pre><code class="lang-plaintext">git push origin branch-name
</code></pre>
<p>And pull the latest changes with:</p>
<pre><code class="lang-plaintext">git pull
</code></pre>
<h3 id="heading-working-with-github-actions">Working with GitHub Actions</h3>
<p>GitHub CLI also supports GitHub Actions, allowing you to manage workflows directly from your terminal.</p>
<p>You can manually trigger workflows using the following:</p>
<pre><code class="lang-plaintext">gh workflow run workflow-name
</code></pre>
<p>And you can monitor the status of workflows with:</p>
<pre><code class="lang-plaintext">gh run list
</code></pre>
<p>To see detailed logs of a workflow, run this:</p>
<pre><code class="lang-plaintext">gh run view run-id --log
</code></pre>
<h3 id="heading-cloning-and-forking-repositories">Cloning and Forking Repositories</h3>
<p>Cloning and forking are essential tasks when working on projects from other repositories.</p>
<p>To clone a repository, use this command:</p>
<pre><code class="lang-plaintext">gh repo clone &lt;repository-name&gt;
</code></pre>
<p>To fork a repository, do this:</p>
<pre><code class="lang-plaintext">gh repo fork &lt;repository-url&gt;
</code></pre>
<h4 id="heading-example"><strong>Example:</strong></h4>
<p>Here’s what it would look like:</p>
<pre><code class="lang-plaintext">gh repo clone example-repo
</code></pre>
<pre><code class="lang-plaintext">gh repo fork https://github.com/username/repository-name
</code></pre>
<h3 id="heading-how-to-work-with-github-actions">How to Work with GitHub Actions</h3>
<p>Using the GitHub CLI, you can also manage GitHub Actions, which are automated tasks you can run in response to certain events in your repository.</p>
<h4 id="heading-triggering-and-monitoring-workflows">Triggering and Monitoring Workflows</h4>
<p>You can trigger a workflow manually like this:</p>
<pre><code class="lang-plaintext">gh workflow run &lt;workflow-name&gt;
</code></pre>
<p>And you can monitor workflow runs with this:</p>
<pre><code class="lang-plaintext">gh run list
</code></pre>
<h4 id="heading-managing-workflow-runs-and-logs">Managing Workflow Runs and Logs</h4>
<p>If you want to check the details of a specific workflow run, you can view logs directly from the CLI:</p>
<pre><code class="lang-plaintext">gh run view &lt;run-id&gt; --log
</code></pre>
<p>You can also use GitHub CLI commands to enhance your Continuous Integration/Continuous Deployment (CI/CD) pipelines, ensuring smooth automation and better control over our workflows.</p>
<h3 id="heading-how-to-update-the-github-cli">How to Update the GitHub CLI</h3>
<p>To make sure that you’re using the latest version of GitHub CLI with all the latest features and fixes, you can update it using winget.</p>
<pre><code class="lang-plaintext">winget upgrade --id GitHub.cli
</code></pre>
<ul>
<li><p><strong>winget upgrade:</strong> Checks for updates for the specified package.</p>
</li>
<li><p><strong>--id GitHub.cli:</strong> Identifies the GitHub CLI package for the upgrade.</p>
</li>
</ul>
<h2 id="heading-advanced-github-cli-features-and-integrations">Advanced GitHub CLI Features and Integrations</h2>
<p>The GitHub CLI is not only useful for performing basic tasks. You can also perform some advanced operations with its help.</p>
<h3 id="heading-how-to-manage-gists-with-the-github-cli">How to Manage Gists with the GitHub CLI</h3>
<p>Gists are a simple way to share snippets of code. You can create, list, and manage your Gists right from the CLI. Here’s how you can create a gist:</p>
<pre><code class="lang-plaintext">gh gist create my-code-snippet.py
</code></pre>
<p>To list your gists:</p>
<pre><code class="lang-plaintext">gh gist list
</code></pre>
<h3 id="heading-interacting-with-releases-and-tags">Interacting with Releases and Tags</h3>
<p>To manage releases and tags, GitHub CLI provides commands to create, list, and delete releases. Here’s an example of creating a release:</p>
<pre><code class="lang-plaintext">gh release create v1.0.0
</code></pre>
<h3 id="heading-how-to-extend-the-github-cli-with-custom-scripts-and-aliases">How to Extend the GitHub CLI with Custom Scripts and Aliases</h3>
<p>You can write your own scripts and integrate them into GitHub CLI, or create aliases for commands you use frequently to save time. Aliases let you create shortcuts for commands that you use often. For example, the command given below creates an alias <code>prlist</code> that will show all pull requests, regardless of their state:</p>
<pre><code class="lang-plaintext">gh alias set prlist "pr list --state all"
</code></pre>
<p>In the same manner, you can create a shortcut <code>co</code> to quickly check out a pull request branch without typing the full command each time. The command is given below:</p>
<pre><code class="lang-plaintext">gh alias set co "pr checkout"
</code></pre>
<h3 id="heading-troubleshooting-common-issues">Troubleshooting Common Issues</h3>
<p>If you face any issues, you can troubleshoot by checking the command syntax, ensuring your GitHub CLI is up to date, or consulting the documentation using the command:</p>
<pre><code class="lang-plaintext">gh help &lt;command&gt;
</code></pre>
<h2 id="heading-conclusion"><strong>Conclusion</strong></h2>
<p>GitHub CLI is an excellent tool that helps developers work directly from the terminal. It lets you manage repositories, handle pull requests and issues, trigger and monitor GitHub Actions, and even work with Gists.</p>
<p>You can save time and improve productivity as developers using this powerful tool. Keep exploring its new features and stay updated with the latest version.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Tokenize Text in Python — Explained with Code Examples ]]>
                </title>
                <description>
                    <![CDATA[ When working with Python, you may need to perform a tokenization operation on a given text dataset. Tokenization is the process of breaking down text into smaller pieces, typically words or sentences, which are called tokens. These tokens can then be... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-tokenize-text-in-python/</link>
                <guid isPermaLink="false">68cdb0341432174e4c7f6898</guid>
                
                    <category>
                        <![CDATA[ Python ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ AYUSH MISHRA ]]>
                </dc:creator>
                <pubDate>Fri, 19 Sep 2025 19:34:12 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1758310289206/8af072cc-e3f1-4a33-a578-c130b2ae9b11.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>When working with Python, you may need to perform a tokenization operation on a given text dataset.</p>
<p>Tokenization is the process of breaking down text into smaller pieces, typically words or sentences, which are called tokens. These tokens can then be used for further analysis, such as text classification, sentiment analysis, or natural language processing tasks.</p>
<p>In this article, we’ll discuss five different ways of tokenizing text in Python using some popular libraries and methods.</p>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ul>
<li><p><a class="post-section-overview" href="#heading-how-to-use-the-split-method-to-tokenize-text-in-python">How to Use the split() Method to Tokenize Text in Python</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-use-nltks-wordtokenize-function-to-tokenize-text-in-python">How to Use NLTK’s word_tokenize() Function to Tokenize Text in Python</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-use-the-refindall-method-to-tokenize-text-in-python">How to Use the re.findall() Method to Tokenize Text in Python</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-use-strsplit-in-pandas-to-tokenize-text-in-python">How to Use str.split() In Pandas to Tokenize Text in Python</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-use-gensims-tokenize">How to Use Gensim’s tokenize()</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-text-tokenization-methods-in-python-when-to-use">Text Tokenization Methods in Python: When to Use</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-conclusion">Conclusion</a></p>
</li>
</ul>
<h2 id="heading-how-to-use-the-split-method-to-tokenize-text-in-python">How to Use the <code>split()</code> Method to Tokenize Text in Python</h2>
<p>The <code>split()</code> Method is the most basic way to tokenize text in Python. You can use the <code>split()</code> method to split a string into a list based on a specified delimiter.</p>
<p>A delimiter is a simple character or symbol that is used to separate the pieces of text. For example: spaces(“ “), commas ( , ) , hyphens ( - ) can be used as delimiters.</p>
<p>By default, if we do not specify a delimiter, then the split method uses spaces as a delimiter. If we do not specify a delimiter, it splits the text wherever there are spaces.</p>
<p><strong>Example Code:</strong></p>
<pre><code class="lang-cpp">text = <span class="hljs-string">"Ayush and Anshu are a beautiful couple"</span>
tokens = text.split()
print(tokens)
</code></pre>
<p><strong>Explanation:</strong></p>
<p>In the above example code, the string is broken into words whenever a space is found. Each word in the given text becomes a separate token.</p>
<p><strong>Output:</strong></p>
<pre><code class="lang-plaintext">`['Ayush' , 'and' , 'Anshu' , 'are' , 'a',  'beautiful' , 'couple']`
</code></pre>
<h2 id="heading-how-to-use-nltks-wordtokenize-function-to-tokenize-text-in-python">How to Use NLTK’s <code>word_tokenize()</code> Function to Tokenize Text in Python</h2>
<p>NLTK (Natural Language Toolkit) is a powerful library for NLP. You can use the <code>word_tokenize()</code> function to tokenize a string into words and punctuation marks. When we use <code>word_tokenize()</code> it recognizes punctuation as separate tokens, which is particularly useful when the meaning of the text could change depending on punctuation.</p>
<p><strong>Example Code:</strong></p>
<pre><code class="lang-cpp"><span class="hljs-keyword">import</span> nltk
from nltk.tokenize <span class="hljs-keyword">import</span> word_tokenize

nltk.download(<span class="hljs-string">'punkt'</span>)
text = <span class="hljs-string">"Ayush and Anshu are a beautiful couple"</span>
tokens = word_tokenize(text)
print(tokens)
</code></pre>
<p><strong>Explanation:</strong></p>
<p>The text in the above code is tokenized into individual words. This method is different from other methods as it also treats punctuation, such as commas, question marks as separate tokens.</p>
<p><strong>Output:</strong></p>
<pre><code class="lang-plaintext">`['Ayush' , 'and' , 'Anshu' , 'are' , 'a', 'beautiful' , 'couple']`
</code></pre>
<p><strong>Example Code with Punctuation:</strong></p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> nltk
<span class="hljs-keyword">from</span> nltk.tokenize <span class="hljs-keyword">import</span> word_tokenize

nltk.download(<span class="hljs-string">'punkt'</span>)
text = <span class="hljs-string">"Ayush and Anshu aren't a beautiful couple"</span>
tokens = word_tokenize(text)
print(tokens)
</code></pre>
<p><strong>Explanation:</strong></p>
<p>In the above example, the apostrophe in “aren’t” will be handled separately.</p>
<p><strong>Output:</strong></p>
<pre><code class="lang-plaintext">['Ayush', 'and', 'Anshu', 'are', 'a', 'beautiful', 'couple', ',', 'are', "n't", 'they', '?']
</code></pre>
<p>The above output shows why the word_tokenize() method is preferred in cases where punctuation is used. This method ensures the accurate separation of tokens.</p>
<h2 id="heading-how-to-use-the-refindall-method-to-tokenize-text-in-python">How to Use the <code>re.findall()</code> Method to Tokenize Text in Python</h2>
<p>The <code>re</code> module allows you to define patterns to extract tokens. In Python, the <code>re.findall()</code> method allows us to extract tokens based on a pattern you define. For example, we can extract all words using the \w+ pattern. With <code>re.findall()</code>, you have complete control over how the text is tokenized.</p>
<p><strong>Example Code:</strong></p>
<pre><code class="lang-cpp"><span class="hljs-keyword">import</span> re

text = <span class="hljs-string">"Ayush and Anshu are a beautiful couple"</span>
tokens = re.findall(r<span class="hljs-number">'</span>\w+<span class="hljs-string">', text)
print(tokens)</span>
</code></pre>
<p><strong>Explanation:</strong></p>
<p>In the above code, <code>\w+</code> tells Python to find “word-like” sequences of characters. Punctuation is ignored, so only words are returned.</p>
<p><strong>Output:</strong></p>
<pre><code class="lang-cpp">`[<span class="hljs-string">'Ayush'</span> , <span class="hljs-string">'and'</span> , <span class="hljs-string">'Anshu'</span> , <span class="hljs-string">'are'</span> , <span class="hljs-string">'a'</span>, <span class="hljs-string">'beautiful'</span> , <span class="hljs-string">'couple'</span>]`
</code></pre>
<h2 id="heading-how-to-use-strsplit-in-pandas-to-tokenize-text-in-python">How to Use <code>str.split()</code> In Pandas to Tokenize Text in Python</h2>
<p>You can use Pandas to tokenize text in DataFrames. It provides an easy way of doing this. You can use the <code>str.split()</code> method to split strings into tokens. This method allows you to tokenize text in an entire column of a DataFrame, making it incredibly efficient for processing large amounts of text data at once.</p>
<p><strong>Example Code:</strong></p>
<pre><code class="lang-cpp"><span class="hljs-keyword">import</span> pandas as pd

df = pd.DataFrame({<span class="hljs-string">"text"</span>: [<span class="hljs-string">"Ayush and Anshu are a beautiful couple"</span>]})
df[<span class="hljs-string">'tokens'</span>] = df[<span class="hljs-string">'text'</span>].str.split()
print(df[<span class="hljs-string">'tokens'</span>][<span class="hljs-number">0</span>])
</code></pre>
<p><strong>Explanation:</strong></p>
<p>In the above code, the text column is split into tokens. This method is similar to Python’s basic <code>split()</code> method. This method is very helpful when we want to tokenize text in thousands of rows at once.</p>
<p><strong>Output:</strong></p>
<pre><code class="lang-cpp"> `[<span class="hljs-string">'Ayush'</span> , <span class="hljs-string">'and'</span> , <span class="hljs-string">'Anshu'</span> , <span class="hljs-string">'are'</span> , <span class="hljs-string">'a'</span> <span class="hljs-string">'beautiful'</span> , <span class="hljs-string">'couple'</span>]`
</code></pre>
<h2 id="heading-how-to-use-gensims-tokenize-function-to-tokenize-text-in-python">How to Use Gensim’s <code>tokenize()</code> Function to Tokenize Text in Python</h2>
<p><strong>Genism</strong> is a popular library in Python that is used for topic modeling and text processing. It provides a simple way to tokenize text using the <code>tokenize()</code> function. This method is particularly useful when we are working with text data in the context of Gensim’s other functionalities, such as building word vectors or creating topic models.</p>
<p><strong>Example Code</strong></p>
<pre><code class="lang-cpp">from gensim.utils <span class="hljs-keyword">import</span> tokenize

text = <span class="hljs-string">"Ayush and Anshu are a beautiful couple"</span>
tokens = <span class="hljs-built_in">list</span>(tokenize(text))
print(tokens)
</code></pre>
<p><strong>Explanation:</strong></p>
<p>In the above code, Gensim’s <code>tokenize()</code> function is used to break the text into individual words. It works similar to <code>split()</code>, but it is more powerful because it automatically removes punctuation and only keeps valid words. Since <code>tokenize()</code> returns an iterator, we use <code>list()</code> to convert it into a list of tokens.</p>
<p><strong>Output:</strong></p>
<pre><code class="lang-cpp">`[<span class="hljs-string">'Ayush'</span> , <span class="hljs-string">'and'</span> , <span class="hljs-string">'Anshu'</span> , <span class="hljs-string">'are'</span> , <span class="hljs-string">'a'</span> , <span class="hljs-string">'beautiful'</span> , <span class="hljs-string">'couple'</span>]`
</code></pre>
<h2 id="heading-text-tokenization-methods-in-python-when-to-use">Text Tokenization Methods in Python: When to Use</h2>
<div class="hn-table">
<table>
<thead>
<tr>
<td><strong>Method</strong></td><td><strong>Description</strong></td><td><strong>When to Use</strong></td></tr>
</thead>
<tbody>
<tr>
<td><strong>Using</strong> <code>split()</code> <strong>Method</strong></td><td>Basic method that splits a string into a list based on a delimiter. Default splits on spaces.</td><td>- Simple text tokenization.  </td></tr>
</tbody>
</table>
</div><p>- When you do not need to handle punctuation or special characters. |
| <strong>Using NLTK’s</strong> <code>word_tokenize()</code> | Uses the NLTK library to tokenize text into words and punctuation marks. | - Handling punctuation.<br>- Advanced NLP tasks.<br>- When precise tokenization is needed. |
| <strong>Using Regex with</strong> <code>re.findall()</code> | Uses regular expressions to define patterns for token extraction. | - Full control over token patterns.<br>- Extracting specific patterns like hashtags or email addresses. |
| <strong>Using</strong> <code>str.split()</code> <strong>in Pandas</strong> | Tokenizes text in DataFrames using the <code>str.split()</code> method. | - When working with large datasets in DataFrames.<br>- Efficient text processing across entire columns. |
| <strong>Using Gensim’s</strong> <code>tokenize()</code> | Tokenizes text using the Gensim library, suitable for text processing tasks. | - When working on topic modeling or text processing with Gensim.<br>- Integration with Gensim’s other functionalities. |</p>
<p>Tokenization is a fundamental step in text processing and natural language processing (NLP), transforming raw text into manageable units for analysis. Each of the methods discussed provides unique advantages, allowing for flexibility depending on the complexity of the task and the nature of the text data.</p>
<ol>
<li><p><strong>Using</strong> <code>split</code> <strong>Method:</strong> This basic approach is suitable for simple text tokenization where punctuation and special characters are not a concern. It’s ideal for quick and straightforward tasks.</p>
</li>
<li><p><strong>Using NLTK’s</strong> <code>word_tokenize()</code><strong>:</strong> NLTK offers a more sophisticated tokenization approach by handling punctuation and providing support for advanced NLP tasks. This method is beneficial when working on projects that require detailed text analysis.</p>
</li>
<li><p><strong>Using Regex with</strong> <code>re.findall()</code><strong>:</strong> This method gives you precise control over token patterns, making it useful for extracting tokens based on specific patterns like hashtags, email addresses, or other custom tokens.</p>
</li>
<li><p><strong>Using</strong> <code>str.split()</code> <strong>In Pandas:</strong> When dealing with large datasets within DataFrames, Pandas provides an efficient way to tokenize text across entire columns. This method is ideal for handling large-scale text data processing tasks.</p>
</li>
<li><p><strong>Using Gensim’s</strong> <code>tokenize()</code><strong>:</strong> For tasks related to topic modeling or when working with Gensim’s text processing functionalities, this method integrates seamlessly into Gensim’s ecosystem, facilitating tokenization in the context of more complex text analysis.</p>
</li>
</ol>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Selecting the right tokenization method depends on your specific requirements, such as the need for handling punctuation, processing large datasets, or integrating with advanced text analysis tools.</p>
<p>By understanding the strengths and appropriate use cases for each method, you can effectively prepare your text data for further analysis and modeling, ensuring that your NLP workflows are both efficient and accurate.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How Infinite Loops Work in C++ ]]>
                </title>
                <description>
                    <![CDATA[ In C++, a loop is a part of code that is executed repetitively until the given condition is satisfied. An infinite loop is a loop that runs indefinitely, without any condition to exit the loop. In this article, we will learn about infinite loops in C... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-infinite-loops-work-in-c/</link>
                <guid isPermaLink="false">688d43422c35fedc5be4fa94</guid>
                
                    <category>
                        <![CDATA[ C++ ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Loops ]]>
                    </category>
                
                    <category>
                        <![CDATA[ while loop ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Do while loop ]]>
                    </category>
                
                    <category>
                        <![CDATA[ for loop ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ AYUSH MISHRA ]]>
                </dc:creator>
                <pubDate>Fri, 01 Aug 2025 22:44:18 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1754065314765/5c8e45f0-6a43-4f1f-b254-2603b7d37e0c.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>In C++, a loop is a part of code that is executed repetitively until the given condition is satisfied. An infinite loop is a loop that runs indefinitely, without any condition to exit the loop.</p>
<p>In this article, we will learn about infinite loops in C++, their types and causes, and their applications.</p>
<h3 id="heading-heres-what-well-cover">Here’s what we’ll cover:</h3>
<ol>
<li><p><a class="post-section-overview" href="#heading-what-is-infinte-loop-in-c">What is an Infinite Loop in C++?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-types-of-infinte-loops-in-c">Types of Infinite Loops in C++</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-common-cause-of-accidental-infinte-loops-in-c">Common Causes of Accidental Infinite Loops in C++</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-application-of-infinte-loops-in-c">Applications of Infinite Loops in C++</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-using-infinite-loops-to-take-user-input-in-c">Using Infinite Loops To Take User Input in C++</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-conclusion">Conclusion</a></p>
</li>
</ol>
<h2 id="heading-what-is-an-infinite-loop-in-c">What is an Infinite Loop in C++?</h2>
<p>An infinite loop is any loop in which the loop condition is always true, leading to the given block of code being executed an infinite number of times. They can also be called endless or non-terminating loops, which will run until the end of the program’s life.</p>
<p>Infinite loops are generally accidental and occur due to some mistake by the programmer. But they are pretty useful, too, in different kinds of applications, such as creating a program that does not terminate until a specific command is given.</p>
<h2 id="heading-types-of-infinite-loops-in-c">Types of Infinite Loops in C++</h2>
<p>There are several ways to create an infinite loop in C++, using different loop constructs such as while, for, and do-while loops. Here, we will explore each method and provide examples.</p>
<ul>
<li><p>Infinite While Loops</p>
</li>
<li><p>Infinite For Loops</p>
</li>
<li><p>Infinite do-while Loops</p>
</li>
</ul>
<h3 id="heading-1-infinite-loop-using-while-loop">1. Infinite Loop using While Loop</h3>
<p>This is the most popular type of while loop due to its simplicity. You just pass the value that will result in true as the condition of the while loop.</p>
<p><strong>Syntax:</strong></p>
<pre><code class="lang-plaintext">while(1)
    or
while(true)
</code></pre>
<p><strong>Example Code:</strong></p>
<pre><code class="lang-cpp"><span class="hljs-comment">// Example of Infinite loop in C++ using for loop</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;</span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-comment">// Infinite loop using while</span>
    <span class="hljs-keyword">while</span> (<span class="hljs-literal">true</span>) {
        <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"This is an infinite loop."</span> &lt;&lt; <span class="hljs-built_in">endl</span>;
    }
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>Output:</p>
<p>This is an infinite loop.</p>
<p>This is an infinite loop.</p>
<p>This is an infinite loop.</p>
<p>This is an infinite loop.</p>
<p>This is an infinite loop.</p>
<p>This is an infinite loop.</p>
<p>………………….</p>
<h3 id="heading-infinite-loop-using-for-loop">Infinite Loop using For Loop</h3>
<p>In a for loop, if we remove the initialization, comparison, and update conditions, then it will result in an infinite loop.</p>
<p><strong>Syntax:</strong></p>
<pre><code class="lang-plaintext">for(;;)
</code></pre>
<p><strong>Example Code:</strong></p>
<pre><code class="lang-cpp"><span class="hljs-comment">//Example of Infinite loop in C++ using for loop</span>

<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;</span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-comment">// Infinite loop using for loop</span>
    <span class="hljs-keyword">for</span> (;;) {
        <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"This is an infinite loop."</span> &lt;&lt; <span class="hljs-built_in">endl</span>;
    }
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>Output:</p>
<p>This is an infinite loop.</p>
<p>This is an infinite loop.</p>
<p>This is an infinite loop.</p>
<p>This is an infinite loop.</p>
<p>This is an infinite loop.</p>
<p>This is an infinite loop.</p>
<p>This is an infinite loop.</p>
<p>……………………….</p>
<h3 id="heading-infinite-loop-using-do-while-loop">Infinite Loop using do-while Loop</h3>
<p>Just like the other two loops discussed above, we can also create an infinite loop using a do-while loop. Although this loop is not preferred much due to its longer syntax.</p>
<p><strong>Syntax:</strong></p>
<pre><code class="lang-plaintext">do{
}while(1)
</code></pre>
<p><strong>Example Code:</strong></p>
<pre><code class="lang-cpp"><span class="hljs-comment">// Infinite loop in C++ using do-while loop</span>

<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;</span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
   <span class="hljs-comment">// infinite do-while loop</span>
    <span class="hljs-keyword">do</span> {
        <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"This is an infinite loop."</span> &lt;&lt; <span class="hljs-built_in">endl</span>;
    } <span class="hljs-keyword">while</span> (<span class="hljs-literal">true</span>);

    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>Output:</p>
<p>This is an infinite loop.</p>
<p>This is an infinite loop.</p>
<p>This is an infinite loop.</p>
<p>This is an infinite loop.</p>
<p>This is an infinite loop.</p>
<p>This is an infinite loop.</p>
<p>This is an infinite loop.</p>
<p>……………………….</p>
<h2 id="heading-common-causes-of-accidental-infinite-loops-in-c">Common Causes of Accidental Infinite Loops in C++</h2>
<p>Infinite loops can be both intentional and accidental. Accidental infinite loops are those which were not intended by the programmer but are caused due to some error in the program.</p>
<p>Following are some of the errors that may cause infinite loops in your programs unintentionally:</p>
<h3 id="heading-1-missing-update-statements">1. Missing Update Statements</h3>
<p>Infinite loops are caused when you forget to add an update condition inside the loop, which will terminate the loop in the future. The following program illustrates such a scenario:</p>
<p><strong>Example Code:</strong></p>
<pre><code class="lang-cpp"><span class="hljs-comment">// Infinite loop caused due to missing update statement</span>

<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;</span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">int</span> i = <span class="hljs-number">3</span>;
    <span class="hljs-keyword">while</span> (i &lt; <span class="hljs-number">5</span>) {
        <span class="hljs-built_in">cout</span> &lt;&lt; i &lt;&lt;<span class="hljs-built_in">endl</span>;
        <span class="hljs-comment">// Missing update: i++;</span>
    }
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>Output:</p>
<p>3</p>
<p>3</p>
<p>3</p>
<p>3</p>
<p>3</p>
<p>3</p>
<p>3</p>
<p>……………………</p>
<p>To fix the above code, we can add an update condition inside the loop like this:</p>
<pre><code class="lang-cpp"><span class="hljs-comment">// fixed code</span>

<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;iostream&gt;</span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span> ;

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">int</span> i = <span class="hljs-number">3</span>;
<span class="hljs-keyword">while</span> (i &lt; <span class="hljs-number">5</span>) {
    <span class="hljs-built_in">cout</span> &lt;&lt; i &lt;&lt; <span class="hljs-built_in">endl</span>;
    i++; <span class="hljs-comment">// add the condition</span>
}

<span class="hljs-keyword">return</span> <span class="hljs-number">0</span> ; 

}
</code></pre>
<p>Output:</p>
<p>3</p>
<p>4</p>
<h3 id="heading-incorrect-loop-conditions">Incorrect Loop Conditions</h3>
<p>The conditions mentioned inside the loop body are crucial to terminate a loop. An incorrect loop condition can result in an infinite loop. The following program illustrates such a scenario:</p>
<p><strong>Example Code:</strong></p>
<pre><code class="lang-cpp"><span class="hljs-comment">// Infinite loop caused due to incorrect loop conditions</span>

<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;</span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">int</span> i = <span class="hljs-number">2</span>;
    <span class="hljs-keyword">while</span> (i &gt;= <span class="hljs-number">0</span>) {  
        <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"Hello AnshuAyush "</span> &lt;&lt; <span class="hljs-built_in">endl</span>;

    }
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>Output:</p>
<p>Hello AnshuAyush</p>
<p>Hello AnshuAyush</p>
<p>Hello AnshuAyush</p>
<p>Hello AnshuAyush</p>
<p>Hello AnshuAyush</p>
<p>……………………..</p>
<p>To fix the above code, we can update <code>i</code> inside the loop to eventually make the condition false:</p>
<pre><code class="lang-cpp"><span class="hljs-comment">// fixed code </span>

<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;iostream&gt;</span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span> ;

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">int</span> i = <span class="hljs-number">2</span>;
<span class="hljs-keyword">while</span> (i &gt;= <span class="hljs-number">0</span>) {  
    <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"Hello AnshuAyush"</span> &lt;&lt; <span class="hljs-built_in">endl</span>;
    i--; <span class="hljs-comment">// loop will stop</span>
}

<span class="hljs-keyword">return</span> <span class="hljs-number">0</span> ; 

}
</code></pre>
<p>Ouptut:</p>
<p>Hello AnshuAyush</p>
<p>Hello AnshuAyush</p>
<p>Hello AnshuAyush</p>
<h3 id="heading-logical-errors-in-the-loop">Logical Errors in the Loop</h3>
<p>In many scenarios, infinite loops are caused by small logical errors in the code. The following program illustrates such a scenario:</p>
<p><strong>Example Code:</strong></p>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;</span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">3</span>; i &gt;<span class="hljs-number">2</span>; i += <span class="hljs-number">2</span>) {  
        <span class="hljs-built_in">cout</span> &lt;&lt;<span class="hljs-string">"This is an infinite loop"</span> &lt;&lt; <span class="hljs-built_in">endl</span>;
    }
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>Output:</p>
<p>This is an infinite loop.</p>
<p>This is an infinite loop.</p>
<p>This is an infinite loop.</p>
<p>This is an infinite loop.</p>
<p>This is an infinite loop.</p>
<p>This is an infinite loop.</p>
<p>This is an infinite loop.</p>
<p>……………………….</p>
<p>To fix the above code, we can either use a decreasing condition or use an incrementing loop condition.</p>
<p>Decreasing condition:</p>
<pre><code class="lang-cpp"><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">3</span>; i &gt; <span class="hljs-number">0</span>; i--) {
    <span class="hljs-built_in">cout</span> &lt;&lt;<span class="hljs-string">"This is NOT an infinite loop"</span> &lt;&lt; <span class="hljs-built_in">endl</span>;
}
</code></pre>
<p>Increasing condition:</p>
<pre><code class="lang-cpp"><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">3</span>; i &lt; <span class="hljs-number">10</span>; i += <span class="hljs-number">2</span>) {
    <span class="hljs-built_in">cout</span> &lt;&lt;<span class="hljs-string">"Loop will end when i reaches 10"</span> &lt;&lt; <span class="hljs-built_in">endl</span>;
}
</code></pre>
<h2 id="heading-applications-of-infinite-loops-in-c">Applications of Infinite Loops in C++</h2>
<p>Infinite loops do not only occur by accident, as I mentioned above. You can also create them on purpose for different use cases. The following are some of the common applications where you might use infinite loops intentionally:</p>
<ul>
<li><p><strong>Event loops:</strong> Many Graphical User Interfaces (GUIs) use infinite loops to keep the program running and responsive to user actions.</p>
</li>
<li><p><strong>Server applications:</strong> Web servers use infinite loops to continuously listen to client connections or requests.</p>
</li>
<li><p><strong>Embedded systems:</strong> Embedded systems, such as microcontrollers, frequently use infinite loops as their main program loops to continuously respond to external events.</p>
</li>
<li><p><strong>User inputs:</strong> Infinite loops are also used to wait for valid user inputs. The loop keeps running until a valid input is provided by the user. We’ll look at an example of this one.</p>
</li>
</ul>
<h3 id="heading-using-infinite-loops-to-take-user-input-in-c">Using Infinite Loops to Take User Input in C++</h3>
<p>Infinite loops are commonly used in scenarios where a program needs to continuously take user input until a specific condition is met, such as exiting the program or getting a valid user input. The following program demonstrates how we can take user input from the user until a specific condition is met:</p>
<p><strong>Example Code:</strong></p>
<pre><code class="lang-cpp"><span class="hljs-comment">// C++ Program to take user input from users using infinite loops</span>

<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;string&gt;</span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-built_in">string</span> input;

    <span class="hljs-keyword">while</span> (<span class="hljs-literal">true</span>) {
        <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"Enter a command (type 'exit' to quit): "</span>;
        getline(<span class="hljs-built_in">cin</span>, input);

        <span class="hljs-keyword">if</span> (input == <span class="hljs-string">"exit"</span>) {
        <span class="hljs-comment">// Exit the loop if the user types 'exit'</span>
            <span class="hljs-keyword">break</span>; 
        }

        <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"You entered: "</span> &lt;&lt; input &lt;&lt; <span class="hljs-built_in">endl</span>;
        <span class="hljs-comment">// Process the input</span>
    }
    <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"Program exited."</span> &lt;&lt; <span class="hljs-built_in">endl</span>;
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>Output:</p>
<p>Enter a command (type 'exit' to quit): Anshu</p>
<p>You entered: Anshu</p>
<p>Enter a command (type 'exit' to quit): Ayush</p>
<p>You entered: Ayush</p>
<p>Enter a command (type 'exit' to quit): exit</p>
<p>Program exited.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Infinite loops aren’t always dangerous. They can be very useful when used with proper control, like break statements or condition checks. But if you use them carelessly, they can crash your program.</p>
<p>So just make sure you check your loop conditions and test your code using print statements between the programs to discover any unexpected behavior. In sum, infinite loops can be very powerful when handled carefully but can be very risky if left unchecked.</p>
<p>And if you'd like to support me and my work directly so I can keep creating these tutorials, <a target="_blank" href="https://paypal.me/ayushM010">you can do so here</a>. Thank you!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Helpful Built-in Functions in C++ that All Devs Should Know ]]>
                </title>
                <description>
                    <![CDATA[ Built-in functions in C++ are those functions that are part of the C++ standard libraries. These functions are designed to provide common and essential functionality that is often required in programming. In this article, we will look at some of the ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/helpful-built-in-functions-in-cpp/</link>
                <guid isPermaLink="false">687fc93c8ff83d5b41883c1e</guid>
                
                    <category>
                        <![CDATA[ C++ ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Functional Programming ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ AYUSH MISHRA ]]>
                </dc:creator>
                <pubDate>Tue, 22 Jul 2025 17:24:12 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1753105870543/7bdb3c7e-873b-46a2-bdbd-0d881aacedfc.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Built-in functions in C++ are those functions that are part of the C++ standard libraries. These functions are designed to provide common and essential functionality that is often required in programming.</p>
<p>In this article, we will look at some of the most commonly used built-in functions in C++ so you can start using them in your code.</p>
<h3 id="heading-what-well-cover">What we’ll cover:</h3>
<ol>
<li><p><a class="post-section-overview" href="#heading-the-sqrt-function">The <code>sqrt()</code> Function</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-the-pow-function">The <code>pow()</code> Function</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-the-sort-function">The <code>sort()</code> Function</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-the-find-function">The <code>find()</code> Function</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-the-binarysearch-function">The <code>binarysearch()</code> Function</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-the-max-function">The <code>max()</code> Function</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-the-min-function">The <code>min()</code> Function</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-the-swap-function">The <code>swap()</code> Function</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-the-toupper-function">The <code>toupper()</code> Function</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-the-tolower-function">The <code>tolower()</code> Function</a></p>
</li>
</ol>
<h2 id="heading-the-sqrt-function">The <code>sqrt()</code> Function</h2>
<p>You use the <code>sqrt()</code> function to determine the square root of the value of type double. It is defined inside the <code>&lt;cmath&gt;</code> header file.</p>
<h3 id="heading-syntax"><strong>Syntax:</strong></h3>
<pre><code class="lang-plaintext">sqrt (n)
</code></pre>
<p><strong>Parameter:</strong> This function takes only one parameter of type double which is a number we want to find the square root of.</p>
<p><strong>Return Type:</strong> The square root of the value of type double.</p>
<h3 id="heading-example-code">Example Code</h3>
<p>Let’s look at an example so you can see how this function works:</p>
<pre><code class="lang-cpp"><span class="hljs-comment">// C++ program to see the use of sqrt() function</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;cmath&gt;     </span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;  </span></span>

<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;  
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{
    <span class="hljs-keyword">double</span> x = <span class="hljs-number">100</span>;

    <span class="hljs-keyword">double</span> answer;

    <span class="hljs-comment">// Use the sqrt() function to calculate the square root of the number</span>
    answer = <span class="hljs-built_in">sqrt</span>(x);

    <span class="hljs-comment">// Print the result </span>
    <span class="hljs-built_in">cout</span> &lt;&lt; answer &lt;&lt; <span class="hljs-built_in">endl</span>;

    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>Output:</p>
<p>10</p>
<h2 id="heading-the-pow-function"><strong>The</strong> <code>pow()</code> <strong>Function</strong></h2>
<p>You use the <code>pow()</code> function to find the value of the given number raised to some power. This function is also defined inside the <code>&lt;cmath&gt;</code> header file.</p>
<h3 id="heading-syntax-1"><strong>Syntax:</strong></h3>
<pre><code class="lang-plaintext">double pow(double x, double y);
</code></pre>
<p><strong>Parameters:</strong></p>
<ul>
<li><p><strong>x:</strong> The base number.</p>
</li>
<li><p><strong>y:</strong> The exponential power.</p>
</li>
</ul>
<p><strong>Return Type:</strong> value of <strong>x</strong> raised to the power <strong>y</strong>.</p>
<h3 id="heading-example-code-1">Example Code:</h3>
<p>Let’s look at an example to see how this works:</p>
<pre><code class="lang-cpp"><span class="hljs-comment">// C++ program to see the use of the pow() function</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;cmath&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;</span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{
 <span class="hljs-comment">// Declare an integer variable 'base' </span>
    <span class="hljs-keyword">int</span> base = <span class="hljs-number">5</span>;

<span class="hljs-comment">// Declare an integer variable 'exponent' </span>
    <span class="hljs-keyword">int</span> exponent = <span class="hljs-number">3</span>;

<span class="hljs-comment">// pow(5, 3) means 5^3 which is 5*5*5 = 125</span>
<span class="hljs-comment">// Use the pow() function to calculate base raised to the power of exponent</span>
    <span class="hljs-keyword">int</span> answer = <span class="hljs-built_in">pow</span>(base, exponent);

<span class="hljs-comment">// output the result</span>
    <span class="hljs-built_in">cout</span> &lt;&lt; answer &lt;&lt; <span class="hljs-built_in">endl</span>;
}
</code></pre>
<p>Output:</p>
<p>125</p>
<h2 id="heading-the-sort-function">The <code>sort()</code> Function</h2>
<p>The <code>sort()</code> function is part of STL's <code>&lt;algorithm&gt;</code> header. It is a function template that you can use to sort the random access containers, such as vectors, arrays, and so on.</p>
<h3 id="heading-syntax-2">Syntax:</h3>
<pre><code class="lang-plaintext">sort (arr , arr + n, comparator)
</code></pre>
<p><strong>Parameters:</strong></p>
<ul>
<li><p><strong>arr:</strong> The pointer or iterator to the first element of the array.</p>
</li>
<li><p><strong>arr + n:</strong> The pointer to the imaginary element next to the last element of the array.</p>
</li>
<li><p><strong>comparator:</strong> The unary predicate function that is used to sort the value in some specific order. The default value of this sorts the array in ascending order.</p>
</li>
</ul>
<p><strong>Return Value:</strong> This function does not return any value.</p>
<h3 id="heading-example-code-2">Example Code:</h3>
<p>Let’s look at an example:</p>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;     </span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;algorithm&gt;    // Header file that includes the sort() function</span></span>

<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;    

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{
    <span class="hljs-comment">// Declare and initialize an integer array with unsorted elements</span>
    <span class="hljs-keyword">int</span> arr[] = { <span class="hljs-number">13</span>, <span class="hljs-number">15</span>, <span class="hljs-number">12</span>, <span class="hljs-number">14</span>, <span class="hljs-number">11</span>, <span class="hljs-number">16</span>, <span class="hljs-number">18</span>, <span class="hljs-number">17</span> };

    <span class="hljs-comment">// Calculate the number of elements in the array</span>
    <span class="hljs-keyword">int</span> n = <span class="hljs-keyword">sizeof</span>(arr) / <span class="hljs-keyword">sizeof</span>(arr[<span class="hljs-number">0</span>]);

    <span class="hljs-comment">// Use the built-in sort() function from the algorithm library</span>
    sort(arr, arr + n);

    <span class="hljs-comment">// Print the sorted array using a loop</span>
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; n; ++i)
        <span class="hljs-built_in">cout</span> &lt;&lt; arr[i] &lt;&lt; <span class="hljs-string">" "</span>;  

    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>Output:</p>
<p>11 12 13 14 15 16 17 18</p>
<h2 id="heading-the-find-function">The <code>find()</code> Function</h2>
<p>The <code>find()</code> function is also part of the STL <code>&lt;algorithm&gt;</code> library. You use this function to find a value in the given range. You can use it with both sorted and unsorted datasets as it implements a linear search algorithm.</p>
<h3 id="heading-syntax-3"><strong>Syntax:</strong></h3>
<pre><code class="lang-plaintext">find(startIterator, endIterator, key)
</code></pre>
<p><strong>Parameters:</strong></p>
<ul>
<li><p><strong>startIterator:</strong> Iterates to the beginning of the range.</p>
</li>
<li><p><strong>endIterator:</strong> Iterates to the end of the range.</p>
</li>
<li><p><strong>key:</strong> The value to be searched.</p>
</li>
</ul>
<p><strong>Return Value:</strong> If the element is found, then the iterator is set to the element. Otherwise, it iterates to the end.</p>
<h3 id="heading-example-code-3">Example Code:</h3>
<p>Let’s look at an example to better understand how it works:</p>
<pre><code class="lang-cpp"><span class="hljs-comment">// C++ program to see the the use of the find() function</span>

<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;algorithm&gt;   // Required for the find() function</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;    </span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;vector&gt;      </span></span>

<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;   

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{
    <span class="hljs-comment">// Initialize a vector </span>
    <span class="hljs-built_in">vector</span>&lt;<span class="hljs-keyword">int</span>&gt; dataset{ <span class="hljs-number">12</span>, <span class="hljs-number">28</span>, <span class="hljs-number">16</span>, <span class="hljs-number">7</span>, <span class="hljs-number">33</span>, <span class="hljs-number">43</span> };

    <span class="hljs-comment">// Use the find() function to search for the value 7</span>
    <span class="hljs-keyword">auto</span> index = find(dataset.begin(), dataset.end(), <span class="hljs-number">7</span>);

    <span class="hljs-comment">// Check if the element was found</span>
    <span class="hljs-keyword">if</span> (index != dataset.end()) {
        <span class="hljs-comment">// If found, print the position (index) by subtracting the starting iterator</span>
        <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"The element is found at the "</span>
             &lt;&lt; index - dataset.begin() &lt;&lt; <span class="hljs-string">"nd index"</span>;
    }
    <span class="hljs-keyword">else</span> {
        <span class="hljs-comment">// If not found</span>
        <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"Element not found"</span>;
   }
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>Output:</p>
<p>The element is found at the 3rd index</p>
<h2 id="heading-the-binarysearch-function">The <code>binary_search()</code> Function</h2>
<p>The <code>binary_search()</code> function is also used to find an element in the range – but this function implements binary search instead of linear search as compared to the <code>find()</code> function. It’s also faster than the <code>find()</code> function, but you can only use it on sorted datasets with random access. It’s defined inside the <code>&lt;algorithm&gt;</code> header file.</p>
<h3 id="heading-syntax-4"><strong>Syntax:</strong></h3>
<pre><code class="lang-plaintext">binary_search (starting_pointer , ending_pointer , target);
</code></pre>
<p><strong>Parameters:</strong></p>
<ul>
<li><p><strong>starting_pointer:</strong> Pointer to the start of the range.</p>
</li>
<li><p><strong>ending_pointer:</strong> Pointer to the element after the end of the range.</p>
</li>
<li><p><strong>target:</strong> Value to be searched in the dataset.</p>
</li>
</ul>
<p><strong>Return Value:</strong></p>
<ul>
<li><p>Returns true if the target is found.</p>
</li>
<li><p>Else return false.</p>
</li>
</ul>
<h3 id="heading-example-code-4">Example Code:</h3>
<p>Let’s check out an example to see how it works:</p>
<pre><code class="lang-cpp"><span class="hljs-comment">// C++ program for the binary_search() function</span>

<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;algorithm&gt;   </span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;    </span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;vector&gt;      </span></span>

<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;   

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{
    <span class="hljs-comment">// Initialize a sorted vector of integers</span>
    <span class="hljs-built_in">vector</span>&lt;<span class="hljs-keyword">int</span>&gt; arr = { <span class="hljs-number">56</span>, <span class="hljs-number">57</span>, <span class="hljs-number">58</span>, <span class="hljs-number">59</span>, <span class="hljs-number">60</span>, <span class="hljs-number">61</span>, <span class="hljs-number">62</span> };

    <span class="hljs-comment">// binary_search() works only on sorted containers</span>
    <span class="hljs-keyword">if</span> (binary_search(arr.begin(), arr.end(), <span class="hljs-number">62</span>)) {
        <span class="hljs-comment">// If found, print that the element is present</span>
        <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-number">62</span> &lt;&lt; <span class="hljs-string">" is present in the vector."</span>;
    }
    <span class="hljs-keyword">else</span> {
        <span class="hljs-comment">// If not found, print that the element is not present</span>
        <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-number">16</span> &lt;&lt; <span class="hljs-string">" is not present in the vector"</span>;
    }

    <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-built_in">endl</span>;
}
</code></pre>
<p>Output:</p>
<p>62 is present in the vector.</p>
<h2 id="heading-the-max-function">The <code>max()</code> Function</h2>
<p>You can use the <code>std::max()</code> function to compare two numbers and find the bigger one between them. It’s also defined inside the <code>&lt;algorithm&gt;</code> header file.</p>
<h3 id="heading-syntax-5">Syntax:</h3>
<pre><code class="lang-plaintext">max (a , b)
</code></pre>
<p><strong>Parameters:</strong></p>
<ul>
<li><p><strong>a:</strong> First number</p>
</li>
<li><p><strong>b:</strong> Second number</p>
</li>
</ul>
<p><strong>Return Value:</strong></p>
<ul>
<li><p>This function returns the larger number between the two numbers <strong>a</strong> and <strong>b.</strong></p>
</li>
<li><p>If the two numbers are equal, it returns the first number.</p>
</li>
</ul>
<h3 id="heading-example-code-5">Example Code:</h3>
<p>Here’s an example:</p>
<pre><code class="lang-cpp"><span class="hljs-comment">// max() function</span>

<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;algorithm&gt;  </span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;   </span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{
    <span class="hljs-comment">// Declare two integer variables</span>
    <span class="hljs-keyword">int</span> a = <span class="hljs-number">8</span> ;
    <span class="hljs-keyword">int</span> b = <span class="hljs-number">10</span> ;

    <span class="hljs-comment">// Use the max() function to find the larger number between a and b</span>
    <span class="hljs-keyword">int</span> maximum = max(a, b);

    <span class="hljs-comment">// Display the result with a meaningful message</span>
    <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"The maximum of "</span> &lt;&lt; a &lt;&lt; <span class="hljs-string">" and "</span> &lt;&lt; b &lt;&lt; <span class="hljs-string">" is: "</span> &lt;&lt; maximum &lt;&lt; <span class="hljs-built_in">endl</span>;

    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>Output:</p>
<p>The maximum of 8 and 10 is: 10</p>
<h2 id="heading-the-min-function">The <code>min()</code> Function</h2>
<p>You can use the <code>std::min()</code> function to compare two numbers and find the smaller of the two. It’s also defined inside the <code>&lt;algorithm&gt;</code> header file.</p>
<h3 id="heading-syntax-6">Syntax:</h3>
<pre><code class="lang-plaintext">min (a , b)
</code></pre>
<p><strong>Parameters:</strong></p>
<ul>
<li><p><strong>a:</strong> First number</p>
</li>
<li><p><strong>b:</strong> Second number</p>
</li>
</ul>
<p><strong>Return Value:</strong></p>
<ul>
<li><p>This function returns the smaller number between the two numbers <strong>a</strong> and <strong>b.</strong></p>
</li>
<li><p>If the two numbers are equal, it returns the first number.</p>
</li>
</ul>
<h3 id="heading-example-code-6">Example Code:</h3>
<p>Here’s an example:</p>
<pre><code class="lang-cpp"><span class="hljs-comment">// use of the min() function</span>

<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;algorithm&gt;  // For the built-in min() function</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;   </span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{
    <span class="hljs-comment">// Declare two integer variables to store user input</span>
    <span class="hljs-keyword">int</span> a = <span class="hljs-number">4</span> ;
    <span class="hljs-keyword">int</span> b = <span class="hljs-number">8</span> ;

    <span class="hljs-comment">// Use the min() function to find the smaller </span>
    <span class="hljs-keyword">int</span> smallest = min(a, b);

    <span class="hljs-comment">// Display the result </span>
    <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"The smaller number between "</span> &lt;&lt; a &lt;&lt; <span class="hljs-string">" and "</span> &lt;&lt; b &lt;&lt; <span class="hljs-string">" is: "</span> &lt;&lt; smallest &lt;&lt; <span class="hljs-built_in">endl</span>;

    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>Output:</p>
<p>The smaller number between 4 and 8 is: 4</p>
<h2 id="heading-the-swap-function">The <code>swap()</code> Function</h2>
<p>The <code>std::swap()</code> function lets you swap two values. It’s defined inside <code>&lt;algorithm&gt;</code> header file.</p>
<h3 id="heading-syntax-7"><strong>Syntax:</strong></h3>
<pre><code class="lang-plaintext">swap(a , b);
</code></pre>
<p><strong>Parameters:</strong></p>
<ul>
<li><p><strong>a:</strong> First number</p>
</li>
<li><p><strong>b:</strong> Second number</p>
</li>
</ul>
<p><strong>Return Value:</strong> This function does not return any value.</p>
<h3 id="heading-example">Example:</h3>
<p>Here’s how it works:</p>
<pre><code class="lang-cpp"><span class="hljs-comment">//  use of the swap() function</span>

<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;algorithm&gt;  // For the built-in swap() function</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;   </span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{
    <span class="hljs-keyword">int</span> firstNumber = <span class="hljs-number">8</span> ;
    <span class="hljs-keyword">int</span> secondNumber = <span class="hljs-number">9</span> ;


    <span class="hljs-comment">// Use the built-in swap() function to exchange values</span>
    swap(firstNumber, secondNumber);

    <span class="hljs-comment">// Display values after swapping</span>
    <span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"After the swap:"</span> &lt;&lt; <span class="hljs-built_in">endl</span>;
    <span class="hljs-built_in">cout</span> &lt;&lt; firstNumber &lt;&lt; <span class="hljs-string">" "</span> &lt;&lt; secondNumber &lt;&lt; <span class="hljs-built_in">endl</span>;

    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>Output:</p>
<p>After the swap:</p>
<p>9 8</p>
<h2 id="heading-the-tolower-function">The <code>tolower()</code> Function</h2>
<p>You can use the <code>tolower()</code> function to convert a given alphabet character to lowercase. It’s defined inside the <code>&lt;cctype&gt;</code> header.</p>
<h3 id="heading-syntax-8">Syntax:</h3>
<pre><code class="lang-plaintext">tolower (c);
</code></pre>
<p><strong>Parameter(s):</strong></p>
<ul>
<li><strong>c:</strong> The character to be converted.</li>
</ul>
<p><strong>Return Value:</strong></p>
<ul>
<li><p>Lowercase of the character c.</p>
</li>
<li><p>Returns c if c is not a letter.</p>
</li>
</ul>
<h3 id="heading-example-code-7">Example Code:</h3>
<p>Here’s how it works:</p>
<pre><code class="lang-cpp"><span class="hljs-comment">// C++ program</span>

<span class="hljs-comment">// use of tolower() function</span>

<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;cctype&gt;     </span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;   </span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{
    <span class="hljs-comment">// Declare and initialize a string with uppercase characters</span>
    <span class="hljs-built_in">string</span> str = <span class="hljs-string">"FRECODECAMP"</span>;

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">auto</span>&amp; a : str) {
        a = <span class="hljs-built_in">tolower</span>(a);
    }

    <span class="hljs-comment">// Print the modified string </span>
    <span class="hljs-built_in">cout</span> &lt;&lt; str;

    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>Output:</p>
<p>freecodecamp</p>
<h2 id="heading-the-toupper-function">The <code>toupper()</code> Function</h2>
<p>You can use the <code>toupper()</code> function to convert the given alphabet character to uppercase. It’s defined inside the <code>&lt;cctype&gt;</code> header.</p>
<h3 id="heading-syntax-9">Syntax:</h3>
<pre><code class="lang-plaintext">toupper (c);
</code></pre>
<p><strong>Parameters:</strong></p>
<ul>
<li><strong>c:</strong> The character to be converted.</li>
</ul>
<p><strong>Return Value</strong></p>
<ul>
<li><p>Uppercase of the character c.</p>
</li>
<li><p>Returns c if c is not a letter.</p>
</li>
</ul>
<h3 id="heading-example-code-8">Example Code:</h3>
<p>Here’s how it works:</p>
<pre><code class="lang-cpp"><span class="hljs-comment">// use of toupper() function</span>

<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;cctype&gt;     </span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;   </span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{
    <span class="hljs-comment">// Declare and initialize a string </span>
    <span class="hljs-built_in">string</span> str = <span class="hljs-string">"freecodecamp"</span>;

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">auto</span>&amp; a : str) {
        a = <span class="hljs-built_in">toupper</span>(a);
    }

    <span class="hljs-comment">// Output the converted uppercase string</span>
    <span class="hljs-built_in">cout</span> &lt;&lt; str;

    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>Output:</p>
<p>FREECODECAMP</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Inbuilt functions are helpful tools in competitive programming and in common programming tasks. These help in improving code readability and enhance the efficiency of code. In the above article, we discussed some very useful common inbuilt functions. Some common inbuilt functions are <code>max()</code>, <code>min()</code>, <code>sort()</code>, and <code>sqrt()</code>, etc. By using these inbuilt libraries, we can reduce boilerplate code and speed up the process of software development. These help in writing more concise, reliable, and maintainable C++ programs.</p>
<p>And if you'd like to support me and my work directly so I can keep creating these tutorials, <a target="_blank" href="https://paypal.me/ayushM010">you can do so here</a>. Thank you!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Write a PHP Script to Calculate the Area of a Triangle ]]>
                </title>
                <description>
                    <![CDATA[ In programming, being able to find the area of a triangle is useful for many reasons. It can help you understand logic-building and syntax, and it’s a common programming problem used in school assignments. There are also many real-world applications,... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/use-php-to-calculate-the-area-of-a-triangle/</link>
                <guid isPermaLink="false">68542db2cefef90e768ba1fe</guid>
                
                    <category>
                        <![CDATA[ PHP ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Mathematics ]]>
                    </category>
                
                    <category>
                        <![CDATA[ triangle ]]>
                    </category>
                
                    <category>
                        <![CDATA[ DSA ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Programming Blogs ]]>
                    </category>
                
                    <category>
                        <![CDATA[ MathJax ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ AYUSH MISHRA ]]>
                </dc:creator>
                <pubDate>Thu, 19 Jun 2025 15:33:06 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1750346934679/2e46bebb-9614-4f1a-afb5-9bbe27906b4e.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>In programming, being able to find the area of a triangle is useful for many reasons. It can help you understand logic-building and syntax, and it’s a common programming problem used in school assignments. There are also many real-world applications, such as computer graphics, geometry-based simulations, or construction-related calculations.</p>
<p>In this article, we’ll look at a common problem: we are given the dimensions of a triangle, and our task is to calculate its area. You can calculate the area of a triangle using different formulas, depending on the information you have about the triangle. Here, you’re going to learn how to do it using PHP.</p>
<h3 id="heading-after-reading-this-tutorial">After reading this tutorial:</h3>
<ul>
<li><p>You will understand the basic logic behind calculating the area of a triangle.</p>
</li>
<li><p>You will know how to write PHP code that calculates the triangle’s area using pre-defined and user-entered values.</p>
</li>
<li><p>You will know how to apply this logic in small projects and assignments.</p>
</li>
</ul>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ol>
<li><p><a class="post-section-overview" href="#heading-prerequisites">Prerequisites</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-find-the-area-of-a-triangle-using-direct-formulas">Find the Area of a Triangle Using Direct Formulas</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-find-the-area-of-a-triangle-using-the-base-and-height-approach">Find the Area of a Triangle Using the Base and Height Approach</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-find-the-area-of-a-triangle-using-herons-formula">Find the Area of a Triangle Using Heron's Formula</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-find-the-area-of-a-triangle-using-two-sides-and-included-angle-trigonometric-formula">Find the Area of a Triangle Using Two Sides and Included Angle (Trigonometric Formula)</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-conclusion">Conclusion</a></p>
</li>
</ol>
<h2 id="heading-prerequisites">Prerequisites</h2>
<p>You’ll understand this guide more easily if you have some knowledge about a few things:</p>
<h3 id="heading-basic-php">Basic PHP</h3>
<p>You’ll need to know basic PHP syntax to fully understand the problem. If you know how to write a simple echo statement or create a variable in PHP, then you should be good to go.</p>
<h3 id="heading-local-php-environment">Local PHP Environment</h3>
<p>To run the PHP code successfully, you should have local PHP development, such as XAMPP or WAMP, on your machine. You can also use online PHP editors like PHP Fiddle or OnlineGDB to run a PHP script without any installation.</p>
<p>In this tutorial we are going to explore three approaches to determine the area of the triangle in PHP based on the amount of information available about the triangle.</p>
<ul>
<li><p><strong>Base and Height Formula Approach:</strong> This approach is applicable when you have the perpendicular height from the base and length of the base in the problem.</p>
</li>
<li><p><strong>Heron’s Formula:</strong> This approach is used to calculate the area of triangle when you have the lengths of all three sides of the triangle.</p>
</li>
<li><p><strong>Trigonometric Formula Approach:</strong> This approach is applied on the problem when you have the length of two sides and the included angle between them.</p>
</li>
</ul>
<p>First, let’s go back to math class and use some direct formulas to find the area.</p>
<h2 id="heading-find-the-area-of-a-triangle-using-direct-formulas">Find the Area of a Triangle Using Direct Formulas</h2>
<h3 id="heading-example-1">Example 1:</h3>
<p>In this first example, you’re given the input base and height of a triangle. You have to return the area of the triangle. For this example, you’ll use a direct formula to calculate the area of the triangle.</p>
<p><strong>Input:</strong></p>
<p>Base = 5,</p>
<p>Height = 10</p>
<p>You can calculate the area of the triangle using the formula:</p>
<p>$$Area = (Base * Height) / 2$$</p><p>So, if you plug in the values you have, you get: (5* 10) / 2 = 25.</p>
<p><strong>Output:</strong></p>
<p>Area = 25</p>
<h3 id="heading-example-2">Example 2:</h3>
<p>In this second example, you’re given the length of two sides of a triangle and one angle between them. You have to return the area of the triangle. In this example, you’ll use another direct formula to calculate the area of the triangle.</p>
<p><strong>Input:</strong></p>
<p>Side A = 7, Side B = 9, Angle between them = 60°</p>
<p>In this case, you’ll use the formula:</p>
<p>$$Area = (1/2) A B * sin(Angle).$$</p><p>Then just substitute in the values you’ve been given to find the area.</p>
<p><strong>Output:</strong></p>
<p>Area = 27.33 (approximately)</p>
<p>Now let’s look at some different approaches to finding the area of a triangle using PHP.</p>
<h2 id="heading-find-the-area-of-a-triangle-using-the-base-and-height-approach">Find the Area of a Triangle Using the Base and Height Approach</h2>
<p>This is the simplest and most direct approach for calculating the area of a triangle when you know the base and height. In this approach, you’ll directly put values in the formula and find the area of the triangle – but you’ll do it with PHP code.</p>
<p>First, define the base and height of the triangle. Then apply the formula for the area of the triangle. As we saw above, the formula for the area of a triangle is:</p>
<p>$$Area = (Base * Height) / 2$$</p><p>After calculating the area of the triangle, output the answer.</p>
<p>Alright, so here’s how we can implement that in PHP:</p>
<pre><code class="lang-php"><span class="hljs-meta">&lt;?php</span>
<span class="hljs-comment">// Define the base and height</span>
$base = <span class="hljs-number">5</span>;
$height = <span class="hljs-number">10</span>;

<span class="hljs-comment">// Calculate the area</span>
$area = ($base * $height) / <span class="hljs-number">2</span>;

<span class="hljs-comment">// Output the result</span>
<span class="hljs-keyword">echo</span> <span class="hljs-string">"The area of the triangle is: "</span> . $area . <span class="hljs-string">" square units."</span>;
<span class="hljs-meta">?&gt;</span>
</code></pre>
<p>Output:</p>
<p>The area of the triangle is 25 square units.</p>
<p>In the above code, first we initialize the base and height of triangle in two variables. Then we plug those values into the area formula. PHP calculates the area of the triangle and displays the answer.</p>
<p><strong>Time Complexity</strong>: In the above approach, we are using the direct formula to calculate and return the area of the triangle, so the time complexity will be constant at O(1). The constant time complexity is efficient as it will remain constant, regardless of the size or values of the base and height.</p>
<p><strong>Space Complexity</strong>: The Space Complexity will be O(1). The space used by the above program is constant, which ensures minimal use of memory. This space complexity is ideal in environments where memory efficiency is a priority.</p>
<p>We use the above approach when we have the length of the base and height of the triangle (whether directly given or easily measurable in a right angle triangle). This method works best for right-angled triangles.</p>
<h2 id="heading-find-the-area-of-a-triangle-using-herons-formula">Find the Area of a Triangle Using Heron's Formula</h2>
<p>Heron’s formula is named after a Greek mathematician named Heron of Alexandria. Heron’s formula is useful when you know the lengths of all three sides of the triangle and you want to calculate the area without needing the height. This formula works for any type of triangle, including scalene triangles (triangles with sides of all different lengths).</p>
<p>Here’s Heron’s formula to calculate the area of a triangle:</p>
<p>$$√s(s−a)(s−b)(s−c) ​$$</p><p>Where:</p>
<ul>
<li><p>s = semi-perimeter = (a+b+c)/2 is the semi-perimeter of the triangle.</p>
</li>
<li><p>a, b, and c are the lengths of the sides.</p>
</li>
</ul>
<p>First, we define the three sides of the triangle. Then, we check all three conditions of the <a target="_blank" href="https://en.wikipedia.org/wiki/Triangle_inequality">Triangle Inequality Theorem</a> which states that if the sum of two sides is greater than the third side, then it is a valid triangle, and the given sides can form a triangle.</p>
<p>We can calculate the semi-perimeter of the triangle using the formula s = a+b+c/2. Then we can apply Heron's formula to calculate the area. After calculating the area, then output the answer.</p>
<p>Here’s how you can implement this in PHP:</p>
<pre><code class="lang-php"><span class="hljs-meta">&lt;?php</span>
<span class="hljs-comment">// Define the sides of the triangle</span>
$a = <span class="hljs-number">7</span>;
$b = <span class="hljs-number">9</span>;
$c = <span class="hljs-number">10</span>;

<span class="hljs-comment">// Check if the sides form a valid triangle using the Triangle Inequality Theorem</span>
<span class="hljs-keyword">if</span> (($a + $b &gt; $c) &amp;&amp; ($a + $c &gt; $b) &amp;&amp; ($b + $c &gt; $a)) {

    <span class="hljs-comment">// Calculate the semi-perimeter</span>
    $s = ($a + $b + $c) / <span class="hljs-number">2</span>;

    <span class="hljs-comment">// Calculate the area using Heron's formula</span>
    $area = sqrt($s * ($s - $a) * ($s - $b) * ($s - $c));

    <span class="hljs-comment">// Output the result</span>
    <span class="hljs-keyword">echo</span> <span class="hljs-string">"The area of the triangle is: "</span> . $area . <span class="hljs-string">" square units."</span>;

} <span class="hljs-keyword">else</span> {
    <span class="hljs-comment">// If the sides can't form a valid triangle</span>
    <span class="hljs-keyword">echo</span> <span class="hljs-string">"The given sides do not form a valid triangle."</span>;
}
<span class="hljs-meta">?&gt;</span>
</code></pre>
<p>Output:</p>
<p>The area of the triangle is: 27.321 square units.</p>
<p>In the above code, we first create three variables to store the lengths of the triangle’s sides, and check if the given sides form a valid triangle or not using the Triangle Inequality Theorem. Then we calculate the semi-perimeter using the formula: s = a + b + c / 2. We put the value of the semi-perimeter and lengths of all sides in Heron’s formula to calculate the area. The area of triangle is returned after calculating using the formula.</p>
<p><strong>Time Complexity</strong>: There is a total fixed number of operations such as addition, subtraction, multiplication, and square root. These operations don’t depend on input size as they are performed only a fixed number of times. This means that the time complexity is constant O(1).</p>
<p><strong>Space Complexity</strong>: We have used a fixed number of variables to calculate the area of the triangle. We have not used any additional data structures such as arrays or objects. The memory usage in the program is constant, which is better for low-memory environments. The space complexity is constant O(1).</p>
<p>This approach works best when the lengths of all sides are given. This approach is used mainly for scalene or isosceles triangles where height is directly not given. This approach can work for any type of triangle, however – scalene, isosceles, or equilateral.</p>
<h2 id="heading-find-the-area-of-a-triangle-using-two-sides-and-included-angle-trigonometric-formula">Find the Area of a Triangle Using Two Sides and Included Angle (Trigonometric Formula)</h2>
<p>In this approach, we will see a different variation of the problem. When you know two sides of a triangle and the included angle between them, you can calculate the area using this formula:</p>
<p>$$Area = 1/2 × a × b × sin(θ)$$</p><p>Where:</p>
<ul>
<li><p>a and b are the lengths of the two sides.</p>
</li>
<li><p>θ is the included angle between the two sides, measured in degrees or radians.</p>
</li>
</ul>
<p>Using the above formula, you can calculate the area of a triangle without needing its height. First, you define the two sides of the triangle and the angle between them. Then you convert the angle from degrees to radians if needed (in PHP, you can use deg2rad() to convert degrees to radians). Then you apply the formula.</p>
<p>After calculating the area of the triangle, output the result.</p>
<p>Here’s how to implement this in PHP:</p>
<pre><code class="lang-php"><span class="hljs-meta">&lt;?php</span>
<span class="hljs-comment">// Define the two sides and the included angle</span>
$a = <span class="hljs-number">7</span>;
$b = <span class="hljs-number">9</span>;
$angle = <span class="hljs-number">60</span>; <span class="hljs-comment">// Angle in degrees</span>

<span class="hljs-comment">// Convert the angle from degrees to radians</span>
$angle_in_radians = deg2rad($angle);

<span class="hljs-comment">// Calculate the area using the formula</span>
$area = <span class="hljs-number">0.5</span> * $a * $b * sin($angle_in_radians);

<span class="hljs-comment">// Output the result</span>
<span class="hljs-keyword">echo</span> <span class="hljs-string">"The area of the triangle is: "</span> . $area . <span class="hljs-string">" square units."</span>;
<span class="hljs-meta">?&gt;</span>
</code></pre>
<p>Output:</p>
<p>The area of the triangle is: 27.321 square units.</p>
<p>Explanation:</p>
<p>In the above case, we’re using the formula:</p>
<p>Area of Triangle = 1/2 × a × b × sin(θ)</p>
<p>And we’re substituting the following values into the formula:</p>
<p>Area= 1/2 × 7 × 9 × sin(60 ∘) ≈ 27.321</p>
<p>In the code, we declared two variables to store the length of the two sides of the triangle, and the variable <code>$angle</code> hold the included angle in degrees. We used <code>deg2rad()</code>, a PHP built-in function which converts an angle from degrees to radians. Then, we applied the actual formula: Area = 1/2 × 7 × 9 × sin(60 ∘). PHP stores the final answer in the <code>$area</code> variable.</p>
<p><strong>Time Complexity</strong>: We are using the direct formula to calculate the area of a triangle when the length of two sides and the angle between them are given. The constant time complexity is O(1).</p>
<p><strong>Space Complexity</strong>: Similarly, it does not take any extra space or use any data structures. It uses a single variable to store the result, which is why the space complexity is constant O(1).</p>
<p>This approach is perfect for the problem in which two sides and the included angle (angle between those sides) are known. You can use it when you cannot easily calculate the height of the triangle. This problem has real-life applications in geometry problems, CAD applications, or physics simulations. This method is very accurate and doesn’t require the length of all sides.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In this article, you’ve learned how you can calculate the area of a triangle, both manually and using PHP. You have seen different approaches and learned about which one is best given the information you have. First, we discussed the base and height approach, then looked at Heron’s formula, and finally examined how to handle things when two sides and the included angle are given.</p>
<p>Understanding the logic behind each of these approaches helps you choose the right one based on the given data.</p>
<p>And if you'd like to support me and my work directly so I can keep creating these tutorials, <a target="_blank" href="https://paypal.me/ayushM010">you can do so here</a>. Thank you!</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
