<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged string</title>
<link>https://coderlegion.com/tag/string</link>
<description>Powered by Question2Answer</description>
<item>
<title>Write a java program to count the letters, spaces, numbers and other characters of an input string.</title>
<link>https://coderlegion.com/26155/write-java-program-to-count-the-letters-spaces-numbers-and-other-characters-input-string</link>
<description>&lt;ol&gt;
&lt;li&gt;First take the input form the user as a string&lt;/li&gt;
&lt;li&gt;copy that string into a character array&lt;/li&gt;
&lt;li&gt;write logic for checking each character of character array is a letter/number/space/other-character&lt;/li&gt;
&lt;li&gt;print all the updated values of variables&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Note :- There is pre-built fuctions to copy all characters of string into a character array which is : &lt;br&gt;
char name_of_character_array[ ] = name_of_string.toCharArray and There are also pre-bilut functions are there to check weather character is letter/number/space in an array which are : Character.isLetter(ch[i]) , Character.isDigit(ch[i]) , Character.isSpaceChar(ch[i]) respectively&lt;/p&gt;
</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/26155/write-java-program-to-count-the-letters-spaces-numbers-and-other-characters-input-string</guid>
<pubDate>Thu, 03 Sep 2026 12:40:15 +0000</pubDate>
</item>
<item>
<title>Unity Devs: Stop Starving Your Frame Rate!</title>
<link>https://coderlegion.com/20516/unity-devs-stop-starving-your-frame-rate</link>
<description>
&lt;h2 id=&quot;unity-devs-stop-starving-your-frame-rate-embrace-modern-memory-discipline&quot;&gt;Unity Devs: Stop Starving Your Frame Rate! Embrace Modern Memory Discipline&lt;/h2&gt;

&lt;p&gt;It&amp;rsquo;s 2026, and I&amp;rsquo;m &lt;em&gt;still&lt;/em&gt; seeing incredible Unity games marred by avoidable Garbage Collection (GC) spikes, inexplicably dragging frame rates down from silky smooth to stuttering messes. We&amp;rsquo;ve got powerful tools like Burst, DOTS, and advanced asynchronous patterns at our fingertips, yet many developers are missing the fundamental lesson: memory discipline. Every &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;string&lt;/code&gt; concatenation, every &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;List&amp;lt;T&amp;gt;&lt;/code&gt; resize without pre-allocation, every casual LINQ query &lt;em&gt;hurts&lt;/em&gt;. These small, seemingly innocuous actions accumulate into significant performance bottlenecks as your game scales.&lt;/p&gt;

&lt;p&gt;The good news is that modern C# offers powerful, accessible solutions. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Span&amp;lt;T&amp;gt;&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Memory&amp;lt;T&amp;gt;&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ArrayPool&amp;lt;T&amp;gt;&lt;/code&gt; aren&amp;rsquo;t just for optimization gurus anymore; they&amp;rsquo;re &lt;em&gt;mandatory&lt;/em&gt; for any serious C# game developer aiming for consistent, high frame rates. It&amp;rsquo;s time to embrace a data-oriented mindset, even within your MonoBehaviour scripts, and stop treating memory as infinite. Your players (and their frame counters) will thank you.&lt;/p&gt;

&lt;h3 id=&quot;code-layout--walkthrough-taming-allocations&quot;&gt;Code Layout &amp;amp; Walkthrough: Taming Allocations&lt;/h3&gt;

&lt;p&gt;The core problem is the managed heap. When you &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;new&lt;/code&gt; up an object or perform an operation that implicitly allocates memory, that memory needs to be tracked. Eventually, the Garbage Collector (GC) steps in to reclaim unused memory. This process, however, is not free; it causes CPU overhead and can pause your game thread, leading to those dreaded frame spikes. Our goal is to minimize these allocations, especially during hot code paths.&lt;/p&gt;

&lt;h4 id=&quot;1-string-operations-beyond-basic-concatenation&quot;&gt;1. String Operations: Beyond Basic Concatenation&lt;/h4&gt;

&lt;p&gt;Strings are often the silent killers of frame rates because they are immutable. Every time you &amp;ldquo;modify&amp;rdquo; a string (e.g., concatenation with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+&lt;/code&gt;), you&amp;rsquo;re actually creating an entirely new string in memory.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Problematic Way:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;LogPlayerScore&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;playerId&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;score&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// Each '+' operator creates a new string intermediate&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;log&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Player &quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;playerId&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot; scored &quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;score&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot; points!&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; 
    &lt;span class=&quot;n&quot;&gt;Debug&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; 
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;The Optimized Way (using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StringBuilder&lt;/code&gt;):&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StringBuilder&lt;/code&gt; allows you to construct strings piece by piece without creating intermediate string objects for each part.&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;System.Text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Use a static/reusable StringBuilder to avoid allocating it repeatedly&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;readonly&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;StringBuilder&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s_stringBuilder&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;StringBuilder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;LogPlayerScoreOptimized&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;playerId&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;score&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;s_stringBuilder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Clear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Clear for reuse&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;s_stringBuilder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Player &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                   &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;playerId&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                   &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot; scored &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                   &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;score&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                   &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot; points!&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Debug&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s_stringBuilder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ToString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// One allocation for the final string&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;For advanced scenarios involving parsing or formatting into fixed-size character buffers, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Span&amp;lt;char&amp;gt;&lt;/code&gt; offers direct, allocation-free memory access.&lt;/p&gt;

