Counting number of occurrences of a char in a string in C
Here’s the way I’d do it (minimal number of variables needed): for (i=0; s[i]; s[i]==’.’ ? i++ : *s++);
Here’s the way I’d do it (minimal number of variables needed): for (i=0; s[i]; s[i]==’.’ ? i++ : *s++);
The ASCII table is arranged so that the value of the character ‘9’ is nine greater than the value of ‘0’; the value of the character ‘8’ is eight greater than the value of ‘0’; and so on. So you can get the int value of a decimal digit char by subtracting ‘0’. char x … Read more
Why does not give it out a compilation error or a runtime Exception? Because the language specification mandates that arithmetic on primitive types is modulo 2^width, so -1 becomes 2^16-1 as a char. In the section on integer operations, it is stated that The built-in integer operators do not indicate overflow or underflow in any … Read more
The string literal will be allocated in data segment. The pointer to it, a, will be allocated on the stack. Your code will eventually get transformed by the compiler into something like this: #include <stdio.h> const static char literal_constant_34562[7] = {‘t’, ‘e’, ‘s’, ‘a’, ‘j’, ‘a’, ‘\0’}; int main() { char *a; a = &literal_constant_34562[0]; … Read more
A string literal is a const char[] in C++, and may be stored in read-only memory so your program will crash if you try to modify it. Pointing a non-const pointer at it is a bad idea.
Clean method to check for vowels: public static boolean isVowel(char c) { return “AEIOUaeiou”.indexOf(c) != -1; }
First of all, a char array is an Object in Java just like any other type of array. It is just printed differently. PrintStream (which is the type of the System.out instance) has a special version of println for character arrays – public void println(char x[]) – so it doesn’t have to call toString for … Read more
Char is not valid primitive type for entity framework = entity framework doesn’t map it. If you check CSDL reference you will see list of valid types (char is not among them). Database char(1) is translated as string (SQL to CSDL translation). Char is described as non-unicode string with fixed length 1. The only ugly … Read more
What about: //true if it doesn’t contain letters bool result = hello.Any(x => !char.IsLetter(x));
Assuming s is non-empty: Character.isUpperCase(s.charAt(0)) or, as mentioned by divec, to make it work for characters with code points above U+FFFF: Character.isUpperCase(s.codePointAt(0));