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

Accept 2 String & check whether all Characters in first String is Present in second String & Print

/* Accept 2 String & check whether all Characters in first String is Present in second String & Print */ #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> #define CHAR_SIZE 26 void alphacheck(char *, int []); void create(char *, int[]); int main() { char str1[50], str2[50]; int a1[CHAR_SIZE] = {0}, a2[CHAR_SIZE] = {0}, x; char str1_alpha[CHAR_SIZE], str2_alpha[CHAR_SIZE]; printf("Enter string1: "); scanf("%s", str1); printf("Enter string2: "); scanf("%s", str2); alphacheck(str1, a1); alphacheck(str2, a2); create(str1_alpha, a1); create(str2_alpha, a2); if (strcmp(str1_alpha, str2_alpha) == 0) { printf("All characters match in %s and %s.\n", str1, str2); printf("The characters that match are: "); for (x = 0; x < strlen(str1_alpha); x++) { printf("%c, ", str1_alpha[x]); } printf("\n"); } else { printf("All characters do not match in %s and %s.\n", str1, str2); } return 0; } void alphacheck(char *str, int a[]) { int x, index; for (x = 0; x < strlen(str); x++) { str[x] = tolower(str[x]); index = str[x] - 'a'; if (!a[index]) { a[index] = 1; } } } void create(char *str, int a[]) { int x, j = 0; for (x = 0; x < CHAR_SIZE; x++) { if (a[x]) { str[j++] = x + 'a'; } } str[j] = '\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.

Compare two strings. Compares the C string str1 to the C string str2. This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminating null-character is reached. This function performs a binary comparison of the characters. For a function that takes into account locale-specific rules, see strcoll.

In the C Programming Language, the #define directive allows the definition of macros within your source code. These macro definitions allow constant values to be declared for use throughout your code. Macro definitions are not variables and cannot be changed by your program code like variables. You generally use this syntax when creating constants that represent numbers, strings or expressions.

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

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.

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).

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.

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,

Read formatted data from stdin. Reads data from stdin and stores them according to the parameter format into the locations pointed by the additional arguments. The additional arguments should point to already allocated objects of the type specified by their corresponding format specifier within the format string. In C programming, scanf() is one of the commonly used function to take input from the user. The scanf() function reads formatted input from the standard input such as keyboards. The scanf() function enables the programmer to accept formatted inputs to the application or production code. Moreover, by using this function, the users can provide dynamic input values to the application.

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.





C Programming language code for circular queue implementation through Array. Add element to Circular Queue. Delete element from the Circular Queue. Do you want to do


We are Counting the Number of characters in a given String to find out & display its Length on console. Upon 'execution' of this program, the user would be asked to enter string, then


Adding two numbers using inline assembly language. Assembly language can be written in c. C supports assembly as well as higher language features so called "middle level...

The annual examination is conducted for 50 students for three subjects. Write a program to read the data and determine the following: Total marks obtained by each student. The...


Insert, swap, search value, search minimum and search maximum values. Destroy, count tree nodes. Insert a tnode into the binary tree. Traverse the tree to get a pointer to the