Skip to main content
found another bug
Source Link
ChrisWue
  • 20.6k
  • 4
  • 43
  • 107
  1. Bug: you assume that the result will never be longer than the original string while in fact it could be twice as long. For example abcd would yield a1b1c1d1.

  2. Instead of using memset to initialize result you can also initialize it to 0 like this:

     char result[len] = { 0 };
    
  3. You don't have to assign the counter on every iteration to the result. Simply increment the counter while the letter doesn't change and then add the count afterwards.

  4. Another bug: the way you convert the counter into a string will stop working for any number larger than 9. You want sprintf.

  5. Also strlen returns the length of the string excluding the terminating NULL character so your result is short by 1 in any case.

  6. Given the fairly severe bugs you really ought to test your code better.

  1. Bug: you assume that the result will never be longer than the original string while in fact it could be twice as long. For example abcd would yield a1b1c1d1.

  2. Instead of using memset to initialize result you can also initialize it to 0 like this:

     char result[len] = { 0 };
    
  3. You don't have to assign the counter on every iteration to the result. Simply increment the counter while the letter doesn't change and then add the count afterwards.

  4. Another bug: the way you convert the counter into a string will stop working for any number larger than 9. You want sprintf.

  5. Given the fairly severe bugs you really ought to test your code better.

  1. Bug: you assume that the result will never be longer than the original string while in fact it could be twice as long. For example abcd would yield a1b1c1d1.

  2. Instead of using memset to initialize result you can also initialize it to 0 like this:

     char result[len] = { 0 };
    
  3. You don't have to assign the counter on every iteration to the result. Simply increment the counter while the letter doesn't change and then add the count afterwards.

  4. Another bug: the way you convert the counter into a string will stop working for any number larger than 9. You want sprintf.

  5. Also strlen returns the length of the string excluding the terminating NULL character so your result is short by 1 in any case.

  6. Given the fairly severe bugs you really ought to test your code better.

added 131 characters in body
Source Link
ChrisWue
  • 20.6k
  • 4
  • 43
  • 107
  1. Bug: you assume that the result will never be longer than the original string while in fact it could be twice as long. For example abcd would yield a1b1c1d1.

  2. Instead of using memset to initialize result you can also initialize it to 0 like this:

     char result[len] = { 0 };
    
  3. You don't have to assign the counter on every iteration to the result. Simply increment the counter while the letter doesn't change and then add the count afterwards.

  4. Another bug: the way you convert the counter into a string will stop working for any number larger than 9. You want sprintf.

  5. Given the fairly severe bugs you really ought to test your code better.

  1. Bug: you assume that the result will never be longer than the original string while in fact it could be twice as long. For example abcd would yield a1b1c1d1.

  2. You don't have to assign the counter on every iteration to the result. Simply increment the counter while the letter doesn't change and then add the count afterwards.

  3. Another bug: the way you convert the counter into a string will stop working for any number larger than 9. You want sprintf.

  4. Given the fairly severe bugs you really ought to test your code better.

  1. Bug: you assume that the result will never be longer than the original string while in fact it could be twice as long. For example abcd would yield a1b1c1d1.

  2. Instead of using memset to initialize result you can also initialize it to 0 like this:

     char result[len] = { 0 };
    
  3. You don't have to assign the counter on every iteration to the result. Simply increment the counter while the letter doesn't change and then add the count afterwards.

  4. Another bug: the way you convert the counter into a string will stop working for any number larger than 9. You want sprintf.

  5. Given the fairly severe bugs you really ought to test your code better.

Source Link
ChrisWue
  • 20.6k
  • 4
  • 43
  • 107

  1. Bug: you assume that the result will never be longer than the original string while in fact it could be twice as long. For example abcd would yield a1b1c1d1.

  2. You don't have to assign the counter on every iteration to the result. Simply increment the counter while the letter doesn't change and then add the count afterwards.

  3. Another bug: the way you convert the counter into a string will stop working for any number larger than 9. You want sprintf.

  4. Given the fairly severe bugs you really ought to test your code better.