Questions tagged [char]

char refers to the character data-type, representing letters, digits, punctuation marks, control characters, etc. Use this tag for questions relating to and usage of the character data-type.

The keyword char is used in many programming languages, often defining a 1-byte memory location representing ASCII, UTF-8, UTF-16, UTF-32, and ISO-8895-1 character encodings. Arrays of characters can create Strings used to form words, descriptive text, or even extremely large numbers unable to be represented exactly by 32 or 64 bits.

13281 questions
97
votes
2 answers

How to convert char to integer in C?

Possible Duplicates: How to convert a single char into an int Character to integer in C Can any body tell me how to convert a char to int? char c[]={'1',':','3'}; int i=int(c[0]); printf("%d",i); When I try this it gives 49.
Cute
  • 13,643
  • 36
  • 96
  • 112
94
votes
43 answers

Check string for palindrome

A palindrome is a word, phrase, number or other sequence of units that can be read the same way in either direction. To check whether a word is a palindrome I get the char array of the word and compare the chars. I tested it and it seems to work.…
DarkLeafyGreen
  • 69,338
  • 131
  • 383
  • 601
92
votes
8 answers

In Java, is the result of the addition of two chars an int or a char?

When adding 'a' + 'b' it produces 195. Is the output datatype char or int?
orange
  • 5,297
  • 12
  • 50
  • 71
87
votes
5 answers

Converting an int or String to a char array on Arduino

I am getting an int value from one of the analog pins on my Arduino. How do I concatenate this to a String and then convert the String to a char[]? It was suggested that I try char msg[] = myString.getChars();, but I am receiving a message that…
Chris
  • 5,485
  • 15
  • 68
  • 130
85
votes
6 answers

Java Switch Statement - Is "or"/"and" possible?

I implemented a font system that finds out which letter to use via char switch statements. There are only capital letters in my font image. I need to make it so that, for example, 'a' and 'A' both have the same output. Instead of having 2x the…
GlassZee
  • 1,117
  • 2
  • 11
  • 22
85
votes
2 answers

Ruby - How to select some characters from string

I am trying to find a function for select e.g. first 100 chars of the string. In PHP, there exists the substr function Does Ruby have some similar function?
user1946705
  • 2,858
  • 14
  • 41
  • 58
85
votes
11 answers

Converting a char to uppercase in Java

How could I get the f and l variables converted to uppercase? String lower = Name.toLowerCase(); int a = Name.indexOf(" ",0); String first = lower.substring(0, a); String last = lower.substring(a+1); char f = first.charAt(0); char l =…
shep
  • 869
  • 2
  • 8
  • 6
81
votes
4 answers

How can I convert a char to int in Java?

(I'm new at Java programming) I have for example: char x = '9'; and I need to get the number in the apostrophes, the digit 9 itself. I tried to do the following, char x = 9; int y = (int)(x); but it didn't work. So what should I do to get the…
Haim Lvov
  • 903
  • 2
  • 10
  • 20
79
votes
6 answers

"Too many characters in character literal error"

I'm struggling with a piece of code and getting the error: Too many characters in character literal error Using C# and switch statement to iterate through a string buffer and reading tokens, but getting the error in this line: case '&&': case…
Jinx
  • 807
  • 1
  • 6
  • 3
77
votes
16 answers

How to get the real and total length of char * (char array)?

For a char [], I can easily get its length by: char a[] = "aaaaa"; int length = sizeof(a)/sizeof(char); // length=6 However, I cannot do like this to get the length of a char * by: char *a = new char[10]; int length =…
herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
76
votes
7 answers

How to generate a random String in Java

I have an object called Student, and it has studentName, studentId, studentAddress, etc. For the studentId, I have to generate random string consist of seven numeric charaters, eg. studentId = getRandomId(); studentId = "1234567" <-- from the random…
chandra wibowo
  • 799
  • 1
  • 6
  • 5
74
votes
13 answers

Comparing chars in Java

I want to check a char variable is one of 21 specific chars, what is the shortest way I can do this? For example: if(symbol == ('A'|'B'|'C')){} Doesn't seem to be working. Do I need to write it like: if(symbol == 'A' || symbol == 'B' etc.)
Alex
  • 5,364
  • 9
  • 54
  • 69
73
votes
7 answers

How can I check if char* variable points to empty string?

How can I check if char* variable points to an empty string?
Aan
  • 12,247
  • 36
  • 89
  • 150
73
votes
7 answers

Best way to check if a character array is empty

Which is the most reliable way to check if a character array is empty? char text[50]; if(strlen(text) == 0) {} or if(text[0] == '\0') {} or do i need to do memset(text, 0, sizeof(text)); if(strlen(text) == 0) {} Whats the most efficient way…
ZPS
  • 1,566
  • 4
  • 16
  • 20
72
votes
8 answers

In Java, how to find if first character in a string is upper case without regex

In Java, find if the first character in a string is upper case without using regular expressions.
Vjy
  • 2,106
  • 3
  • 22
  • 34