Find Substring in C

Write a program to find the substring in c. Use the C substring program to find a substring of a string and all of its substrings. A string that is a substring of another string is referred to as a substring. Substrings of the string “the” include, for example, “h,” “he,” and “e.” In the header file “string.h,” there is no library function for finding a substring directly.

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

 

Output:

Enter the element
c programming
Enter the position and length
Substring is ” “

Program to find the substring in c using array

An Array comprising a fixed number of String values is known as a String Array. A succession of characters is referred to as a String. In general, a string is an immutable object, meaning that its value cannot be modified. The String Array works in the same way as other Array data types.

What is while loop in c program

A “While” Loop is used to repeat a block of code indefinitely until it meets a condition. A while construct is made up of a code block and a condition/expression. This process is repeated until the condition/expression is no longer true. The control structure is sometimes known as a pre-test loop since the while loop examines the condition/expression before executing the block.

Iteration statements cause statements to be executed zero or more times, depending on whether or not the loop is terminated. Except when the break or continue statements are encountered, these statements are performed in order when they are compound statements.

What is \0 in c program

\0 stands for zero. It is mostly used in C to signify the end of a character string. Of course, it’s a regular character and can be used as such, although that’s not the case very often. The simpler versions of C’s built-in string manipulation methods expect a null-terminated string.