Java 把一个句子分成几个单词,然后交换每个单词的首字母和末字母
我需要输入一个句子,让电脑把句子分成几个单词,然后交换每个单词的第一个和最后一个字母。我需要为此使用数组。到目前为止,我使用的代码可以输入一个单词并交换第一个和最后一个字母,但现在我需要修改它以输入一个句子。有没有关于我如何做到这一点的建议?我对数组不是很熟悉Java 把一个句子分成几个单词,然后交换每个单词的首字母和末字母,java,Java,我需要输入一个句子,让电脑把句子分成几个单词,然后交换每个单词的第一个和最后一个字母。我需要为此使用数组。到目前为止,我使用的代码可以输入一个单词并交换第一个和最后一个字母,但现在我需要修改它以输入一个句子。有没有关于我如何做到这一点的建议?我对数组不是很熟悉 public class FirstNLast { private String word; private String newWord; private StringBuilder strBuff;
public class FirstNLast {
private String word;
private String newWord;
private StringBuilder strBuff;
private int len;
private char firstLetter;
private char lastLetter;
public FirstNLast() {
word = "";
newWord = "";
strBuff = new StringBuilder();
len = 0;
firstLetter = ' ';
lastLetter = ' ';
}
public void setWord(String word) {
this.word = word;
}
public void compute() {
len = word.length();
firstLetter = word.charAt(0);
lastLetter = word.charAt(len - 1);
for (int i = 0; i < word.length(); i = i + 1) {
if (word.charAt(i) == firstLetter) {
strBuff.append(lastLetter);
} else if (word.charAt(i) == lastLetter) {
strBuff.append(firstLetter);
} else {
strBuff.append(word.charAt(i));
}
newWord = strBuff.toString();
}
}
public String getNewWord() {
return newWord;
}
}
public class FirstNLast{
私有字符串字;
私有字符串新词;
私人STRINGBUFF;
私家侦探;
私人信件;
私人信件;
公共FirstNLast(){
单词=”;
newWord=“”;
strBuff=新的StringBuilder();
len=0;
第一个字母=“”;
lastLetter='';
}
公共无效设置字(字符串字){
这个单词=单词;
}
公共空间计算(){
len=word.length();
第一个字母=word.charAt(0);
lastLetter=单词.charAt(len-1);
for(int i=0;i
使用该方法将句子按空格分隔。该方法将为您提供一个字符串数组(以单词为例)。然后遍历数组并执行需要执行的操作。类似这样的操作应该可以完成以下任务:
public String mutateSentence(String input) {
String[] words = input.split(" ");
String output = "";
for (int i=0;i<words.length;i++) {
String modifiedWord = yourMethodOfFlippingLetters(words[i]);
output += modifiedWord;
}
output.trim(); // removes the trailing space added
return output;
}
publicstringmutatesentence(字符串输入){
String[]words=input.split(“”);
字符串输出=”;
对于(int i=0;i使用如下的split()
String word,output = "";
String something = "This is a sentence with some words and spaces";
String[] parts = something.split(" ");
for(int i = 0; i < parts.length; i++){
word = flipLetters(parts[i]); /* swap the letters in the word*/
output += word + " ";
}
字符串字,输出=”;
String something=“这是一个包含一些单词和空格的句子”;
String[]parts=something.split(“”);
对于(int i=0;i
要在课堂上使用完整的句子:
FisrtNlast f = new FirstNLast()
String words[] = sentence.split(' ');
String words[] = sentence.split(' ');
String out = null;
for( word in words){
f.setWord(word);
f.compute();
out +=' ' + f.getNewWord();
}
sysout(out);
首先,你需要将句子分割成单独的单词成分。分割方法基本上是在每个空格处将句子分割成小块
String sentence = "The quick brown fox";
String[] words = sentence.Split(" ");
因此,在这里,词语等于:
{ "The", "quick", "brown", "fox" }
现在,每个单词都被分隔并放置到一个数组中,您可以遍历该数组并交换第一个和最后一个字母
for(int i = 0; i < words.length; i++)
{
char[] wordCharArray = word[i].toCharArray();
char firstLetter = wordCharArray[0]; // Holds the first so we can replace it.
wordCharArray[0] = wordCharArray[wordCharArray.length - 1]; // Sets the first letter as the last.
wordCharArray[wordCharArray.length - 1] = firstLetter; // Sets the last letter as the original first.
words[i] = new String(wordCharArray); // Just converts the char array back into a String.
}
sentence = new String(words);
for(int i=0;i
查找split()
方法。