Program to Search for a Substring in a String in C

Program-to-Search-for-a-Substring-in-a-String-in-C-Language

Program Description:

Write a Program to Search for a substring in a string in C programming language. The program should accept two strings from the user, one for the main string and one for the substring. The program must then look for the substring within the main string and return the result.

📢 This Program is part of the C String Practice Programs series

We are going to look at a few methods to search for a substring in a string and each program will be followed by a detailed step-by-step explanation of the program and program output. We also included the instructions to compile and run the program.

Let’s look at the excepted input and output of the program.

Expected Input and Output:

Input:

Enter a main string : learn programming at sillycodes.com

Enter the substring(String to search in main string): sillycodes

Output:

FOUND! Substring:'sillycodes' is found in mainString:'learn programming at sillycodes.com'

The sub string sillycodes is found in the main string learn programming at sillycodes.com.

Prerequisites:

To better understand the program, please go through the following articles, where we discussed about the strings and functions in c language.

We will look at three methods to search for the substring in the main string. They are

  • Manual Approach – Where we use an iterative method to search for the sub-string in the main string
  • User-defined function – we use a custom user-defined to search for a string in the main string.
  • Library function – We use the strstr() library function to search for the target string in the main string.

Let’s look at the step-by-step instructions for the iterative program.

Algorithm to Search for a Substring in a String (Manual Method):

  1. Create a Macro( SIZE) to store the max size of the strings. We can also use a global constant.
  2. Start the main() function by declaring the two strings and name them as mainStr and subStr. The size of both strings is equal to SIZE macro.
  3. Take two strings from the user and store them in mainStr and subStr respectively. We use the gets function to read the strings.
  4. To search for the substring( subStr) in the main string( mainStr), Iterate through the main string( mainStr), looking for the first character of the substring( subStr[0]). If we found the first character of the substring, we should then determine whether all other characters of the substring match. If that’s the case, we found the substring( subStr) in the main string( mainStr).
  5. Create a For loop by initializing variable i with 0 to iterate over the mainStr. It will continue looping as long as the current character ( mainStr[i]) is not a NULL character ( '\0').
    • At each iteration, Check if the current character( mainStr[i]) is equal to first character of substring( subStr[0]) - i.e if(mainStr[i] == subStr[0])
    • If the above condition is true, Then search for all characters of the subStr in the mainStr. We need to use another for loop to iterate over the contents of the subStr – i.e for(j=0; subStr[j]; j++).
      • Check if the mainStr[i] is not matching to subStr[j], If so then the subString is not found. Otherwise, continue to check all elements of the subStr.
    • Once the above loop is completed, Check if we iterated complete subStr ( use j == strlen(substr) ). If we iterated the complete subStr, then we found the match. Display the message saying FOUND on the console. Terminate the program.
  6. If step 5 is completed and the program is not exited, Then it means the substring subStr is not found in the main string mainStr. Display the NOT FOUND message on the console and stop the program.

Search for a Substring in a String in C Language – Manual Method:

Here is the program to search for a substring in a main string in c programming language.

As we are using the strlen() library function, We need to include the string.h header file.

Program Output:

Compile the program using GCC compiler (Any of your favorite compilers)

$gcc search-string-iterative.c

The above command generates an executable file named a.out. Run the executable file.

As we can see from the above output, The substring sillycodes.com is found in the main string.

If we look at the step-by-step execution of the program.

The program looks for the first character of the substring( subStr[0]) in the main string. In the above example, The first character subStr[0] is found at index 21 of the main string.

mainStr[21]=s, subStr[0]=s

Then we will try to match all characters of the substring with the main string. Here is how the flow looks like

mainStr[22]=i, subStr[1]=i

mainStr[23]=l, subStr[2]=l

mainStr[24]=l, subStr[3]=l

mainStr[25]=y, subStr[4]=y

mainStr[26]=c, subStr[5]=c

mainStr[27]=o, subStr[6]=o

mainStr[28]=d, subStr[7]=d

mainStr[29]=e, subStr[8]=e

mainStr[30]=s, subStr[9]=s

Once all characters are matched, The inner for loop will be completed, Then we are checking the length of the j with the subStr length.

If we look at the j and sub string Length.

i:31, j:10, length:10

Both are the same. So we found the sub-string sillycodes.com in the main string learn programming at sillycodes.com

FOUND! Substring:’sillycodes’ is found in mainString:’learn programming at sillycodes.com’

Program to Search for a substring in main string using a user-defined function:

In the above program, We have written the complete program inside the main function. Let’s divide the above program and convert it to use a function to search for the substring in the main string.

By using the functions we can define a function once and can call it as many times as we want. Functions also increase the readability of the program and make it easier to debug issues. Here are the benefits using the functions in programming.

Here is the rewritten version of the above program, Where we used a custom function to search for a substring in the main string.

In the above program, We have created a function called searchString to search for the target string the main string. THe Prototype of the function is

As we can see from the prototype, The searchString function takes two character pointers( char *) that point to the main string( mainStr) and substring( subStr) respectively.

It also returns an integer to the caller. It returns One(1) if the subStr is found in the mainStr, Otherwise, it returns Zero(0).

Finally, call the searchString() function from the main() function with the main string and substring as the parameters.

Store the return value in isFoundvariable and display the results based on the isFound variable.

Program Output:

Let’s compile and Run the program.

Test 1: Positive test case

Program-to-Search-for-substring-in-main-string-using-user-defined-function

In the above example, The sub-string is found in the main string.

Test 2: Negative test case

The substring online is not found in the main string Learn Programming.

Search for a substring in a string using strstr function in C:

The C Programming language also provides a library function called strstr to search for a string in a string. Let’s use the strstr library function to search for the string in the main string.

▶️ We have covered the strstr function in detail in the following article

Program Output:

Compile and Run the program.

search-for-substring-in-a-string-in-c-program-output

Let’s look at another example.

As we can see from the above output, The program is properly searching and finding the target string in the main string.

Related Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

1 Response

  1. […] C Program to Search for substring in a String […]

Leave a Reply