Array and string
I have an array of type char
and a string that I will be introducing from the keyboard. Can anyone tell me how can I开发者_C百科 introduce each character of the string in the char
array?
You don't need to create an array in advance. Here is the code
String s; //this is your string which you enter from keyboard
char[] c=s.toCharArray();
string.toCharArray()
will convert a String to a char array.
Alternatively, iterate over the string's characters and store them into your array:
char[] myArray = ...
int index = ...
for(int i = 0 ; i < string.length() ; i++) {
char c = string.charAt(i);
myArray[index] = c;
index++;
}
精彩评论