<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged camel-case</title>
<link>https://coderlegion.com/tag/camel-case</link>
<description>Powered by Question2Answer</description>
<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 id=&quot;conclusion&quot; 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>
</channel>
</rss>