0

We are given an array of Strings and what we require is a char[], i.e, array of all characters in all the Strings For example:

Input: [i, love, you]

output: [i, l, o, v, e, y, o, u]

First I made an array of arrays.

Then I have found the length of the required char[] array.

I have tried the following so far:

char[][] a1 = new char[str.length][];

for(int i =0;i<str.length;i++){
    a1[i]=str[i].toCharArray();
}

int total=0;
for(int i =0;i<str.length;i++){
    total = total + a1[i].length;
}

char[] allchar = new char[total];

for(int i=0;i<str.length;i++){
    //NOW HERE I WANT TO MERGE ALL THE char[] ARRAYS TOGETHER.
//HOW SHOULD I DO THIS?
}
19
  • 2
    stop doing brainstorming, and write some code. That would rather help. Commented Oct 25, 2013 at 16:04
  • 4
    string.toCharArray(); Now stop thinking and code. Commented Oct 25, 2013 at 16:05
  • 1
    The String documentation would be a good place to start. Commented Oct 25, 2013 at 16:07
  • 9
    Why so rude? A beginner can ask questions on StackOverflow, can't he? Sick of all the downvotes I get! Commented Oct 25, 2013 at 16:10
  • 6
    You can ask a question, but you need to show you've made some effort. How can I ... is a bad format for StackOverflow questions. StackOverflow questions should typically be in the following format: This is what I want to accomplish. This is what I've tried. Here is the code that I've actually compiled (or attempted to compile). I'm having this result. I'm expecting this result. What have I done wrong? If you want someone else to write the code to turn an array of Strings into an array of char, then you should hire a programmer. Commented Oct 25, 2013 at 16:12

8 Answers 8

8
String[] sArray = {"i", "love", "you"};
    String s = "";
    for (String n:sArray)
        s+= n;
    char[] c = s.toCharArray();
Sign up to request clarification or add additional context in comments.

1 Comment

stringbuilder would be more efficient for longer input.
2

You can do that like this

char[] allchar = new char[total]; // Your code till here is proper

// Copying the contents of the 2d array to a new 1d array
int counter = 0; // Counter as the index of your allChar array
for (int i = 0; i < a1.length; i++) { 
    for (int j = 0; j < a1[i].length; j++) { // nested for loop - typical 2d array format
        allchar[counter++] = a1[i][j]; // copying it to the new array
    }
}

Comments

1

You can do something like the following method..

    public void converter(String[] stringArray) {


    char[] charsInArray = new char[500];    //set size of char array    
    int counterChars = 0;

    for (int i = 0; i < stringArray.length; i++) { 

        int counterLetters = 0; //declare counter for for amount of letters in each string in the array 

        for (int j = 0; j < stringArray[i].length(); j++) { 


            // below pretty self explanatory extracting individual strings and a
            charsInArray[counterChars] = stringArray[i].charAt(counterLetters);

            counterLetters++;
            counterChars++;
        }
    }


    }

Comments

1
String [] s = new String[]{"i", "love", "you"};

int length = 0;
for (int i = 0; i < s.length; i++) {        
    for (int j = 0; j < s[i].length(); j++) {
        length++;
    }
}

char [] c = new char[length];
int k = 0;
for (int i = 0; i < s.length; i++) {        
    for (int j = 0; j < s[i].length(); j++) {
        c[k] = s[i].charAt(j);
        k++;
    }
}
for (int j = 0; j < c.length; j++) {
    System.out.print("char is: " + c[j]);
}

Comments

1

In Java 8 we can use streams.

public char[] convert(String[] words) {
    return Arrays.stream(words)
            .collect(Collectors.joining())
            .toCharArray();
}

Here we created a stream from an array using Array.stream(T[] array) where T is the type of the array elements. Than we obtain a string using Collectors.joining(). Finally we get a char array using the String's toCharArray() method.

Comments

0
public static void main(String[] args)
{
    String sentence="Gokul Krishna is class leader";
    String[] array=sentence.split("\\s");
    int counter;
    //String word = new String();
    for(String word:array)
    {
        counter=0;
        char[] wordArray=word.toCharArray();
        for(int i=0;i<wordArray.length;i++)
        {
            counter++;
        }
        System.out.println(word+ " :" +counter);
    }
}

1 Comment

"Code only" answers do little to help a novice understand. Please consider explaining your solution.
0
public char[] convert(String[] arr) {
    StringBuilder sb = new StringBuilder();
    for (String s : arr) {
        sb.append(s);
    }
    return sb.toString().toCharArray();
}

Comments

-1
String value = "i love java to write";
Stream.of(value.split("")).filter(i -> !i.equals(" ")).forEach(System.out::println);

2 Comments

Why do you prefer this approach over the previous answers? Why should a reader consider this?
Additionally to Jeremy's request for more information I think it is worth pointing out that the expected input is an Array of Strings, which is not the same as a String. So this answer doesn't address the original question.

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.