Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class school {
- public static void main(String args[]) {
- String[] array = new String [50];
- String sentence = "There are 87,000,000 people in Canada";
- int sentenceLength = sentence.length();
- int k=0; // counter for the array, "array"
- int h=0; // counter for first time inserting into array -- may not need!!
- // STEP ONE
- for (int i = 0; i<sentenceLength; i++) // set up the array with the words
- {
- if (array[k] == null) // make sure to empty the slot of the array element first!!
- {
- array[k] = "";
- }
- else if(sentence.charAt(i) != (' ')) // as long as we haven't reached a space,
- // update the string in the array with the new string
- {
- array[k] = array[k] + sentence.charAt(i);
- }
- else
- {
- k += 1; // go to the next spot to get the next word
- }
- }
- for(int j=0; j<array.length; j++) // Print out the array of words
- {
- if(array[j] != null)
- {
- System.out.println(array[j]);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement