PROGRAM IN C TO EXTRACT A SUBSTRING OF A STRING :  

Substring is a set of a continuous sequence of characters within a string.

For instance: If the given string is “Smile” – then mile, ile, s, sm are some of the substrings of the given string as they have continuity in the sequence as stored in the string.

Sile / mle / ie are not a substring of the above string as they do not appear in contiguous blocks.

We shall see three approaches in order to extract a substring from a string.

 

APPROACH 1:

  • The user will be asked to enter the string – ‘str’, the starting position from where the substring is to be extracted – ‘start’, the length of the substring to be extracted – ‘l’.
  • Then we shall calculate the length of the entered string.
  • Initially, we will check if the entered start position is valid or not. The index entered in our program will start from 0 according to the way our arrays are actually stored. You can alternatively ask the user to enter normally stating the first position to be position, however, while processing you would have to make position = position – 1.
  • If valid, we shall extract the required substring traversing the char array from the position mentioned until the required length is met.

 

Code:

#include <stdio.h>

int main()

{

char *str, substr[100];

int i, j, start, length, l, size = 100;



printf("\n Enter the main string: ");

str = (char*)malloc(size);

length = getline(&str, &size, stdin);



length --;



printf("\n Enter the position from which to start the substring (Index starts from 0): ");

scanf ("%d", &start) ;

printf("\n Enter the length of the substring: ") ;

scanf ("%d", &l);





if(start < 0 || start > length)

printf("The start index position entered is invalid.\n");

else

{

for(i= start, j = 0; str[i] !='\0' && l > 0; i++, j ++, l--)

{

substr[j] = str[i];

}

substr[j] = '\0';      //terminating the string



printf ("\n The substring is : ");

puts (substr);

}



return 0;

}

Output:

Case 1: When the starting index provided is valid.

 

#include <stdio.h>

int main()

{

char *str, substr[100];

int i, j, start, length, l, size = 100;



printf("\n Enter the main string: ");

str = (char*)malloc(size);

length = getline(&str, &size, stdin);



length --;



printf("\n Enter the position from which to start the substring (Index starts from 0): ");

scanf ("%d", &start) ;

printf("\n Enter the length of the substring: ") ;

scanf ("%d", &l);





if(start < 0 || start > length)

printf("The start index position entered is invalid.\n");

else

{

for(i= start, j = 0; str[i] !='\0' && l > 0; i++, j ++, l--)

{

substr[j] = str[i];

}

substr[j] = '\0';      //terminating the string



printf ("\n The substring is : ");

puts (substr);

}



return 0;

}

 

Case 2: When the starting index provided is invalid.

 

Ex 1:

Enter the main string: merry !

Enter the position from which to start the substring (Index starts from 0): 8

Enter the length of the substring: 2

The start index position entered is invalid.

 

 

Ex 2:

Enter the main string: merry !

Enter the position from which to start the substring (Index starts from 0): -1

Enter the length of the substring: 3

The start index position entered is invalid.

 

Case 3: When the starting index provided is valid, but substring length extends greater than string length.

Enter the main string: merry!

Enter the position from which to start the substring: 3

Enter the length of the substring: 5

The substring is : ry!

 

Here, since the condition we use uses && operator, hence we do not access areas of memory which are not in use, thus program running as expected.

 

APPROACH 2: Using function – user defined

  • The logic is same as above, however, we implement the same using function.
  • We have created a function called – ‘find_substring‘ in order to compute the substring.
  • Source string / Input string along with the substring array, starting position of substring and length of the input string is passed to the function as parameters to proceed with computation of substring.

 

Code:

#include <stdio.h>

void find_substring(char str[], char substr[], int start, int l)

{

int i, j;

 

for(i= start, j = 0; str[i] !='\0' && l > 0; i++, j ++, l--)

{

substr[j] = str[i];

}

substr[j] = '\0';            //terminating the string

 

printf ("\n The substring is : ");

puts (substr);

}

 

int main()

