I want to beak a sentence which has no spaces between next words.
Example :
String str = "johncancomewithme";
I want to print the string as follows:
"john can come with me"
My main concern is about occurrence of proper noun
I want to beak a sentence which has no spaces between next words.
Example :
String str = "johncancomewithme";
I want to print the string as follows:
"john can come with me"
My main concern is about occurrence of proper noun
There are many factors to consider. Can the string have multiple valid sentences (herowedashore = he rowed ashore AND hero wed a shore)? Do you have a list of all the words you need, including names and proper nouns? If so, this should be easily solvable.
First you'll probably want to load the list of words in memory, or if it's too large, have some sort of caching system in place for frequently used words. I think a Java HashMap would be the quickest way to store your bank of words.
Start by splitting your String into an array of one character Strings (there are many alternatives here, this is just one way):
String[] splitStr = str.split("");
Then, you want to loop thorugh your array from index 0. Loop through each letter adding it to a temporary String until your temp string forms a complete word. Then, bank it in a stack implementation (you could use another array as well, perhaps a Java ArrayList):
String word = "";
for (int i = 0; i < splitStr.length; i++) {
word = word + splitStr[i];
if (mapOfValidWords.get(word) != null ){
sentence.push(word); //Assuming sentence is a stack implementation
word = "";
}
Continue doing this until you get to the end of the String. If at the end of the loop you have any remaining characters (word != "") and it isn't a match for your list of words, you can assume that one or more of the words were not what this sentence requires.
If the original string was "hewillwalktowardsthecastle", your first iteration will give you "he will walk to ward sthecastle""
You'll need to pop the previously accepted word off of your stack "ward", decrement your counter by the length of that word, and skip over your previously deemed valid word to see if there's another valid word that longer. If not, you'll have to go further back and pop the next word of the stack, doing the same. In this case, you'll get "he will walk to wardsthecastle", then finally "he will walk towards the castle".
Assuming a valid input, eventually you'll reach a point where your sentence is separated into different values in the stack, and you can reconstruct it from there.
This is only returning one answer. If you need to return multiple answers then once you reach the end of your first answer, you'll need to go through your string again, and every "hit" of a word from the previous iteration needs to be tried by going one letter again, this time by continuing past the point where you stopped on your first go through.
This could likely be done through recursion, as you'll have a tree like structure. Your first sentence will be the root case, and each level deeper will attempt to go further with a word that succeeded in the first sentence. If that level of recursion finds a match, it will for it's own branch of recursion where this happens again. Eventually you'll reach a point where no more sentences can be formed from the String.