Archive for December, 2013

Replace Non-Alphanumeric Characters in a C++ String

I needed to replace the non-alphanumeric characters in a std::string. While the Java String class has the replace() and replaceAll() methods for doing this, the std::string class has no such utility methods. Instead you can use the std::replace_if function from the Standard Template Library. Here’s some example code:

#include <iostream>
#include <string>
#include <algorithm>

int isNotAlphaNum(char c)
{
        return !std::isalnum(c);
}

int main(int argc, char* argv[])
{
        std::string s1 = "some/string/with*/nonalpha/characters+1";

        std::cout << s1 << " : ";
        std::replace_if(s1.begin(), s1.end(), isNotAlphaNum, ' ');
        std::cout << s1 << std::endl;

        return 0;
}

The third parameter of the replace_if() function is a pointer to a function that performs the desired check and returns either true or false if the input satisfies the condition. The last parameter is the character to replace the matched character, in this case a space.

This program produces the following:

$ ./replace.exe 
some/string/with*/nonalpha/characters+1
some string with  nonalpha characters 1

Tags: , , , , ,

Tuesday, December 10th, 2013 Cpp, Programming, Tips and Tricks No Comments

Convert C++ String to Lower Case (or Upper Case)

I don’t usually need to convert string case in C++ so when the need comes up I’ve usually forgotten how to do it and have to Google.

While the Java String class has toLowerCase() and toUpperCase(), C++ std::string does not have such a utility method. Instead, you need to use the std::transform() function. Here’s some example code:

#include <iostream>
#include <string>
#include <utility>

int main(int argc, char* argv[])
{
        std::string s1 = "lowertoupper";
        std::string s2 = "UPPERTOLOWER";

        std::cout << s1 << " : ";
        std::transform(s1.begin(), s1.end(), s1.begin(), ::toupper);
        std::cout << s1 << std::endl;

        std::cout << s2 << " : ";
        std::transform(s2.begin(), s2.end(), s2.begin(), ::tolower);
        std::cout << s2 << std::endl;

        return 0;
}

Produces the following:

$ ./case.exe 
lowertoupper : LOWERTOUPPER
UPPERTOLOWER : uppertolower

Note that while the Java toUpperCase() and toLowerCase() methods do not modify the original string, the std::transform function does.

Tags: , , , , ,

Tuesday, December 10th, 2013 Cpp, Programming, Tips and Tricks No Comments

Baby’s First Game

Baby's Fisrt Game I’ve just launched Baby’s First Game for Android on Google Play. This is a game designed specifically for infants, toddlers and preschool age children. They will have fun learning about colors, shapes, numbers and letters. The skill levels are very customizable and can grow with your child. This game was inspired by Baby Smash by Scott Hanselman and watching the games my own kids were interested in.

Three mini-games provide a fun way to get familiar with and learn to identify colors, shapes, numbers and letters. Just for Baby is targeted specifically at infants and young toddlers. It teaches action/reaction while familiarizing baby with colors and shapes. When baby touches the screen, a happy shape appears. Find It! tests your child’s ability to identify colors, shapes, letters and numbers by picking them out of a group. Pop It! is a fun way to work on your child’s hand/eye coordination while “popping” shapes as they fly across the screen.

Baby’s First Game is and always will be Ad free.

Baby’s First Game for Android is available now on Google Play:

Get it on Google Play

Tags: , , , , , , , , , , , , , , ,

Sunday, December 1st, 2013 Android, Java No Comments