0

I'm working on an assignment for class and a friend helped me write some code, and although I've looked up this character \0 and read the explanations, I'm requesting that someone explain exactly what it is doing in this method. Thank you so much. So again, I'm wondering the exact function or use of this character in my code: \0.

public String runLengthEncoding(String str){
  String encoded = "";
  char prev = '\0';
  char curr;
  int counter = 1;

  for (int i = 0; i < str.length(); i++) {
     curr = str.charAt(i);
        if(prev == curr) {
           //System.out.println("prev == curr");
           counter = counter + 1;
        }
        else {
           if(i != 0) {
              //System.out.println("encoded += counter + prev");
              encoded += Integer.toString(counter) + prev;
              //System.out.println(encoded);
              counter = 1;
           }
        }
        //System.out.println(Integer.toString(i) + str.length());
        if(i == str.length() - 1) {
            encoded += Integer.toString(counter) + curr;
           //System.out.println(encoded);
           counter = 1;
        }
     prev = curr;
     //System.out.println(i + ", " + prev + ", " + curr);
  }

  return encoded;

The purpose of this method is to iterate over a String, count the number of same characters at sequential indices of the String and generate a representative String of the original String. For example, if the original String is "RRRRSTTT", the method should return the String "4R1S3T". It is working great, I just had help coding this as I am quite new and am wondering exactly how \0 works and what it is used for.

  • 5
    It's an ascii nul and is being used as a sentinel (because your input string will not contain it). – Elliott Frisch Mar 26 '19 at 05:03
  • It looks like a escape sequence. \0 is the octal value for zero. They could have just assigned prev to 0 without any quotes or other weird stuff. https://stackoverflow.com/questions/1367322/what-are-all-the-escape-characters – markspace Mar 26 '19 at 05:04
  • 1
    it's just a marker. Ascii nul is often used to indicate end of input. – jwenting Mar 26 '19 at 05:05
  • 1
    It's just a char of value 0, watch they're probably using as a placeholder, assuming that three input string won't actually contain such a char. That's a valid char, though, so that assumption should be checked for in the method (and isn't being checked). – yshavit Mar 26 '19 at 05:05
  • 1
    It's exactly the same as `char prev = 0;` – Bohemian Mar 26 '19 at 05:05
  • It's a "valid char" but also `char`s are integers, and you can use them like integers too. 0, 1, 2, etc. are also valid values for a `char`. – markspace Mar 26 '19 at 05:06
  • It's a bug. If the input string starts with a NUL character, you will miscount. It would be better to set it equal to the first character, making sure to take care of zero length strings correctly, and start the counter from zero instead of 1. @ElliottFrisch There is no reason why a Java String can't contain a NUL. – user207421 Mar 26 '19 at 05:56

1 Answers1

1

They are using a NULL character as a starting value for the variable, so when they iterate, prev will never match the first character of the string that was passed (at least, not in the example you provided)

It's simply a placeholder for an uninitialized spot for placing the last character seen.

The function is replacing multiples of letters with a count and then a character.

Larry Williamson
  • 1,149
  • 5
  • 18