&lt;h4 id=&quot;2-dynamic-collections--array-pooling&quot;&gt;2. Dynamic Collections &amp;amp; Array Pooling&lt;/h4&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;List&amp;lt;T&amp;gt;&lt;/code&gt; is convenient, but frequent &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;new List&amp;lt;T&amp;gt;()&lt;/code&gt; calls or implicit resizing (when &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Capacity&lt;/code&gt; is exceeded) are major allocation points. For temporary buffers, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ArrayPool&amp;lt;T&amp;gt;&lt;/code&gt; is your best friend.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Problematic Way:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Vector3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GetNearbyPositions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Vector3&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;center&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;radius&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Vector3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;positions&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Vector3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;();&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Allocation&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// ... logic to populate 'positions' ...&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;positions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Potentially triggers resize allocations during population&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;The Optimized Way (using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ArrayPool&amp;lt;T&amp;gt;&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Span&amp;lt;T&amp;gt;&lt;/code&gt;):&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ArrayPool&amp;lt;T&amp;gt;&lt;/code&gt; provides a pool of reusable arrays, drastically reducing allocations for temporary collections. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Span&amp;lt;T&amp;gt;&lt;/code&gt; then gives you a high-performance, allocation-free &amp;ldquo;view&amp;rdquo; into that raw array memory.&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;System.Buffers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;UnityEngine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// For Vector3&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;ProcessTemporaryVectorData&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// Rent a buffer from the shared pool. Avoids allocation if available.&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// 'count' should be the maximum expected size.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Vector3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buffer&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ArrayPool&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Vector3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Shared&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Rent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; 
    
    &lt;span class=&quot;c1&quot;&gt;// Create a Span&amp;lt;Vector3&amp;gt; as a view over the rented buffer&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Span&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Vector3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buffer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;AsSpan&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; 

    &lt;span class=&quot;c1&quot;&gt;// Use 'data' (Span&amp;lt;Vector3&amp;gt;) here; operations are allocation-free&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;++)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Vector3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Mathf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Sin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Mathf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Cos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Debug&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;$&quot;Processed &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; vectors. First: &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]}&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// IMPORTANT: Return the buffer to the pool when you're done.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ArrayPool&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Vector3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Shared&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Return&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buffer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; 
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Span&amp;lt;T&amp;gt;&lt;/code&gt; allows direct manipulation of the array&amp;rsquo;s elements without any intermediate copies or allocations.&lt;/p&gt;

&lt;h4 id=&quot;3-linqs-hidden-costs&quot;&gt;3. LINQ&amp;rsquo;s Hidden Costs&lt;/h4&gt;

&lt;p&gt;LINQ (Language Integrated Query) is expressive, but many extension methods (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Where&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Select&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OrderBy&lt;/code&gt;, etc.) implicitly create enumerator objects or even new collections behind the scenes, leading to GC pressure.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Problematic Way:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;System.Collections.Generic&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;System.Linq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Requires System.Linq namespace&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;IEnumerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GameObject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GetActiveEnemies&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GameObject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;allObjects&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// Creates an enumerator object for Where, potentially for ToList()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;allObjects&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Where&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;activeSelf&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;CompareTag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Enemy&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
                     &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ToList&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Explicitly allocates a new List&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;The Optimized Way (Manual Iteration &amp;amp; Reusable Collections):&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For performance-critical code, a good old-fashioned loop is often superior. Even better, pass in a pre-allocated &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;List&amp;lt;T&amp;gt;&lt;/code&gt; to store results, effectively eliminating repeat allocations.&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;System.Collections.Generic&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;UnityEngine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Pass in a 'results' list to be cleared and reused&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GetActiveEnemiesNoAlloc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GameObject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;allObjects&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GameObject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Clear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Clear the existing list for reuse&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;foreach&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;allObjects&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;activeSelf&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;CompareTag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Enemy&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Add to the reusable list&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Here, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;results&lt;/code&gt; is only allocated once (or whenever its capacity needs expansion), avoiding &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;new List&amp;lt;GameObject&amp;gt;()&lt;/code&gt; every frame or every call.&lt;/p&gt;

&lt;h3 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;GC spikes are not an inevitable evil; they are often a symptom of insufficient memory discipline. By consciously adopting tools like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StringBuilder&lt;/code&gt; for string manipulation, leveraging &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ArrayPool&amp;lt;T&amp;gt;&lt;/code&gt; for temporary buffers, and embracing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Span&amp;lt;T&amp;gt;&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Memory&amp;lt;T&amp;gt;&lt;/code&gt; for high-performance memory access, you can drastically reduce your game&amp;rsquo;s allocation footprint. Combine this with the practice of reusing collections instead of constantly instantiating new ones, and you&amp;rsquo;ll see tangible improvements.&lt;/p&gt;

