Skip to main content
added 104 characters in body
Source Link
Andre Kampling
  • 5.6k
  • 2
  • 23
  • 48

YourThe condition of your for loop is wrong. It should be i < start + count, so that your for loop have to be:

for (i = start; i < start + count; i++) { ... } 

to count in your case e.g. 3, 4, 5, 6 until the end (6 will not be executed) is reached, also note the < is used and not the <=.
Output will be: "act" in your case.

You can substitute your for loop where you copy every single character by strncpy which is described at this manpage (synoptics: char *strncpy(char *dest, const char *src, size_t n);).

It would be like this:

strncpy(result, str + start, count); result[start + count] = '\0'; 

Further you should think of making your function robust against buffer overflows of the input (str) and output (result) string by using strlen on the input buffer and passing the buffer size of the output buffer to the function and checking these lengths against start and count. Another possibility would be to allocate the output buffer dynamically with the right size but nevertheless the input buffer should be checked.

Your for loop have to be:

for (i = start; i < start + count; i++) { ... } 

to count in your case e.g. 3, 4, 5, 6 until the end (6) is reached, also note the < is used and not the <=.
Output will be: "act" in your case.

You can substitute your for loop where you copy every single character by strncpy which is described at this manpage (synoptics: char *strncpy(char *dest, const char *src, size_t n);).

It would be like this:

strncpy(result, str + start, count); result[start + count] = '\0'; 

Further you should think of making your function robust against buffer overflows of the input (str) and output (result) string by using strlen on the input buffer and passing the buffer size of the output buffer to the function and checking these lengths against start and count. Another possibility would be to allocate the output buffer dynamically with the right size but nevertheless the input buffer should be checked.

The condition of your for loop is wrong. It should be i < start + count, so that your for loop have to be:

for (i = start; i < start + count; i++) { ... } 

to count in your case e.g. 3, 4, 5, 6 until the end (6 will not be executed) is reached, also note the < is used and not the <=.
Output will be: "act" in your case.

You can substitute your for loop where you copy every single character by strncpy which is described at this manpage (synoptics: char *strncpy(char *dest, const char *src, size_t n);).

It would be like this:

strncpy(result, str + start, count); result[start + count] = '\0'; 

Further you should think of making your function robust against buffer overflows of the input (str) and output (result) string by using strlen on the input buffer and passing the buffer size of the output buffer to the function and checking these lengths against start and count. Another possibility would be to allocate the output buffer dynamically with the right size but nevertheless the input buffer should be checked.

Extend answer.
Source Link
Andre Kampling
  • 5.6k
  • 2
  • 23
  • 48

Your for loop musthave to be:

for (i = start; i < start + count; i++) { ... } 

to count in youyour case e.g. 3, 4, 5, 6 until the end (6) is reached, also note the < is used and not the <=.
Output will be: act"act" in your case.

You can substitute your for loop where you copy every single character by strncpy which is described at this manpage (synoptics: char *strncpy(char *dest, const char *src, size_t n);).

It would be like this:

strncpy(result, str + start, count); result[start + count] = '\0'; 

Further you should think of making your function robust against buffer overflows of the input (str) and output (result) string by using strlen on the input buffer and passing the buffer size of the output buffer to the function or allocating itand checking these lengths against start and count. Another possibility would be to allocate the output buffer dynamically with the right size but nevertheless the input buffer should be checked.

Your loop must be:

for(i = start; i < start + count; i++) { ... } 

to count in you case e.g. 3, 4, 5, 6 until the end (6) is reached, also note the < is used and not the <=.
Output will be: act in your case.

Further you should think of making your function robust against buffer overflows by passing the buffer size to the function or allocating it dynamically.

Your for loop have to be:

for (i = start; i < start + count; i++) { ... } 

to count in your case e.g. 3, 4, 5, 6 until the end (6) is reached, also note the < is used and not the <=.
Output will be: "act" in your case.

You can substitute your for loop where you copy every single character by strncpy which is described at this manpage (synoptics: char *strncpy(char *dest, const char *src, size_t n);).

It would be like this:

strncpy(result, str + start, count); result[start + count] = '\0'; 

Further you should think of making your function robust against buffer overflows of the input (str) and output (result) string by using strlen on the input buffer and passing the buffer size of the output buffer to the function and checking these lengths against start and count. Another possibility would be to allocate the output buffer dynamically with the right size but nevertheless the input buffer should be checked.

deleted 1 character in body
Source Link
Andre Kampling
  • 5.6k
  • 2
  • 23
  • 48

Your loop must be:

for(i = start; i < start + count; i++) { ... } 

to count in you case e.g. 3, 4, 5, 6 until the end (6) is reached, also note the < is used and not the <=.
Output will be: acteact in your case.

Further you should think of making your function robust against buffer overflows by passing the buffer size to the function or allocating it dynamically.

Your loop must be:

for(i = start; i < start + count; i++) { ... } 

to count in you case e.g. 3, 4, 5, 6 until the end (6) is reached, also note the < is used and not the <=.
Output will be: acte in your case.

Further you should think of making your function robust against buffer overflows by passing the buffer size to the function or allocating it dynamically.

Your loop must be:

for(i = start; i < start + count; i++) { ... } 

to count in you case e.g. 3, 4, 5, 6 until the end (6) is reached, also note the < is used and not the <=.
Output will be: act in your case.

Further you should think of making your function robust against buffer overflows by passing the buffer size to the function or allocating it dynamically.

added 62 characters in body
Source Link
Andre Kampling
  • 5.6k
  • 2
  • 23
  • 48
Loading
Source Link
Andre Kampling
  • 5.6k
  • 2
  • 23
  • 48
Loading