Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


C Programming Code Examples

C > Strings Code Examples

A demonstration of seeking and finding

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* A demonstration of seeking and finding */ #include <stdio.h> #include <string.h> void main() { char string1[] = "This string contains the key."; char string2[] = "the key"; char string3[] = "the keys"; if(strstr(string1, string2) == NULL) printf("\nString not found."); else printf("\nString: %s\n was found in string: %s",string2, string1); if(strstr(string1, string3) == NULL) printf("\nString not found."); else printf("\nWe shouldn't get to here!"); }

The if-else statement is used to perform two operations for a single condition. The if-else statement is an extension to the if statement using which, we can perform two different operations, i.e., one is for the correctness of that condition, and the other is for the incorrectness of the condition. Here, we must notice that if and else block cannot be executed simiulteneously. Using if-else statement is always preferable since it always invokes an otherwise case with every if condition.

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,

Locate substring. Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1. The matching process does not include the terminating null-characters, but it stops there. The function returns pointer to the first occurrence of the matched string in the given string. It is used to return substring from first match till the last character. The strstr() function searches the string str1 for the first occurrence of the string str2 (not counting str2's terminating null character). The return value is a pointer to the first character in the first occurrence in str1 of the sequence contained in str2, or a null pointer if there is no such occurrence. If str2 points to an empty string, then strstr() returns the value of its first argument, str1.

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.

#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:








C Program to print odd numbers from 1 to n without if statement. The above approach is not optimal approach to print odd numbers. Observe the above program for a while. You


Program code count number of words, digits, vowels using pointers. Program code Finding number of words, blank spaces, digits, special symbols, vowels using pointers. Counting the

C Program example Return the largest of the two passed numbers. Return smallest of the two passed numbers. Load some random values into the array. Print set of numbers.