fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. void print_substring (char const * from, char const * to) {
  5. fwrite(from, 1, to - from, stdout);
  6. // One could check return value to ensure
  7. // that all characters have been written,
  8. // but then you also need to decide what
  9. // to do in the case of an error.
  10. }
  11.  
  12.  
  13. void print_all_substrings(char const * string) {
  14. size_t const string_length = strlen(string);
  15. for (size_t substring_length = 1;
  16. substring_length <= string_length;
  17. ++substring_length) {
  18. char const * const last_begin = string + (string_length - substring_length);
  19. char const * begin = string;
  20. for (; begin != last_begin; ++begin) {
  21. print_substring(begin, begin + substring_length);
  22. printf(", ");
  23. }
  24. print_substring(last_begin, last_begin + substring_length);
  25. printf("\n");
  26. }
  27. }
  28.  
  29.  
  30. int main () {
  31. print_all_substrings ("abcdefg"); puts("--");
  32. print_all_substrings("a");puts("--");
  33. print_all_substrings("");puts("--");
  34. }
Success #stdin #stdout 0s 9432KB
stdin
Standard input is empty
stdout
a, b, c, d, e, f, g
ab, bc, cd, de, ef, fg
abc, bcd, cde, def, efg
abcd, bcde, cdef, defg
abcde, bcdef, cdefg
abcdef, bcdefg
abcdefg
--
a
--
--