In this article, we will learn about How to Get Substring in C. We will learn multiple techniques and programs to get Substring in C language.
What is substring?
A substring is a string that is part of another string. For example, substrings of string”she” are “” (empty string), “s”, “sh”, “she”, “h”, “he” and “e.”
As you can see a simple string of 3 characters can have so many substrings in it.By calculation , A string of length n has [n*(n+1)/2 +1] substrings. They are unique if all the characters in the string are different.
1.strncpy() Function to Get Substring in C
The strncpy() function is part of the C string library. This function is defined in the <string.h> header file.This function copies the given number of bytes from the source string to the destination.
Syntax
char* strncpy(const char* destination,const char *source)
strncpy() takes three parameters – destination char*, source pointer, and the integer to denote the number of bytes to copy. If the number of bytes specified is more than the source string contains, then additional null bytes are stored at the destination.
2.Get Substring in C entered by user
In our code example below, we are taking a string as input from the user, and then we are asking users to enter the index position from which they want to start the sub-string. At the same time, we are asking users to enter the length of the substring which will be counted from the start index position to truncate the substring.
Program to Get Substring in C
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int main(){
char string[1000];
int position, length;
printf("Please enter a string\n");
gets(string);
char *str = malloc(strlen(string));
printf("Enter the position of substring\n");
scanf("%d", &position);
printf("Enter the length of substring\n");
scanf("%d", &length);
printf("%s\n", strncpy(str, string, length));
printf("%s\n", strncpy(str, string+position, length));
free(str);
return 0;
}
Output
Please enter a string
C Tutorial on Devenum
Enter the position of substring
2
Enter the length of substring
8
C Tutori
Tutorial
3. While loop to Get Substring in C
This is another example with another logic to extract a substring from a string in C. Here we are making use of the while loop to iterate through the string. We are asking the user to enter the string and the start index position of the substring. Once we get the start index from the user, we iterate to the length of the substring which is also entered by the user.
Program to get substring in C using loop
#include <stdio.h>
int main()
{
char string[1000], substr[100];
int position, length, index = 0;
printf("Please enter a string\n");
gets(string);
printf("Enter the position of substring\n");
scanf("%d", &position);
printf("Enter the length of substring\n");
scanf("%d", &length);
while (index < length) {
substr[index] = string[position+index-1];
index++;
}
substr[index] = '
#include <stdio.h>
int main()
{
char string[1000], substr[100];
int position, length, index = 0;
printf("Please enter a string\n");
gets(string);
printf("Enter the position of substring\n");
scanf("%d", &position);
printf("Enter the length of substring\n");
scanf("%d", &length);
while (index < length) {
substr[index] = string[position+index-1];
index++;
}
substr[index] = '\0';
printf("Required substring is \"%s\"\n", substr);
return 0;
}
';
printf("Required substring is \"%s\"\n", substr);
return 0;
}
Output
Please enter a string
C Tutorial on Devenum
Enter the position of substring
2
Enter the length of substring
8
Required substring is " Tutoria"
Summary
In this post, we have learned How to Get Substring in C using a built-in function and while loop.