&lt;p&gt;Embrace this data-oriented mindset. Understand where your memory is going. Stop treating memory as infinite. Your players will experience smoother, more consistent frame rates, and your game will feel significantly more polished. Start integrating these practices into your Unity projects today.&lt;/p&gt;
</description>
<category>Other</category>
<guid isPermaLink="true">https://coderlegion.com/20516/unity-devs-stop-starving-your-frame-rate</guid>
<pubDate>Sat, 13 Jun 2026 11:48:39 +0000</pubDate>
</item>
<item>
<title>Convert the string into Camel case and remove special characters from string in C, C++</title>
<link>https://coderlegion.com/18847/convert-the-string-into-camel-case-and-remove-special-characters-from-string-in-c-c</link>
<description>&lt;p&gt;&lt;strong&gt;You might have encountered situation that's required to &lt;b&gt;Convert the string into Camel case and remove special characters from string&lt;/b&gt; in C or C++. To solve this problem, we can simply converting the string to camel case, then eliminating all special characters. In this article, we will also go over details with examples.&lt;/strong&gt;&lt;/p&gt;
&lt;div id=&quot;tc-div&quot;&gt;
&lt;h4&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;&lt;strong&gt;Table of Contents &lt;a id=&quot;tc-toogle&quot; class=&quot;open&quot;&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/h4&gt;
&lt;div id=&quot;tc-list&quot;&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#idt&quot;&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;Identify the problem&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#sol&quot;&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;Solution&lt;/span&gt;&lt;/a&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#sol1&quot;&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;Solution in C++&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#sol2&quot;&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;Solution in C&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#con&quot;&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;Conclusion&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#ref&quot;&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;Reference&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;&lt;/div&gt;&lt;/div&gt;
&lt;section&gt;
&lt;a id=&quot;idt&quot; class=&quot;custom_anchor&quot;&gt;&lt;/a&gt; &lt;h2&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;&lt;strong&gt;1. Identify the problem&lt;/strong&gt;&lt;/span&gt;&lt;a id=&quot;identify-the-problem&quot; rel=&quot;nofollow&quot; href=&quot;#idt&quot; style=&quot;color: blue&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;An usual string, such as this sentence &lt;strong&gt;&quot;Hello World! How are you today?&quot;&lt;/strong&gt;, is not only contain word seperated by space. It's also has some &lt;span style=&quot;color: #ff04fc;&quot;&gt;special characters&lt;/span&gt; like the question mark &lt;b&gt;'?'&lt;/b&gt; or the exclamation mark &lt;b&gt;'!'&lt;/b&gt;. We only need the words and digits to remain, and erase the rest.  &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Input&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;How old are you? I'm 15 years old&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;HowOldAreYouIm15YearsOld&lt;/code&gt;&lt;/pre&gt;
&lt;/section&gt;
&lt;section&gt;
&lt;a id=&quot;sol&quot; class=&quot;custom_anchor&quot;&gt;&lt;/a&gt; &lt;h2&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;&lt;strong&gt;2. Solution&lt;/strong&gt;&lt;/span&gt; &lt;a id=&quot;solution&quot; rel=&quot;nofollow&quot; href=&quot;#sol&quot; style=&quot;color: blue&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Take the first example:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Hello World! How are you today?&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It'll be easier to consider this problem as 2 smaller sub-problem: Converting a string to camel case, and removing all &lt;span style=&quot;color: #ff04fc;&quot;&gt;special characters&lt;/span&gt; from a string. We will go into the details of the solution with both C and C++, as well as things to keep in mind for each method.&lt;/p&gt;

&lt;a id=&quot;sol1&quot; class=&quot;custom_anchor&quot;&gt;&lt;/a&gt; &lt;h3&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;&lt;strong&gt;1. Solution in C++&lt;/strong&gt;&lt;/span&gt;&lt;/h3&gt;

&lt;p&gt;We first create a function &lt;code&gt;'toCamelCase(string str)'&lt;/code&gt; which takes a string as an input and converts it to camel case. &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;void to_camel_case(std::string &amp;amp;s) {
  if (s.empty()) return;
  for (int i = 0; i &amp;lt; s.length(); i++) {
    if (s[i] == ' ' || s[i] == '_') {
      int j = i + 1;
      if (j &amp;lt; s.length()) {
        s[j] = toupper(s[j]);
      }
    }
  }
}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then, we design a function called &lt;code&gt;'removeSpecialChars(string str)'&lt;/code&gt; to return the input string after removing all special characters.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;void remove_special_characters(std::string &amp;amp;s) {
  if (s.empty()) return;
  for (int i = 0; i &amp;lt; s.length(); i++) {
    if (!isalnum(s[i])) {
      s.erase(i, 1);
      i--;
    }
  }
}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;a id=&quot;sol2&quot; class=&quot;custom_anchor&quot;&gt;&lt;/a&gt; &lt;h3&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;&lt;strong&gt;2. Solution in C&lt;/strong&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;p&gt;There seemes no obvious different when comparing to the solution in C++. First we convert string to camel case, then remove special characters.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;
&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;void to_camel_case(char *s) {
  if (s == NULL) return;
  int len = strlen(s);
  if (len == 0) return;
  for (int i = 0; i &amp;lt; len; i++) {
    if (s[i] == ' ' || s[i] == '_') {
      int j = i + 1;
      if (j &amp;lt; len) {
        s[j] = toupper(s[j]);
      }
    }
  }
}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;pre&gt;&lt;code&gt;
&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;void remove_special_characters(char *s) {
  if (s == NULL) return;
  int len = strlen(s);
  if (len == 0) return;
  for (int i = 0; i &amp;lt; len; i++) {
    if (!isalnum(s[i])) {
      for (int j = i; j &amp;lt; len; j++) {
        s[j] = s[j + 1];
      }
      len--;
      i--;
    }
  }
}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;But we need to pay attention to the type of &lt;code&gt;&lt;strong&gt;string&lt;/strong&gt;&lt;/code&gt; in C.&lt;/p&gt;
&lt;div class=&quot;div-pink&quot;&gt;
&lt;span class=&quot;alert-header&quot;&gt;Caution&lt;/span&gt;
&lt;span class=&quot;alert-body&quot;&gt;In C++, we use the std::string class to handle strings, which provides a lot of helpful methods for manipulating strings, such as erase and empty. In C, we have to use arrays of characters, and manually manage memory allocation and deallocation.&lt;/span&gt;
&lt;/div&gt;

