C Programming - Chapter 4 - String
C Programming - Chapter 4 - String
PROGRAMMING 2
C/C++ LANGUAGE
strcmp(str1, str2) Compares the first string with the second string. If strings are the same it returns 0.
strcat(s1, s2) Concat s1 string with s2 string and the result is stored in the first string.
strrchr() Finds the last occurrence of the given characters in the string.
strcspn() Returns the span of the source string not containing any character of the given string.
strspn() Returns the span of the source string containing only the characters of the given string.
strpbrk() Finds the first occurrence of any of the characters of the given string in the source string.
strtok() Split the given string into tokens based on some character as a delimiter.
4.5 STRING FUNCTION
// string function
#include <stdio.h>
#include <string.h>
int main()
{
char str[50];
printf("Nhap chuoi: ");
fgets(str, 50, stdin);
int n = strlen(str);
strupr(str);
printf("\nUppercase string: %s " ,str);
return 0;
}
4 . 6 PAT T E R N S E A R C H I N G
• Pattern Searching Problem:
• Pattern matching in C− We have to find if a string is present in another string, as an example, the
string "algorithm” is present within the string "naive algorithm". If it is found, then its location (i.e.
position it is present at) is displayed. They are very useful when performing a search in a database.
• Features of Pattern Searching Algorithm:
■ Pattern searching algorithms should recognize familiar patterns quickly and accurately.
■ Recognize and classify unfamiliar patterns.
■ Identify patterns even when partly hidden.
■ Recognize patterns quickly with ease, and with automaticity.
4 . 6 PAT T E R N S E A R C H I N G
• Naive Pattern Searching algorithm:
• Naive pattern searching is the simplest method among other pattern-searching algorithms.
• It checks for all characters of the main string to the pattern. This algorithm is helpful for
smaller texts. It does not need any pre-processing phases. We can find the substring by checking
once for the string.
4 . 6 PAT T E R N S E A R C H I N G
• Naive Pattern Searching algorithm:
4 . 6 PAT T E R N S E A R C H I N G
• Rabin Karp algorithm:
• Rabin-Karp algorithm is an algorithm used for searching/matching patterns in the text using a hash
function. Unlike Naive string-matching algorithm, it does not travel through every character in
the initial phase rather it filters the characters that do not match and then perform the comparison.
• Rabin-Karp compares a string’s hash values, rather than the strings themselves. For efficiency, the
hash value of the next position in the text is easily computed from the hash value of the current
position.
4 . 6 PAT T E R N S E A R C H I N G
• Rabin Karp algorithm: Working of Rabin-Karp algorithm
• Initially calculate the hash value of the pattern P.
• Start iterating from the start of the string:
■ Calculate the hash value of the current substring having length m.
■ If the hash value of the current substring and the pattern are same, check if the substring is same
as the pattern.
■ If they are same, store the starting index as a valid answer. Otherwise, continue for the next
substrings.
• Return the starting indices as the required answer.
4 . 6 PAT T E R N S E A R C H I N G
• Rabin Karp algorithm:
4 . 6 PAT T E R N S E A R C H I N G
• KMP algorithm:
• KMP algorithm is used to find a “Pattern” in a “Text”.
• This algorithm compares character by character from left to right. But whenever a mismatch
occurs, it uses a preprocessed table called “Prefix Table” to skip characters comparison while
matching. Sometimes prefix table is also known as LPS Table. Here LPS stands for “Longest
proper Prefix which is also Suffix”.
4 . 6 PAT T E R N S E A R C H I N G
• KMP algorithm:
4 . 6 PAT T E R N S E A R C H I N G
• KMP algorithm:
4 . 6 PAT T E R N S E A R C H I N G
• KMP algorithm:
4 . 6 PAT T E R N S E A R C H I N G
• KMP algorithm:
4 . 6 PAT T E R N S E A R C H I N G
• KMP algorithm:
4 . 6 PAT T E R N S E A R C H I N G
• KMP algorithm:
4 . 6 PAT T E R N S E A R C H I N G
• KMP algorithm:
Thank you!