From f4a0df5529a1e6b3c2acdc37c19fb9092b0303a3 Mon Sep 17 00:00:00 2001 From: Sonika Date: Sat, 21 Oct 2023 16:12:55 +0530 Subject: [PATCH] added substring.c --- README.md | 1 + substring.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 substring.c diff --git a/README.md b/README.md index 341baa9..e13cc3d 100644 --- a/README.md +++ b/README.md @@ -105,6 +105,7 @@ These program are written in codeblocks ide for windows. These programs are not - [To check if a matrix is a sparse matrix or not](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/SparseMatrix_017.c) - [To calculate the Least Common Multiple](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/lcm.c) - [Lambda in C](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/lambda_in_c.c) +- [All substrings of a string](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/substring.c) # Contributing This is a personal learning project for me. diff --git a/substring.c b/substring.c new file mode 100644 index 0000000..10e8aa0 --- /dev/null +++ b/substring.c @@ -0,0 +1,31 @@ +// C program to print all possible substrings of a given string +#include +#include +// Function to print all sub strings +void subString(char str[], int n) +{ + // Pick starting point + for (int len = 1; len <= n; len++) + { + // Pick ending point + for (int i = 0; i <= n - len; i++) + { + // Print characters from current + // starting point to current ending + // point. + int j = i + len - 1; + for (int k = i; k <= j; k++) + printf("%c",str[k]); + + printf("\n"); + } + } +} + +// Driver program to test above function +int main() +{ + char str[] = "abcdef"; + subString(str, strlen(str)); + return 0; +}