Convert the string into Camel case and remove special characters from string in C, C++

posted 4 min read

You might have encountered situation that's required to Convert the string into Camel case and remove special characters from string 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.

1. Identify the problem#

An usual string, such as this sentence "Hello World! How are you today?", is not only contain word seperated by space. It's also has some special characters like the question mark '?' or the exclamation mark '!'. We only need the words and digits to remain, and erase the rest.

Input

How old are you? I'm 15 years old

Output

HowOldAreYouIm15YearsOld

2. Solution #

Take the first example:

Hello World! How are you today?

It'll be easier to consider this problem as 2 smaller sub-problem: Converting a string to camel case, and removing all special characters 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.

1. Solution in C++

We first create a function 'toCamelCase(string str)' which takes a string as an input and converts it to camel case.


void to_camel_case(std::string &s) {
  if (s.empty()) return;
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == ' ' || s[i] == '_') {
      int j = i + 1;
      if (j < s.length()) {
        s[j] = toupper(s[j]);
      }
    }
  }
}

Then, we design a function called 'removeSpecialChars(string str)' to return the input string after removing all special characters.


void remove_special_characters(std::string &s) {
  if (s.empty()) return;
  for (int i = 0; i < s.length(); i++) {
    if (!isalnum(s[i])) {
      s.erase(i, 1);
      i--;
    }
  }
}

2. Solution in C

There seemes no obvious different when comparing to the solution in C++. First we convert string to camel case, then remove special characters.


void to_camel_case(char *s) {
  if (s == NULL) return;
  int len = strlen(s);
  if (len == 0) return;
  for (int i = 0; i < len; i++) {
    if (s[i] == ' ' || s[i] == '_') {
      int j = i + 1;
      if (j < len) {
        s[j] = toupper(s[j]);
      }
    }
  }
}

void remove_special_characters(char *s) {
  if (s == NULL) return;
  int len = strlen(s);
  if (len == 0) return;
  for (int i = 0; i < len; i++) {
    if (!isalnum(s[i])) {
      for (int j = i; j < len; j++) {
        s[j] = s[j + 1];
      }
      len--;
      i--;
    }
  }
}

But we need to pay attention to the type of string in C.

Caution 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.

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.

Finally, after execute 2 functions, we will be able to receive the desired result

Output
HelloWorldHowAreYouToday

3. Conclusion#

Going through this article, we've discussed about how to Convert the string into Camel case and remove special characters from string 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!

4. Reference#

For further information, please visit:

  1. toupper(): cplusplus.com/reference/cctype/toupper
  2. string: https://devdocs.io/c/string/byte

1 Comment

0 votes

More Posts

Beyond the 98.6°F Myth: Defining Personal Baselines in Health Management

Huifer - Feb 2

Just completed another large-scale WordPress migration — and the client left this

saqib_devmorph - Apr 7

Legacy in the Data: Transforming Family Medical History into a Blueprint for Longevity

Huifer - Jan 29

All about the function which is used to parse a string to int

wanderer - May 10

Bridging the Silence: Why Objective Data Outperforms Subjective Health Reports in Elderly Care

Huifer - Jan 27
chevron_left

Related Jobs

View all jobs →

Commenters (This Week)

2 comments
2 comments
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!