Skip to main content
Added note on pointers
Source Link
William Morris
  • 9.4k
  • 19
  • 43

I have a few issues with in your code:

  • Consistency in naming - use either camelCase or not_camel_case but don't mix

  • consistency in braces. The opening brace for a function goes in the first column.

  • word_start_index and word_end_index should take a const parameter

  • word_start_index is the same as strspn(string, " "); or if you are also looking for punctuation, strspn(string, " \t\n.,;:");

  • word_end_index - as for word_start_index but use strcspn (note the 'c')

  • word_end_index as a function (ie not in your context) fails for an empty string or a string starting with a space (it returns the char before the string starts).

  • variable test in main() is misnamed. I would prefer something that shows it is a string.

  • the test while (test != '\0') in main() is wrong - should be *test != '\0'. Your loop always exits from the break

  • no return or parameters in main()

Also, arguably the position of the stars in your pointers is wrong. I prefer char* p to be written char *p, which makes it clear that it is p that takes the star. Consider code such as char* a, b;. This is bad because it gives the impression that b is a pointer.

I have a few issues with in your code:

  • Consistency in naming - use either camelCase or not_camel_case but don't mix

  • consistency in braces. The opening brace for a function goes in the first column.

  • word_start_index and word_end_index should take a const parameter

  • word_start_index is the same as strspn(string, " "); or if you are also looking for punctuation, strspn(string, " \t\n.,;:");

  • word_end_index - as for word_start_index but use strcspn (note the 'c')

  • word_end_index as a function (ie not in your context) fails for an empty string or a string starting with a space (it returns the char before the string starts).

  • variable test in main() is misnamed. I would prefer something that shows it is a string.

  • the test while (test != '\0') in main() is wrong - should be *test != '\0'. Your loop always exits from the break

  • no return or parameters in main()

I have a few issues with in your code:

  • Consistency in naming - use either camelCase or not_camel_case but don't mix

  • consistency in braces. The opening brace for a function goes in the first column.

  • word_start_index and word_end_index should take a const parameter

  • word_start_index is the same as strspn(string, " "); or if you are also looking for punctuation, strspn(string, " \t\n.,;:");

  • word_end_index - as for word_start_index but use strcspn (note the 'c')

  • word_end_index as a function (ie not in your context) fails for an empty string or a string starting with a space (it returns the char before the string starts).

  • variable test in main() is misnamed. I would prefer something that shows it is a string.

  • the test while (test != '\0') in main() is wrong - should be *test != '\0'. Your loop always exits from the break

  • no return or parameters in main()

Also, arguably the position of the stars in your pointers is wrong. I prefer char* p to be written char *p, which makes it clear that it is p that takes the star. Consider code such as char* a, b;. This is bad because it gives the impression that b is a pointer.

Source Link
William Morris
  • 9.4k
  • 19
  • 43

I have a few issues with in your code:

  • Consistency in naming - use either camelCase or not_camel_case but don't mix

  • consistency in braces. The opening brace for a function goes in the first column.

  • word_start_index and word_end_index should take a const parameter

  • word_start_index is the same as strspn(string, " "); or if you are also looking for punctuation, strspn(string, " \t\n.,;:");

  • word_end_index - as for word_start_index but use strcspn (note the 'c')

  • word_end_index as a function (ie not in your context) fails for an empty string or a string starting with a space (it returns the char before the string starts).

  • variable test in main() is misnamed. I would prefer something that shows it is a string.

  • the test while (test != '\0') in main() is wrong - should be *test != '\0'. Your loop always exits from the break

  • no return or parameters in main()