&lt;p&gt;The C++ implementation provides a more convenient and easier-to-use interface for string manipulation, while the C implementation provides a lower-level interface that gives you more control over memory and performance.&lt;/p&gt;

&lt;p&gt;Finally, after execute 2 functions, we will be able to receive the desired result&lt;/p&gt;
&lt;strong&gt;Output&lt;/strong&gt;
&lt;pre&gt;&lt;code&gt;HelloWorldHowAreYouToday&lt;/code&gt;&lt;/pre&gt;&lt;/section&gt;
&lt;section&gt;
&lt;a id=&quot;con&quot; class=&quot;custom_anchor&quot;&gt;&lt;/a&gt; &lt;h2&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;&lt;strong&gt;3. Conclusion&lt;/strong&gt;&lt;/span&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#con&quot; style=&quot;color: blue&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Going through this article, we've discussed about how to &lt;b&gt;Convert the string into Camel case and remove special characters from string&lt;/b&gt; in C and C++ , by raising the first letters from each word to uppercase first, after that removing the special characters. I recommend reading in details to understand better how it works and knows the different between 2 cases. Have a nice day!&lt;/p&gt;&lt;/section&gt;
&lt;section&gt;
&lt;a id=&quot;ref&quot; class=&quot;custom_anchor&quot;&gt;&lt;/a&gt; &lt;h2&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;&lt;strong&gt;4. Reference&lt;/strong&gt;&lt;/span&gt;&lt;a id=&quot;reference&quot; rel=&quot;nofollow&quot; href=&quot;#ref&quot; style=&quot;color: blue&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;For further information, please visit:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;i&gt;toupper()&lt;/i&gt;:&lt;a rel=&quot;nofollow&quot; href=&quot;https://cplusplus.com/reference/cctype/toupper/&quot;&gt; cplusplus.com/reference/cctype/toupper&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;string&lt;/b&gt;:&lt;a rel=&quot;nofollow&quot; href=&quot;https://devdocs.io/c/string/byte&quot;&gt; https://devdocs.io/c/string/byte&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/18847/convert-the-string-into-camel-case-and-remove-special-characters-from-string-in-c-c</guid>
<pubDate>Mon, 25 May 2026 04:00:43 +0000</pubDate>
</item>
<item>
<title>All about the function which is used to parse a string to int</title>
<link>https://coderlegion.com/17268/all-about-the-function-which-is-used-to-parse-a-string-to-int</link>
<description>&lt;p style=&quot;text-align: justify;&quot;&gt;There are so many programming languages in the world that are being used to create different applications. All of those programming languages, at some point, need to take data as input from the user and perform operations on user data. Some information is numeric while the other is alphabetic and some contain a combination of alphabets, numbers, and symbols. But everything entered by a user is in String format by default. So, in every programming language, there is a function that is used to convert that string to numbers. In this article, we are going to see the&lt;strong&gt; functions used to parse a string to integer &lt;/strong&gt;in 5 different programming languages i.e. C, C++, Java, C#, and Python.&lt;/p&gt;&lt;h2 style=&quot;text-align: justify;&quot;&gt;&lt;strong&gt;Parsing String to int in C:&lt;/strong&gt;&lt;/h2&gt;&lt;p style=&quot;text-align: justify;&quot;&gt;To &lt;strong&gt;parse a string to int&lt;/strong&gt;, we use&lt;span style=&quot;color:#e74c3c&quot;&gt;&lt;em&gt;&lt;strong&gt; &lt;/strong&gt;&lt;/em&gt;&lt;/span&gt;the&lt;span style=&quot;color:#e74c3c&quot;&gt;&lt;em&gt;&lt;strong&gt; atoi()&lt;/strong&gt;&lt;/em&gt;&lt;/span&gt; function. &lt;em&gt;&lt;strong&gt;&lt;span style=&quot;color:#e74c3c&quot;&gt;atoi() &lt;/span&gt;&lt;/strong&gt;&lt;/em&gt;is the abbreviation of ASCII to Integer. The syntax for using this method is as follows:&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;atoi(string);&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;text-align: justify;&quot;&gt;This method returns the integer representation of the string. If the string is not numeric, it returns 0. Here is an example program that demonstrates the use of the &lt;span style=&quot;color:#e74c3c&quot;&gt;&lt;em&gt;&lt;strong&gt;atoi()&lt;/strong&gt;&lt;/em&gt;&lt;/span&gt; function:&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;#include 
#include
#include

