TagProgramming

Thread-safe singleton class in C# (use it instead of static..but not always)

Static and Singleton are very different in their usage and implementation. So we need to wisely choose either of these two in our projects.Singleton is a design pattern that makes sure that your application creates only one instance of the class anytime. It is highly efficient and very graceful. Singletons have a static property that you must access to get the object reference. However, singleton...

How to convert a number to C string

Unfortunately in embedded systems sometimes it is not feasible to use strtol function to convert an integer to c string. Using the following C function you will be able to convert any string to an integer number. Although it is not an optimal solution but it will help you solve this kind of problem.  Updated Version Here (Please do not hesitate to provide any better...

How to left-trim a C string

Reading a file or any output stream usually comes back with leading to trailing whitespace. The following code snippet removes any whitespace on the left of a c string. char * str_ltrim(char * _arg) { if (_arg == NULL)return NULL; int i = 0, size = 0, j = 0; char * result; size = strlens(_arg)+1; while (isspace(*(_arg + (i++)))); if ((result = (char *)malloc((1 + (size - (i--)))*sizeof(char))) ==...

How to convert a C string to uppercase

The following code snippet provides an alternative method to convert any C string to uppercase. This is the reverse of How to convert a C string to lowercase. char * str_uppercase(char * _arg0) { int i = 0; if (_arg0 == NULL)return NULL; char * _temp = (char *) malloc((strlens(_arg0)+1)*sizeof(char)); strcpy(_temp, _arg0); do if (*(_temp + (i*sizeof(char))) >= 97 && *(_temp +...

How to convert a C string to lowercase

Sometimes I needed to convert a string to lowercase but it there wasn’t any function that could do that. The following code snippet provides an alternative method to convert any C string to lowercase. char * str_lowercase(char * _arg0) { int i = 0; if (_arg0 == NULL)return NULL; char * _temp = (char *) malloc((strlens(_arg0) + 1)*sizeof(char)); strcpy(_temp, _arg0); do if (*(_temp +...

How to reverse a C string

Usually reversing a string in C is not as easy as it could be. The following code snippet provides an alternative method to reverse any C string. char * str_reverse(char * _arg) { int i = 0; char _temp; if (_arg == NULL)return NULL; char * _temp1 = (char *) malloc((strlens(_arg)+1)*sizeof(char)); strcpy(_temp1, _arg); for (i = 0; i<(strlens(_arg)) / 2; i++) { _temp = *(_temp1 +...

Categories

Tags

Subscribe to our Newsletter