1

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 introduce each character of the string in the char array?

5
  • @Damodar: Just for future reference. Commented Sep 5, 2011 at 12:06
  • @Bobby : but this is not gud for every silly question making others alram. Its very very basic qestion, i can understand he is not familiar,we should not make them lazy just providing the answer from us with such questions. I feel ,there should be some self digging nature to the people. Not to depend on others. Commented Sep 5, 2011 at 15:09
  • @Damodar it is not a silly question. It is a beginner's question. If you feel this question is too easy for you - and the title suggests it IS a basic question - then move on to any of thousands of more difficult questions. Commented Sep 5, 2011 at 15:15
  • @Damodar: That's an ongoing dispute and discussion. Too simple question, General Reference close reason, Are some questions too simple?. May I also quote from the downvote-tooltip: This question does not show any research effort; it is unclear or not useful. Commented Sep 5, 2011 at 15:23
  • @Steve McLeod : My intention was not make people lazy by providing answers for simple questions, they should incorporate some self digging/learning habit, because, if we google such questions, there will be 100s of answers available from different sites.Also people will learn more on self learning. Commented Sep 5, 2011 at 15:28

2 Answers 2

7

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();
Sign up to request clarification or add additional context in comments.

Comments

4

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++;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.