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

C Program to Accepts two Strings & Compare them

/* C Program to Accepts two Strings & Compare them - Take two strings as input and store them in the arrays string1[] and string2[] respectively. - Count the number of characters in both the arrays and store the result in the variables count1 and count2. - Compare each character of the strings. If both the strings are equal then assign variable flag to zero or if string1 is greater than string2 then assign 1 to variable flag and break or if string1 is lesser than string2 then assign -1 to variable flag and break. - Print the output according to value of variable flag. */ #include <stdio.h> void main() { int count1 = 0, count2 = 0, flag = 0, j; char string1[18], string2[18]; printf("Enter a string:"); gets(string1); printf("Enter another string:"); gets(string2); /* Count the number of characters in string1 */ while (string1[count1] != '\0') count1++; /* Count the number of characters in string2 */ while (string2[count2] != '\0') count2++; j = 0; while ((j < count1) && (j < count2)) { if (string1[j] == string2[j]) { j++; continue; } if (string1[j] < string2[j]) { flag = -1; break; } if (string1[j] > string2[j]) { flag = 1; break; } } if (flag == 0) printf("Both strings are equal \n"); if (flag == 1) printf("String1 is greater than string2 \n", string1, string2); if (flag == -1) printf("String1 is less than string2 \n", string1, string2); }

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,

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.

Get string from stdin. Reads characters from the standard input (stdin) and stores them as a C string into str until a newline character or the end-of-file is reached. The newline character, if found, is not copied into str. A terminating null character is automatically appended after the characters copied to str. Notice that gets is quite different from fgets: not only gets uses stdin as source, but it does not include the ending newline character in the resulting string and does not allow to specify a maximum size for str (which can lead to buffer overflows).

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

An array is defined as the collection of similar type of data items stored at contiguous memory locations. Arrays are the derived data type in C programming language which can store the primitive type of data such as int, char, double, float, etc. It also has the capability to store the collection of derived data types, such as pointers, structure, etc. The array is the simplest data structure where each data element can be randomly accessed by using its index number.

The break is a keyword in C which is used to bring the program control out of the loop. The break statement is used inside loops or switch statement. The break statement breaks the loop one by one, i.e., in the case of nested loops, it breaks the inner loop first and then proceeds to outer loops.

The continue statement in C programming works somewhat like the break statement. Instead of forcing termination, it forces the next iteration of the loop to take place, skipping any code in between. For the for loop, continue statement causes the conditional test and increment portions of the loop to execute. For the while and do...while loops, continue statement causes the program control to pass to the conditional tests.

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.


C Program calculates the GCD and LCM of 2 integers. Here gcd means greatest common divisor. For two integers a and b, if there are any numbers d so that a / d and b / d doesn't

Initialize the array named array[] with the random values. Call function minabsvaluepair and pass array and its size as parameters. In the function definition receive parameters...


This program takes a string as input and finds its Length without using the built-in function. Take a string as input and store it in the array 'string[]'. Using for loop count the number of

Program reverse the string 'Using Recursion'. C program uses recursive function & reverses the string in the same 'Memory Location'. Eg 'Superman8' will be reversed to '8namrepuS'



C program print all natural numbers from 1 to n using loop. Input upper limit to print natural number from user. Store it in a variable say N. Run a For Loop from 1 to N with 1 increment.