C substring, substring in C
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:
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], *pointer;
int position, length;
printf(“Input a string\n“);
gets(string);
printf(“Enter the position and length of substring\n“);
scanf(“%d%d”,&position, &length);
pointer = substring( string, position, length);
printf(“Required substring is \”%s\”\n“, pointer);
free(pointer);
return 0;
}
/*C substring function: It returns a pointer to the substring */
char *substring(char *string, int position, int length)
{
char *pointer;
int c;
pointer = malloc(length+1);
if (pointer == NULL)
{
printf(“Unable to allocate memory.\n“);
exit(1);
}
for (c = 0 ; c < length ; c++)
{
*(pointer+c) = *(string+position–1);
string++;
}
*(pointer+c) = ‘\0‘;
return pointer;
}
C program for all substrings of a string
#include <stdio.h>
#include <string.h>
#include <malloc.h>
char* substring(char*, int, int);
int main()
{
char string[100], *pointer;
int position = 1, length = 1, temp, string_length;
printf(“Enter a string\n“);
gets(string);
temp = string_length = strlen(string);
printf(“Substring of \”%s\” are\n“, string);
while (position <= string_length)
{
while (length <= temp)
{
pointer = substring(string, position, length);
printf(“%s\n“, pointer);
free(pointer);
length++;
}
temp–;
position++;
length = 1;
}
return 0;
}
/* Use substring function given in above C program*/
Substring program output: