How to write a C Program to Finds Sub-String Search in String in C Programming Language ?
This C Program to Finds sub-string search in string s.
Returns -1 if sub-string was not found
Returns 0 if search was empty
Returns starting index of sub-string if successful
Solution:
- int find(string s, string search)
- {
- if(length(search) == 0)
- {
- return 0;
- }
- int s_index = 0; //Starting index
- int c_index = 0; //Checking index
- int b_table[length(search)]; //Backtracking table
- fill_backtrack_table(*b_table, search);
- while(length(s) >= (s_index + c_index))
- {
- if(s.src[s_index + c_index] == search.src[c_index])
- {
- if(c_index == length(search) - 1)
- {
- return s_index;
- }
- else
- {
- c_index++;
- }
- }
- else
- {
- if(b_table[c_index] == -1)
- {
- s_index++;
- c_index = 0;
- }
- else
- {
- s_index += c_index - b_table[c_index];
- c_index = b_table[c_index];
- }
- }
- }*/
- return -1;
- }
- // Fills the backtrack table for function find
- void fill_backtrack_table(int* b_table, string search)
- {
- b_table[0] = -1;
- if(length(search) == 1)
- {
- return;
- }
- b_table[1] = 0;
- int t_index = 2; //Table index
- int s_index = 0; //Search index
- while (t_index < length(search))
- {
- if(search.src[t_index - 1] == search.src[s_index])
- {
- b_table[t_index] = s_index + 1;
- s_index++;
- t_index++;
- }
- else if(s_index > 0)
- {
- s_index = b_table[s_index];
- }
- else
- {
- b_table[t_index] = 0;
- t_index++;
- }
- }
- return;
- }
Learn More :
String
- Write a c program to check given string is palindrome number or not.
- C Program to Convert Text String to Binary
- C Program String - Alphabet Count Example
- C Program String Example
- C Program String Count Example
- C Program String Example
- C Program to Print a String Without Use Semicolon.
- C Program To Find Length Of A String Including Blank Spaces, Tabs, And Other Special Characters
- C Program To Find Length Of String And Concatenate Two Strings
- C Program To Find The Length, Reverse Of A String And To Check It Is Palindrome or Not
- C Program To Print String In Formatted Output Form
- C Program To Reverse The String Using Pointer
- C Program Concatenating Two Strings Into A Third System
- C Program Date from string DDDD-DD-DD
- BUILDS A STRING FROM THE LSB BITS OF EACH PIXEL
- C Program to concatenate two strings without using string functions
- C Program to Count Vowels, Consonants and Spaces in a string
- C Program to convert a string to upper case
- C Program Performs a search and replace for a specified target and replacement string
- C Program Anagrams
- C Program to find string length without library function
- C Program to Check Given String is Palindrome or Not
- C Program to Converts a Number to a String.
- Design a C function that shortens a string to 8 characters
Return
Find
- Find out the perfect number using c program
- Find g.c.d of two number using c program.
- Write a c program to find out NCR factor of given number
- How to Write a C program to find maximum between two numbers ?
- Write a C program to find maximum between three numbers ?
- C or C++ program to read marks of 4 subjects and find how many students are pass and fail with division
- C Program turnLEDOn to Drives Forward Until It Finds The Line
- Write overloaded function templates for finding the roots of the linear (a * x + b = 0) and square (a * x2 + b * x + c = 0) uravneniy.Zamechanie: in function to send coefficients of the equations.
- C Program to Find Random Number
- C Program To Find LCM and HCF Of Two Number Using Function - 2
- C Program to find LCM and HCF Of Two Number Using Recursion - 3
- C Program To Find LCM and HCF Of Two Number -1
- C Program To Find Length Of A String Including Blank Spaces, Tabs, And Other Special Characters
- C Program To Find Length Of String And Concatenate Two Strings
- C Program To Find Product Of Two No Using MACRO
- C Program To Find Reverse Of Any Digit Number
- C Program To Find Sum and Difference Of Two Matrices
- C Program To Find The Frequency Of A Number
- C Program To Find The Length, Reverse Of A String And To Check It Is Palindrome or Not
- C Program To Find The Maximum And Minimum Value In An Array
- C Program To Find Transpose Of A Matrix
- C Program To Find Union & Intersection Of Two Array
- C Program Finding the sum of Squares using Recursion
- C Program to Find NCR Of A Given Number Program
- C Program To Find Maximum Of Given Numbers