Skip to content

Instantly share code, notes, and snippets.

@bhu1st
Created November 27, 2015 21:09
Show Gist options
  • Save bhu1st/fa070d2eed02d0c52473 to your computer and use it in GitHub Desktop.
Save bhu1st/fa070d2eed02d0c52473 to your computer and use it in GitHub Desktop.
Sentence Count
import java.io.*;
import java.util.*;
/*
* To execute Java, please define "static void main" on a class
* named Solution.
*
* If you need more classes, simply define them inline.
*/
/*
Input:
Rows: 4
Cols: 33
Sentence: {"The", "cat", "is", "wearing", "a", "red", "hat."}
The cat is wearing a red hat. The
cat is wearing a red hat. The cat
is wearing a red hat. The cat is
wearing a red hat. The cat is
Output: 4
*/
class Solution {
public static void main(String[] args) {
ArrayList<String> strings = new ArrayList<String>();
strings.add("Hello, World!");
strings.add("Welcome to CoderPad.");
strings.add("This pad is running Java 8.");
for (String string : strings) {
System.out.println(string);
}
int rows = 4;
int cols = 33;
String[] words = {"The", "cat", "is", "wearing", "a", "red", "hat."};
Solution test = new Solution();
int output = test.countSentence (rows, cols, words);
System.out.println(output);
}
public int countSentence (int rows, int cols, String[] words){
int maxChars = rows * cols;
String output = "";
int wordIndex = 0;
int wordArraySize = words.length;
int sentenceCount = 0;
while (output.length() <= maxChars){
output = output + " " + words[wordIndex];
//System.out.println(output);
wordIndex++;
if (wordIndex >= wordArraySize) {
sentenceCount++;
wordIndex = 0;
//System.out.println("count: " + sentenceCount);
}
}
return sentenceCount;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment