How to realize a diff function?

后端 未结 5 2029
耶瑟儿~
耶瑟儿~ 2021-02-04 22:49

How can I implement a diff function, such as Stack Overflow\'s question revision history?

5条回答
  •  余生分开走
    2021-02-04 23:14

    I guess the only way would be to compare each character forming the 2 strings . Something like this :

    
    void diff(String first,String second) {
       int biggest = (first.length() > second.length()) ? first.length() : second.length();
       for(int i = 0;i < biggest;i++) {
          //compare each char from the longest string with each char from the shorter
          // do something with them if they're not equal
       }
    }
    

    This is just a sketch of how I would do it . Everything depends on what you want to do with the data .

提交回复
热议问题