C Programming Code Examples
C > Strings Code Examples
Program to Implement the KMP Pattern Searching Algorithm
/* Program to Implement the KMP Pattern Searching Algorithm
This C Program performs a pattern searching using KMP Pattern Searching algorithm. Pattern searching is used for finding the existence of a substring in a string. The pattern searching in the following has been made case-insensitive. */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
char string[88], matchcase[20], c;
int x = 0, j = 0, index;
/*Scanning string*/
printf("Enter string: ");
do
{
fflush(stdin);
c = getchar();
string[x++] = tolower(c);
} while (c != '\n');
string[x - 1] = '\0';
/*Scanning substring*/
printf("Enter substring: ");
x = 0;
do
{
fflush(stdin);
c = getchar();
matchcase[x++] = tolower(c);
} while (c != '\n');
matchcase[x - 1] = '\0';
for (x = 0; x < strlen(string) - strlen(matchcase) + 1; x++)
{
index = x;
if (string[x] == matchcase[j])
{
do
{
x++;
j++;
} while(j != strlen(matchcase) && string[x] == matchcase[j]);
if (j == strlen(matchcase))
{
printf("Match found from position %d to %d.\n", index + 1, x);
return 0;
}
else
{
x = index + 1;
j = 0;
}
}
}
printf("No substring match found in the string.\n");
return 0;
}
An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false. Logical operators are commonly used in decision making in C programming. These operators are used to perform logical operations and used with conditional statements like C if-else statements.
#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program. Here are the two types of file that can be included using #include:
Get string length. Returns the length of the C string str. The length of a C string is determined by the terminating null-character: A C string is as long as the number of characters between the beginning of the string and the terminating null character (without including the terminating null character itself).
The for loop is used in the case where we need to execute some part of the code until the given condition is satisfied. The for loop is also called as a per-tested loop. It is better to use for loop if the number of iteration is known in advance. The for-loop statement is a very specialized while loop, which increases the readability of a program. It is frequently used to traverse the data structures like the array and linked list.
Convert uppercase letter to lowercase. Converts c to its lowercase equivalent if c is an uppercase letter and has a lowercase equivalent. If no such conversion is possible, the value returned is c unchanged. Notice that what is considered a letter may depend on the locale being used; In the default "C" locale, an uppercase letter is any of: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z, which translate respectively to: a b c d e f g h i j k l m n o p q r s t u v w x y z.
Flush stream. If the given stream was open for writing (or if it was open for updating and the last i/o operation was an output operation) any unwritten data in its output buffer is written to the file. If stream is a null pointer, all such streams are flushed. In all other cases, the behavior depends on the specific library implementation. In some implementations, flushing a stream open for reading causes its input buffer to be cleared (but this is not portable expected behavior).
Writes the C string pointed by format to the standard output (stdout). If format includes format specifiers (subsequences beginning with %), the additional arguments following format are formatted and inserted in the resulting string replacing their respective specifiers. printf format string refers to a control parameter used by a class of functions in the input/output libraries of C programming language. The string is written in a simple template language: characters are usually copied literally into the function's output, but format specifiers, which start with a % character, indicate the location and method to translate a piece of data (such as a number) to characters. "printf" is the name of one of the main C output functions, and stands for "print formatted". printf format strings are complementary to scanf format strings, which provide formatted input (parsing). In both cases these provide simple functionality and fixed format compared to more sophisticated and flexible template engines or parsers,
Get character from stdin. Returns the next character from the standard input (stdin). It is equivalent to calling getc with stdin as argument. A getchar() function is a non-standard function whose meaning is already defined in the stdin.h header file to accept a single input from the user. In other words, it is the C library function that gets a single character (unsigned char) from the stdin. However, the getchar() function is similar to the getc() function, but there is a small difference between the getchar() and getc() function of the C programming language.
While loop is also known as a pre-tested loop. In general, a while loop allows a part of the code to be executed multiple times depending upon a given boolean condition. It can be viewed as a repeating if statement. The while loop is mostly used in the case where the number of iterations is not known in advance. The while loop evaluates the test expression inside the parentheses (). If test expression is true, statements inside the body of while loop are executed. Then, test expression is evaluated again. The process goes on until test expression is evaluated to false. If test expression is false, the loop terminates.
In C, the "main" function is treated the same as every function, it has a return type (and in some cases accepts inputs via parameters). The only difference is that the main function is "called" by the operating system when the user runs the program. Thus the main function is always the first code executed when a program starts. main() function is a user defined, body of the function is defined by the programmer or we can say main() is programmer/user implemented function, whose prototype is predefined in the compiler. Hence we can say that main() in c programming is user defined as well as predefined because it's prototype is predefined. main() is a system (compiler) declared function whose defined by the user, which is invoked automatically by the operating system when program is being executed.
The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities. The if...else ladder allows you to check between multiple test expressions and execute different statements. In C/C++ if-else-if ladder helps user decide from among multiple options. The C/C++ if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.
C supports nesting of loops in C. Nesting of loops is the feature in C that allows the looping of statements inside another loop. Any number of loops can be defined inside another loop, i.e., there is no restriction for defining any number of loops. The nesting level can be defined at n times. You can define any type of loop inside another loop; for example, you can define 'while' loop inside a 'for' loop.
Input week day number from user. Store it in a variable say week. Print Monday if week=1. I have assumed Monday as first day of week. And check condition for all 7 days & print the
C programming code example for Number Conversions: Binary to decimal, decimal to binary, decimal to octal, decimal to hex, octal to decimal, octal to binary, binary to octal,
C Program reverse counting is sequence of whole numbers in descending order without zero. Developing a program of counting in C programming language is easy and we shall
Picks a random value withing range low-high. Value returned seeds the rand() function. Print version information and exit. First seed the random function. Add line, to the array...
The continue statement is used inside loops. When a continue statement is encountered inside a loop, control jumps to the beginning of the loop for next iteration, skipping the...
This C Program to calculates the volume and surface area of cuboids. The Formula used in this program are surfacerea= 2(w * l + l * h + h * w) where w is Width & l is Length and h is
C Get ngrams for first word. Get ngrams for second word. Compare two arrays, count duplicates. Calculate score. Clean up. Return array of ngrams. Padd word according to one