Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 编写一个方法,在不使用任何标准函数的情况下,打印字词颠倒的字符串_Java_Reverse_Words - Fatal编程技术网

Java 编写一个方法,在不使用任何标准函数的情况下,打印字词颠倒的字符串

Java 编写一个方法,在不使用任何标准函数的情况下,打印字词颠倒的字符串,java,reverse,words,Java,Reverse,Words,我是在一次技术面试中被问到这个问题的。我什么都不知道,请帮帮我 它是无限循环的。我就是找不到正确的逻辑。 不是一次,而是两次我遇到这样的问题,所以请帮助我 public static int numberOfCharsInString(String sentence) { int numberOfChars = 0,i=0; while (!sentence.equals("")) { sentence = sentence.substring(1

我是在一次技术面试中被问到这个问题的。我什么都不知道,请帮帮我

它是无限循环的。我就是找不到正确的逻辑。 不是一次,而是两次我遇到这样的问题,所以请帮助我

public static int numberOfCharsInString(String sentence) 
{

    int numberOfChars = 0,i=0;

    while (!sentence.equals("")) 
    {
        sentence = sentence.substring(1);
        ++numberOfChars;
    }
        return numberOfChars;
}
public static void reverseSequenceOfWords(String inp)
{
    int len=numberOfCharsInString(inp);
    char[] in=inp.toCharArray();
    int i=0;
    for(i=len-1;i>=0;i--)
    {
        if(in[i]==' ')
        {
            while(!in.equals("")||in.equals(" "))
            {
                System.out.print(in[i]+" ");
            }
        }
        else if(in[i]=='\0')
        {
            break;
        }

    }

}
public static void main(String[] args)
{   
    int length=0;
    String inpstring = "";
        InputStreamReader input = new InputStreamReader(System.in);
    BufferedReader reader = new BufferedReader(input);
    try
    {
        System.out.print("Enter a string to reverse:");
        inpstring = reader.readLine();
        length=numberOfCharsInString(inpstring);
        System.out.println("Number of Characters: "+length);
        reverseSequenceOfWords(inpstring);
    }
    catch (Exception e)
            {
              e.printStackTrace();
        }       
}

我唯一还在使用的函数是IndexOf函数,但这并不难为自己创建

static void Main(string[] args)
{
    string sentence = "are you cracy";

    int length = Program.StringLength(sentence);

    int currentpos = 0;

    List<string> wordList = new List<string>();

    int wordCount = 0;
    while (currentpos < length)
    {
        // find the next space
        int spacepos = sentence.IndexOf(' ', currentpos);

        string word;

        if (spacepos < 0)
        {
            // end of string reached.
            word = sentence.Substring(currentpos, length - currentpos);
            wordList.Add(word);
            wordCount++;

            // no need to continue.
            break;
        }

        word = sentence.Substring(currentpos, spacepos - currentpos);
        wordList.Add(word);
        wordCount++;

        currentpos = spacepos + 1;
    }

    // display
    for (int i = wordList.Count - 1; i >= 0; i--)
    {
        // after first word is display, add spaces to the output
        if (i < wordList.Count - 1)
        {
            Console.WriteLine(" ");
        }

        // display word
        Console.WriteLine(wordList[i]);
    }
}

public static int StringLength(String sentence)
{
    int numberOfChars = 0;

    while (!sentence.Equals(""))
    {
        sentence = sentence.Substring(1);
        ++numberOfChars;
    }

    return numberOfChars;
}
static void Main(字符串[]args)
{
string-sense=“你是疯子吗”;
int length=程序.StringLength(句子);
int currentpos=0;
List wordList=新列表();
int字数=0;
while(当前位置<长度)
{
//找到下一个空间
int spacepos=句子索引(“”,currentpos);
字符串字;
if(spacepos<0)
{
//到达字符串的末尾。
单词=句子。子字符串(currentpos,长度-currentpos);
添加(word);
字数++;
//没有必要继续。
打破
}
单词=句子。子字符串(currentpos,spacepos-currentpos);
添加(word);
字数++;
currentpos=spacepos+1;
}
//展示
对于(int i=wordList.Count-1;i>=0;i--)
{
//显示第一个单词后,向输出中添加空格
if(i
蛮力逼得这么厉害哈哈

 public static void main (String args[]){       
    String input = new Scanner(System.in).nextLine();
    input+=" ";
    ArrayList<String> words = new ArrayList<String>();
    int start = 0;

    for(int i=0; i<input.length(); i++){
        if(input.charAt(i)==' '){
            String toAdd="";
            for(int r=start; r<i; r++){
                toAdd+=input.charAt(r);
            }
            words.add(toAdd);
            start = i+1;
        }
    }

    for(int i=words.size()-1; i>=0; i--){
        System.out.print(words.get(i)+" ");
    }


 }
publicstaticvoidmain(字符串args[]){
字符串输入=新扫描仪(System.in).nextLine();
输入+=”;
ArrayList words=新的ArrayList();
int start=0;

对于(int i=0;i我使用了
String.length()
String.substring()
String.charAt()
——我希望这是允许的

private static class Word {

    private final String message;
    private final int start;
    private final int end;

    public Word(String message, int start, int end) {
        this.message = message;
        this.start = start;
        this.end = end;
    }

    @Override
    public String toString() {
        return message.substring(start, end);
    }
}

private Word[] split(String message) {
    // Split it into words - there cannot be more words than characters in the message.
    int[] spaces = new int[message.length()];
    // How many words.
    int nWords = 0;
    // Pretend there's a space at the start.
    spaces[0] = -1;
    // Walk the message.
    for (int i = 0; i < message.length(); i++) {
        if (message.charAt(i) == ' ') {
            spaces[++nWords] = i;
        }
    }
    // Record the final position.
    spaces[++nWords] = message.length();
    // Build the word array.
    Word[] words = new Word[nWords];
    for (int i = 0; i < nWords; i++) {
        words[i] = new Word(message, spaces[i] + 1, spaces[i + 1]);
    }
    return words;
}

private String reverse(String message) {
    Word[] split = split(message);
    String reversed = "";
    for ( int i = split.length - 1; i >= 0; i--) {
        reversed += split[i].toString();
        if ( i > 0 ) {
            reversed += " ";
        }
    }
    return reversed;
}

public void test() {
    String message = "Hello how are you today?";
    System.out.println(reverse(message));
}

更简单但不太有用。仅使用
长度
字符
子字符串

public void printWordsReversed(String message) {
    int end = message.length();
    for ( int i = end - 1; i >= 0; i--) {
        if ( message.charAt(i) == ' ') {
            System.out.print(message.substring(i+1, end)+" ");
            end = i;
        }
    }
    System.out.print(message.substring(0, end));
}

根据空格分隔符将字符串拆分为数组,并在中打印该数组reverse@blank除非您解释如何在不使用
String.split
或任何其他“标准函数”的情况下执行此操作,否则不会有很大帮助@MarkoTopolnik然后问题变成了如何在没有split@MarkoTopolnik但同样,您使用
字符串来反转它所做的一切在某个时候都会使用“标准函数”;)@Markrotterveel只是想问OP:)必须有一个定义的允许的最小方法集。但是你可以猜测这些方法是什么:
charAt
是一个,
System.out.print
是另一个。“不使用任何标准函数”如果(i>0){print”“;}那么就打印这个词。@T.J.Crowder the.split()是这里的标准函数。@LanceJava:是的,但是有一个层次结构。我会假设
System.out.print
可以,但是
String#split
不会。只是一种感觉。我不知道
toCharArray
charAt
相比,在这个范围内会在哪里。@LanceJava没有e> toCharArray
。你所需要的就是
charAt
。这是什么语言?c#,顺便说一句,子字符串也是一个系统内函数。这是一个java问题。那么?我是否也应该为他执行整个面试?我给他一个工作解决方案,不管我用什么语言给他,因为一个通情达理的程序员可以轻松地翻译它。
today? you are how Hello
public void printWordsReversed(String message) {
    int end = message.length();
    for ( int i = end - 1; i >= 0; i--) {
        if ( message.charAt(i) == ' ') {
            System.out.print(message.substring(i+1, end)+" ");
            end = i;
        }
    }
    System.out.print(message.substring(0, end));
}