You are here

C substring, substring in C

Substring

C substring program to find substring of a string and its all substrings. A substring is itself a string that is part of a longer string. For example, substrings of string "the" are "" (empty string), "t", "th", "the", "h", "he" and "e." The header file "string.h" does not contain any library function to find a substring directly.

C substring program

#include <stdio.h>
 
int main()
{
   char string[1000], sub[1000];
   int position, length, c = 0;
 
   printf("Input a string\n");
   gets(string);
 
   printf("Enter the position and length of substring\n");
   scanf("%d%d", &position, &length);
 
   while (c < length) {
      sub[c] = string[position+c-1];
      c++;
   }
   sub[c] = '\0';
 
   printf("Required substring is \"%s\"\n", sub); // '\"' to print "
 
   return 0;
}

C substring program output:
C substring program output

Substring in C language using function

We create a function and pass it four arguments original string array, substring array, position, and length of the required substring. As we use call by reference, we do not need to return the substring array. See another code below in which we return a pointer to substring, which we create in our function using dynamic memory allocation.

#include <stdio.h>
 
void substring(char [], char[], int, int);
 
int main()
{
   char string[1000], sub[1000];
   int position, length, c = 0;
 
   printf("Input a string\n");
   gets(string);
 
   printf("Enter the position and length of substring\n");
   scanf("%d%d", &position, &length);
 
   substring(string, sub, position, length);
 
   printf("Required substring is \"%s\"\n", sub);
 
   return 0;
}
// C substring function definition
void substring(char s[], char sub[], int p, int l) {
   int c = 0;
   
   while (c < l) {
      sub[c] = s[p+c-1];
      c++;
   }
   sub[c] = '\0';
}

C substring program using pointers

To find substring we create a substring function which returns a pointer to string. String address, required length of substring and position from where to extract substring are the three arguments passed to function.

#include <stdio.h>
#include <stdlib.h>

char* substring(char*, int, int);

int main()
{
   char string[100], *p;
   int position, length;
 
   printf("Input a string\n");
   gets(string);
 
   printf("Enter the position and length of substring\n");
   scanf("%d%d", &position, &length);
 
   p = substring(string, position, length);
 
   printf("Required substring is \"%s\"\n", p);
 
   free(p);
 
   return 0;
}

/*C substring function: It returns a pointer to the substring */

char *substring(char *string, int position, int length)
{
   char *p;
   int c;
 
   p = malloc(length+1);
   
   if (p == NULL)
   {
      printf("Unable to allocate memory.\n");
      exit(1);
   }
 
   for (c = 0; c < length; c++)
   {
      *(p+c) = *(string+position-1);      
      string++;  
   }
 
   *(p+c) = '\0';
 
   return p;
}

C program to find all substrings of a string

#include <stdio.h>
#include <string.h>
#include <malloc.h>
 
char* substring(char*, int, int);
 
int main()
{
   char string[100], *p;
   int position = 1, length = 1, t, string_length;
 
   printf("Enter a string\n");
   gets(string);
 
   t = string_length = strlen(string);
 
   printf("Substring of \"%s\" are\n", string);
 
   while (position <= string_length)
   {
      while (length <= t)
      {
         p = substring(string, position, length);
         printf("%s\n", p);
         free(p);
         length++;
      }
      t--;
      position++;
      length = 1;
   }
 
   return 0;
}

/* Use substring function given in above C program*/

Substring program output:
All Substrings of a string C program output

Number of substrings of a string

A string of length n has [n*(n+1)/2 +1] substrings. They are unique, if all the characters in the string are different.

Consider the string: a1a2a3..............an-1an

Number of substrings of length n: 1 [(a1a2a3..............an-1an)]

Number of substrings of length (n-1): 2 [(a1a2a3..............an-1), (a2a3..............an-1an)]

Number of substrings of length (n-2): 3 [(a1a2a3..............an-2), (a2a3..............an-1), (a3a3..............an)]
......
......
......
Number of substrings of length 3: (n-3) [(a1a2a3), (a2a3a4), (a3a4a5),................,(an-2an-1an)]

Number of substrings of length 2: (n-2) [(a1a2), (a2a3), (a3a4),...........,(an-1an)]

Number of substrings of length 1: (n) [(a1), (a2), (a3),...........,(an-1), (an)]

Number of substrings of length 0 (empty string): 1 []