Reversing a string in C

If you want to practice advanced features of C, how about pointers? We can toss in macros and xor-swap for fun too! #include <string.h> // for strlen() // reverse the given null-terminated string in place void inplace_reverse(char * str) { if (str) { char * end = str + strlen(str) – 1; // swap the … Read more

.NET – How can you split a “caps” delimited string into an array?

I made this a while ago. It matches each component of a CamelCase name. /([A-Z]+(?=$|[A-Z][a-z])|[A-Z]?[a-z]+)/g For example: “SimpleHTTPServer” => [“Simple”, “HTTP”, “Server”] “camelCase” => [“camel”, “Case”] To convert that to just insert spaces between the words: Regex.Replace(s, “([a-z](?=[A-Z])|[A-Z](?=[A-Z][a-z]))”, “$1 “) If you need to handle digits: /([A-Z]+(?=$|[A-Z][a-z]|[0-9])|[A-Z]?[a-z]+|[0-9]+)/g Regex.Replace(s,”([a-z](?=[A-Z]|[0-9])|[A-Z](?=[A-Z][a-z]|[0-9])|[0-9](?=[^0-9]))”,”$1 “)

How to convert string to char array in C++?

Simplest way I can think of doing it is: string temp = “cat”; char tab2[1024]; strcpy(tab2, temp.c_str()); For safety, you might prefer: string temp = “cat”; char tab2[1024]; strncpy(tab2, temp.c_str(), sizeof(tab2)); tab2[sizeof(tab2) – 1] = 0; or could be in this fashion: string temp = “cat”; char * tab2 = new char [temp.length()+1]; strcpy (tab2, … Read more

How to format a Java string with leading zero?

public class LeadingZerosExample { public static void main(String[] args) { int number = 1500; // String format below will add leading zeros (the %0 syntax) // to the number above. // The length of the formatted string will be 7 characters. String formatted = String.format(“%07d”, number); System.out.println(“Number with leading zeros: ” + formatted); } }

length and length() in Java

Let me first highlight three different ways for similar purpose. length — arrays (int[], double[], String[]) — to know the length of the arrays length() — String related Object (String, StringBuilder, etc) — to know the length of the String size() — Collection Object (ArrayList, Set, etc) — to know the size of the Collection … Read more

Finding index of character in Swift String

You are not the only one who couldn’t find the solution. String doesn’t implement RandomAccessIndexType. Probably because they enable characters with different byte lengths. That’s why we have to use string.characters.count (count or countElements in Swift 1.x) to get the number of characters. That also applies to positions. The _position is probably an index into … Read more

how to read value from string.xml in android?

Try this String mess = getResources().getString(R.string.mess_1); UPDATE String string = getString(R.string.hello); You can use either getString(int) or getText(int) to retrieve a string. getText(int) will retain any rich text styling applied to the string. Reference: https://developer.android.com/guide/topics/resources/string-resource.html