{

char *str, substr[100];

int length, l, size = 100, start;

 

printf("\n Enter the main string: ");

str = (char*)malloc(size);

length = getline(&str, &size, stdin);

 

length --;



printf("\n Enter the position from which to start the substring (Index starts from 0): ");

scanf ("%d", &start) ;

printf("\n Enter the length of the substring: ") ;

scanf ("%d", &l);





if(start < 0 || start > length)

printf("The start index position entered is invalid.\n");

else

{

find_substring(str, substr, start,l);

}

 

return 0;

}

Output:

Case 1: When the starting index provided is valid.

Enter the main string: Hello World!



Enter the position from which to start the substring (Index starts from 0): 3



Enter the length of the substring: 7



The substring is : lo Worl

 

Case 2: When the starting index provided is invalid.

Enter the main string: merry !

Enter the position from which to start the substring (Index starts from 0): 8

Enter the length of the substring: 2

The start index position entered is invalid.

 

APPROACH 3: Using pointers

  • The find_substring function is now modified to return a pointer to the required substring of the string.
  • Length of string, the source string, start position of substring is passed to the function.
  • Here we do not need a substring array to be passed separately as seen in approach 2.
  • Pointer to the substring is dynamically allocated and de-allocated the memory space as it points to the substring.

 

Code:

#include <stdio.h>

#include <stdlib.h>



char *find_substring(char *string, int position, int length)

{

char *pointer;

int i;



pointer = malloc(length+1);



if (pointer == NULL)

{

printf("Unable to allocate memory.\n");    //possibility since dynamic allocation

exit(1);

}



for (i = 0 ; i < length ; i++)

{

*(pointer+i) = *(string+position-1);

string++;

}



*(pointer+i) = '\0';



return pointer;      //it returns pointer to the substring

}



int main()

{

char *str, *pointer;

int start, length, l, size = 100;



printf("\n Enter the main string: ");

str = (char*)malloc(size);

length = getline(&str, &size, stdin);



length --;



printf("\nEnter the position and length of substring (Index starts from 0): \n");

scanf("%d %d", &start, &l);



if(start < 0 || start > length)

printf("The start index position entered is invalid.\n");

else

{

pointer = find_substring( str, start, l);



printf("The substring is: %s", pointer);

}



free(pointer);



return 0;

}

Output:

Case 1: When the starting index provided is valid.

Enter the main string: Merry christmas !

Enter the position and length of substring (Index starts from 0):

6

9

The substring is: christmas

 

Case 2: When the starting index provided is invalid.

Enter the main string: hello

Enter the position and length of substring (Index starts from 0):

6

3

The start index position entered is invalid.

PROGRAM IN C TO EXTRACT ALL SUBSTRINGS OF A STRING:

  • In order to extract all possible substrings of the string, we need to traverse the array sequentially.
  • We start with the first location and display the possible substrings from that position, wherein the terminating condition would be when a substring of length equal to (length of the string – index position) would be extracted.
  • This is because substrings are sequential sets of characters of a string.

 

Code:

#include <stdio.h>

#include <string.h>



void print(char *ptr, int num)

{

int i;

for (i = 0; i < num; i++)

{

printf("%c", *(ptr + i));    //printing each alphabet of the substring under consideration

}

printf("\n");

return;

}

int main()

{

char *str, *pointer;

int i, j, length, l, size = 100;

printf("\nEnter the main string: ");

str = (char*)malloc(size);

length = getline(&str, &size, stdin);

length --;   //length of string

/* finding the possible substrings */

printf("\nPossible Substrings of entered string:\n");

for(i=0; str[i]!='\0'; i++)

{

/* printing possible substrings */

for (j = 1; j <= (length - i); j++)

{

/* prints j characters from original string str[i] */

print((str + i), j);

}

}

return 0;

}

Output:

Enter the main string: Blue

Possible Substrings of entered string:

B

Bl

Blu

Blue

l

lu

lue

u

ue

e

 

Report Error/ Suggestion

Related Posts:

[yuzo_views]











CopyRight © 2019

CopyRight © 2019