int main()
{
    int number = atoi(&quot;6&quot;);    //Converting 6 from string to integer
    printf(&quot;%d&quot;,number);   //printing the result
    return 0;
}
&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;text-align: justify;&quot;&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;6&lt;/code&gt;&lt;/pre&gt;&lt;h2 style=&quot;text-align: justify;&quot;&gt;&lt;strong&gt;Parsing String to int in C++:&lt;/strong&gt;&lt;/h2&gt;&lt;p style=&quot;text-align: justify;&quot;&gt;As you might already know that C++ is the advanced version of C, there is not a lot of difference in the functions used in C and C++. In C++, we have &lt;span style=&quot;color:#e74c3c&quot;&gt;&lt;em&gt;&lt;strong&gt;stoi() &lt;/strong&gt;&lt;/em&gt;&lt;/span&gt;function. stoi is the abbreviated form of String to Integer. The syntax for using this function is as follows:&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;stoi(string);&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;text-align: justify;&quot;&gt;This function returns the integer representation of the string passed to it. If the string is not numeric, it returns 0. Here is an example program:&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;#include 
#include
using namespace std;

int main()
{
    int number = stoi(&quot;6&quot;);    //Converting 6 from string to integer
    cout&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;text-align: justify;&quot;&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;6&lt;/code&gt;&lt;/pre&gt;&lt;h2 style=&quot;text-align: justify;&quot;&gt;&lt;strong&gt;Parsing String to Int in Java:&lt;/strong&gt;&lt;/h2&gt;&lt;p style=&quot;text-align: justify;&quot;&gt;Java is an easy-to-learn programming language that provides so many easy ways to perform complex tasks. There is an Integer class that contains a method named&lt;span style=&quot;color:#e74c3c&quot;&gt;&lt;em&gt;&lt;strong&gt; parseInt()&lt;/strong&gt;&lt;/em&gt;&lt;/span&gt; that can be used to convert a string to an integer. Just like the rest functions in the rest of the programming languages, the &lt;span style=&quot;color:#e74c3c&quot;&gt;&lt;em&gt;&lt;strong&gt;parseInt()&lt;/strong&gt;&lt;/em&gt;&lt;/span&gt; returns the numeric representation of the string. The syntax for using this method is as follows:&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot;&gt;public class Main
{
public static void main(String[] args) {
    String str = &quot;1234&quot;;
int number = Integer.parseInt(str);
System.out.println(number);
}
}
&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;text-align: justify;&quot;&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot;&gt;1234&lt;/code&gt;&lt;/pre&gt;&lt;h2 style=&quot;text-align: justify;&quot;&gt;&lt;strong&gt;Parse String to Integer in C#:&lt;/strong&gt;&lt;/h2&gt;&lt;p style=&quot;text-align: justify;&quot;&gt;In C#, we have two ways to convert a string to an integer:&lt;/p&gt;&lt;ul&gt;&lt;li style=&quot;text-align: justify;&quot;&gt;Using the &lt;em&gt;&lt;strong&gt;ToInt()&lt;/strong&gt;&lt;/em&gt; method of the Convert class.&lt;/li&gt;&lt;li style=&quot;text-align: justify;&quot;&gt;Using&lt;em&gt;&lt;strong&gt; int.Parse() &lt;/strong&gt;&lt;/em&gt;method&lt;/li&gt;&lt;/ul&gt;&lt;p style=&quot;text-align: justify;&quot;&gt;Let’s see both one by one:&lt;/p&gt;&lt;h3 style=&quot;text-align: justify;&quot;&gt;&lt;strong&gt;Convert.ToInt():&lt;/strong&gt;&lt;/h3&gt;&lt;p style=&quot;text-align: justify;&quot;&gt;This method is used according to the following syntax:&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;language-cs&quot;&gt;Convert.ToInt(string);&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;text-align: justify;&quot;&gt;This method also returns the numeric representation of the string passed to it.&lt;/p&gt;&lt;p style=&quot;text-align: justify;&quot;&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;language-cs&quot;&gt;using System;
class HelloWorld {
  static void Main() {
    string str = &quot;1234&quot;;
    int number = Convert.ToInt32(str);
    Console.WriteLine(number);
  }
}&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;text-align: justify;&quot;&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;language-cs&quot;&gt;1234&lt;/code&gt;&lt;/pre&gt;&lt;h3 style=&quot;text-align: justify;&quot;&gt;&lt;strong&gt;Int32.Parse():&lt;/strong&gt;&lt;/h3&gt;&lt;p style=&quot;text-align: justify;&quot;&gt;The syntax for using this method is as follows:&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;language-cs&quot;&gt;int32.Parse(string);&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;text-align: justify;&quot;&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;language-cs&quot;&gt;using System;
class HelloWorld {
  static void Main() {
    string str = &quot;1234&quot;;
    int number = Int32.Parse(str);
    Console.WriteLine(number);
  }
}&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;text-align: justify;&quot;&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;language-cs&quot;&gt;1234&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;text-align: justify;&quot;&gt;&lt;strong&gt;Both of these methods throw an exception when the string passed to them is not numeric.&lt;/strong&gt;&lt;/p&gt;&lt;h2 style=&quot;text-align: justify;&quot;&gt;&lt;strong&gt;Parse String to Int in Python:&lt;/strong&gt;&lt;/h2&gt;&lt;p style=&quot;text-align: justify;&quot;&gt;Python is the simplest yet complex language of all but converting a string to an integer is easiest in Python. You can do so by following the following syntax:&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;int(string)&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;text-align: justify;&quot;&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;print(int(&quot;1234&quot;))&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;text-align: justify;&quot;&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;1234&lt;/code&gt;&lt;/pre&gt;&lt;p style=&quot;text-align: justify;&quot;&gt;If the string passed to this function is non-numeric, the Python gives an error.&lt;/p&gt;&lt;p style=&quot;text-align: justify;&quot;&gt;I hope you find this article helpful!&lt;/p&gt;
</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/17268/all-about-the-function-which-is-used-to-parse-a-string-to-int</guid>
<pubDate>Sun, 10 May 2026 10:37:33 +0000</pubDate>
</item>
<item>
<title>TypeError: unsupported operand type(s) for +: 'int' and 'str' in Python</title>
<link>https://coderlegion.com/14377/typeerror-unsupported-operand-type-s-for-int-and-str-in-python</link>
<description>&lt;p&gt;&lt;strong&gt;The exception or error message &lt;span style=&quot;color: #ff04fc;&quot;&gt;TypeError: unsupported operand type(s) for +: 'int' and 'str'&lt;/span&gt; appears because an &lt;span style=&quot;color: #ff04fc;&quot;&gt;integer&lt;/span&gt; and a &lt;span style=&quot;color: #ff04fc;&quot;&gt;string&lt;/span&gt; operands are operated by the &lt;span style=&quot;color: #ff04fc;&quot;&gt;+&lt;/span&gt; or addition operator. The &lt;span style=&quot;color: #ff04fc;&quot;&gt;operands&lt;/span&gt; are the objects of type &lt;span style=&quot;color: #ff04fc;&quot;&gt;int&lt;/span&gt; and &lt;span style=&quot;color: #ff04fc;&quot;&gt;str&lt;/span&gt;. This error is usually encountered by people new to Python. This post will discuss this mistake and how to avoid it.&lt;/strong&gt;&lt;/p&gt;
&lt;div id=&quot;toc-div&quot;&gt;
&lt;h4&gt;&lt;strong&gt; Table of Contents&lt;a id=&quot;toc-toogle&quot; class=&quot;open&quot;&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/h4&gt;
&lt;div id=&quot;toc-list&quot;&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#what&quot;&gt;&lt;strong&gt;What is the problem?&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#how&quot;&gt;&lt;strong&gt;How to avoid this error?&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#con&quot;&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#ref&quot;&gt;&lt;strong&gt;References&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;section&gt;&lt;a id=&quot;what&quot; class=&quot;custom_anchor&quot;&gt;&lt;/a&gt;
&lt;h2&gt;&lt;strong&gt;1.  What is the problem? &lt;a style=&quot;color: blue;&quot; rel=&quot;nofollow&quot; href=&quot;#what&quot;&gt;#&lt;/a&gt;&lt;/strong&gt;&lt;/h2&gt;
&lt;/section&gt;
&lt;p&gt;Let us dissect the exception message &lt;strong&gt;TypeError: unsupported operand type(s) for +: 'int' and 'str'&lt;/strong&gt;.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#ref&quot;&gt;&lt;strong&gt;TypeError&lt;/strong&gt;&lt;/a&gt; - It is an exception class that tells us there is an error in the code related to the type of the object and the operator used on the object.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;unsupported operand&lt;/strong&gt; - The operand or items being operated. For example in &lt;strong&gt;2 + 5&lt;/strong&gt;, the &lt;strong&gt;2&lt;/strong&gt; and &lt;strong&gt;5&lt;/strong&gt; are called operands and the &lt;strong&gt;+&lt;/strong&gt; is called the operator.  &lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#ref&quot;&gt;&lt;strong&gt;type(s)&lt;/strong&gt;&lt;/a&gt; - The type() is a &lt;a rel=&quot;nofollow&quot; href=&quot;#ref&quot;&gt;built-in&lt;/a&gt; function used to get the type or class of an object. &lt;/li&gt;
&lt;li&gt;&lt;strong&gt;+&lt;/strong&gt; - The operator that performs addition if the operands are numeric like &lt;strong&gt;int&lt;/strong&gt; 50 or &lt;strong&gt;float&lt;/strong&gt; 25.5, or concatenation if operands are &lt;strong&gt;str&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;'int' and &lt;a rel=&quot;nofollow&quot; href=&quot;#ref&quot;&gt;'str'&lt;/a&gt;&lt;/strong&gt; - The type of operands, one is of type &lt;strong&gt;int&lt;/strong&gt; and the other is of type &lt;strong&gt;str&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Let us look at some examples of how to get and check the type of an object.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How to get the type of an object?&lt;/strong&gt;&lt;br&gt;
Use the &lt;a rel=&quot;nofollow&quot; href=&quot;#ref&quot;&gt;type()&lt;/a&gt; built-in function.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;var_1 = 100
var_1_type = type(var_1)
print(var_1_type)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;em&gt;output&lt;/em&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;class 'int'&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;How to check if an object is an int or integer?&lt;/strong&gt;&lt;br&gt;
Use the &lt;a rel=&quot;nofollow&quot; href=&quot;#ref&quot;&gt;insinstance()&lt;/a&gt; built-in function.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;var_2 = 50
if isinstance(var_2, int):
    print(f'var_2 is of type int')
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;em&gt;output&lt;/em&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;var_2 is of type int
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;How to check if an object is an str or string?&lt;/strong&gt;&lt;br&gt;
Use the &lt;a rel=&quot;nofollow&quot; href=&quot;#ref&quot;&gt;isinstance()&lt;/a&gt; built-in function.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;var_3 = 'east'
if isinstance(var_3, str):
    print(f'var_3 is of type str')
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;em&gt;output&lt;/em&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;var_3 is of type str
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let us try to duplicate the exception message.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;# Define our variables.
num_fruits_oranges = 7
num_fruits_apples = '3'

# Get the sum from our variables.
total_fruits = num_fruits_oranges + num_fruits_apples

print(f'total fruits: {total_fruits}')
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;em&gt;output&lt;/em&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Traceback (most recent call last):
  File &quot;f:\Project\kodlogs\art4.py&quot;, line 7, in &amp;lt;module&amp;gt;
    total_fruits = num_fruits_oranges + num_fruits_apples
TypeError: unsupported operand type(s) for +: 'int' and 'str'
&lt;/code&gt;&lt;/pre&gt;
&lt;div class=&quot;div-pink&quot;&gt;
&lt;span class=&quot;alert-header&quot;&gt;Caution&lt;/span&gt;
&lt;span class=&quot;alert-body&quot;&gt;TypeError: 'TypeError: unsupported operand type(s) for +: 'int' and 'str'&lt;/span&gt;
&lt;/div&gt;
&lt;p&gt;We have two &lt;strong&gt;operands&lt;/strong&gt;, &lt;em&gt;num_fruits_oranges&lt;/em&gt; of type &lt;strong&gt;int&lt;/strong&gt; and &lt;em&gt;num_fruits_apples&lt;/em&gt; of type &lt;strong&gt;str&lt;/strong&gt;. The &lt;strong&gt;+&lt;/strong&gt; operator attempts to add the operands. The &lt;strong&gt;TypeError&lt;/strong&gt; occurred because Python does not &lt;strong&gt;support the addition&lt;/strong&gt; between int and str objects.&lt;/p&gt;
&lt;section&gt;&lt;a id=&quot;how&quot; class=&quot;custom_anchor&quot;&gt;&lt;/a&gt;
&lt;h2&gt;&lt;strong&gt;2.  How to avoid this error? &lt;a style=&quot;color: blue;&quot; rel=&quot;nofollow&quot; href=&quot;#how&quot;&gt;#&lt;/a&gt;&lt;/strong&gt;&lt;/h2&gt;
&lt;/section&gt;
&lt;p&gt;To &lt;strong&gt;avoid&lt;/strong&gt; this type of &lt;strong&gt;exception&lt;/strong&gt;, we need to know the &lt;strong&gt;types&lt;/strong&gt; of objects to add. If one object is a &lt;strong&gt;string&lt;/strong&gt; and the other is an &lt;strong&gt;integer&lt;/strong&gt;, we should not add them. Integers are whole numbers and can have a value of negative, positive and 0 values. &lt;a rel=&quot;nofollow&quot; href=&quot;#ref&quot;&gt;&lt;strong&gt;Strings&lt;/strong&gt;&lt;/a&gt; are characters enclosed in single and double quotes. A string can also be a single character, such as the letter 'a.'&lt;/p&gt;
&lt;p&gt;If for some reason we don't know the type of a variable, we can use the &lt;a rel=&quot;nofollow&quot; href=&quot;#ref&quot;&gt;isinstance()&lt;/a&gt; built-in function to check if it is an &lt;strong&gt;int&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;code&lt;/em&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;def weird_function(isnumber=True):
    return 10 if isnumber else 'numeric'
    
# Get the value of the weird function.
value = weird_function()

# Check if value is an int or not.
if isinstance(value, int):
    print('value is of type int')
else:
    print('value is not of type int')
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;em&gt;output&lt;/em&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;value is of type int
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Once we know that the object is an &lt;strong&gt;int&lt;/strong&gt;, we can then proceed with our addition operation.&lt;/p&gt;
&lt;p&gt;There is a possibility that the &lt;strong&gt;operand&lt;/strong&gt; is a numeric string, such as &lt;strong&gt;&quot;10&quot;&lt;/strong&gt;. In this scenario, we must first convert the &lt;strong&gt;str&lt;/strong&gt; to an &lt;strong&gt;int&lt;/strong&gt; object before doing any integer-specific operations.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;code&lt;/em&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;# Ask the age, we expect a number.
num_years = input('How old are you? ')

print(f'num_years value is {num_years}')

# Assuming we dont' know the return type of the input function,
# we will use the isinstance() function to know if it is an int.
if isinstance(num_years, int):
    print('num_years is an int')
else:
    print('num_years is not an int')

    # Convert the numeric string to int.
    print('Convert the num_years str to int using int().')
    num_years_int = int(num_years)

    # Check the type of num_years_int.
    print(f'num_years_int is of type {type(num_years_int)}')
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After the &lt;strong&gt;str&lt;/strong&gt; is successfully converted to &lt;strong&gt;int&lt;/strong&gt; using the built-in function &lt;a rel=&quot;nofollow&quot; href=&quot;#ref&quot;&gt;&lt;strong&gt;int()&lt;/strong&gt;&lt;/a&gt;, we can then continue operating on it.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;sample output&lt;/em&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;How old are you? 54
num_years value is 54
num_years is not an int
Convert the num_years str to int using int().
num_years_int is of type &amp;lt;class 'int'&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;div class=&quot;div-faq&quot;&gt;
&lt;span class=&quot;faq-header&quot;&gt;FAQ&lt;/span&gt;
&lt;p&gt;
&lt;span class=&quot;faq-body&quot;&gt;How to check if a variable or object type is &lt;strong&gt;int&lt;/strong&gt;?&lt;/span&gt;
&lt;strong&gt;Answer&lt;/strong&gt;: Use the built-in function &lt;strong&gt;isinstance(variable, int)&lt;/strong&gt;. If it returns True, it is &lt;strong&gt;int&lt;/strong&gt;.
&lt;/p&gt;

&lt;p&gt;
How to know the type or class of a variable or object?&lt;br&gt;
&lt;strong&gt;Answer&lt;/strong&gt;: Use the built-in function &lt;strong&gt;type(variable)&lt;/strong&gt;.
&lt;/p&gt;

What is the return type of an &lt;strong&gt;input()&lt;/strong&gt; built-in function?&lt;br&gt;
&lt;strong&gt;Answer&lt;/strong&gt;: The return type is &lt;strong&gt;str&lt;/strong&gt;.
&lt;/div&gt;
&lt;section&gt;&lt;a class=&quot;custom_anchor&quot;&gt;&lt;/a&gt;
&lt;h2&gt;&lt;strong&gt;3.  Conclusion &lt;a style=&quot;color: blue;&quot; rel=&quot;nofollow&quot; href=&quot;#con&quot;&gt;#&lt;/a&gt;&lt;/strong&gt;&lt;/h2&gt;
&lt;/section&gt;
&lt;p&gt;We will encounter the exception message &lt;span style=&quot;color: #ff04fc;&quot;&gt;&lt;strong&gt;TypeError: unsupported operand type(s) for +: 'int' and 'str'&lt;/strong&gt;&lt;/span&gt; if we try to add an &lt;strong&gt;integer&lt;/strong&gt; object with the &lt;strong&gt;string&lt;/strong&gt; object. We can only add two or more operands if they are numeric such as &lt;strong&gt;int&lt;/strong&gt; or &lt;strong&gt;float&lt;/strong&gt;. We can avoid this errror by knowing the object type. We can use the &lt;a rel=&quot;nofollow&quot; href=&quot;#ref&quot;&gt;&lt;strong&gt;isinstance()&lt;/strong&gt;&lt;/a&gt; built-in function to check if the object is of type &lt;strong&gt;int&lt;/strong&gt; or &lt;strong&gt;str&lt;/strong&gt;. We can also determine the type or class of the object by using another built-in function &lt;a rel=&quot;nofollow&quot; href=&quot;#ref&quot;&gt;&lt;strong&gt;type()&lt;/strong&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;section&gt;&lt;a class=&quot;custom_anchor&quot;&gt;&lt;/a&gt;
&lt;h2&gt;&lt;strong&gt;4.  References &lt;a style=&quot;color: blue;&quot; rel=&quot;nofollow&quot; href=&quot;#ref&quot;&gt;#&lt;/a&gt;&lt;/strong&gt;&lt;/h2&gt;
&lt;/section&gt;
&lt;ul&gt;
&lt;li&gt;Python-Dev. (n.d.-a). &lt;strong&gt;built-in-functions&lt;/strong&gt;. Retrieved November 10, 2022, from &lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.python.org/3/library/functions.html#built-in-functions&quot;&gt;https://docs.python.org/3/library/functions.html#built-in-functions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Python-Dev. (n.d.-b). &lt;strong&gt;input&lt;/strong&gt;. Retrieved November 22, 2022, from &lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.python.org/3/library/functions.html#input&quot;&gt;https://docs.python.org/3/library/functions.html#input&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Python-Dev. (n.d.-c). &lt;strong&gt;int&lt;/strong&gt;. Retrieved November 22, 2022, from &lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.python.org/3/library/functions.html#int&quot;&gt;https://docs.python.org/3/library/functions.html#int&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Python-Dev. (n.d.-d). &lt;strong&gt;isinstance&lt;/strong&gt;. Retrieved November 22, 2022, from &lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.python.org/3/library/functions.html#isinstance&quot;&gt;https://docs.python.org/3/library/functions.html#isinstance&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Python-Dev. (n.d.-e). &lt;strong&gt;Text Sequence Type - str&lt;/strong&gt;. Retrieved November 22, 2022, from &lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.python.org/3/library/stdtypes.html#textseq&quot;&gt;https://docs.python.org/3/library/stdtypes.html#textseq&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Python-Dev. (n.d.-f). &lt;strong&gt;type&lt;/strong&gt;. Retrieved November 16, 2022, from &lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.python.org/3/library/functions.html#type&quot;&gt;https://docs.python.org/3/library/functions.html#type&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Python-Dev. (n.d.-g). &lt;strong&gt;TypeError&lt;/strong&gt;. Retrieved November 15, 2022, from &lt;a rel=&quot;nofollow&quot; href=&quot;https://docs.python.org/3/library/exceptions.html#TypeError&quot;&gt;https://docs.python.org/3/library/exceptions.html#TypeError&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/14377/typeerror-unsupported-operand-type-s-for-int-and-str-in-python</guid>
<pubDate>Thu, 09 Apr 2026 23:33:33 +0000</pubDate>
</item>
</channel>
</rss>