C Programming Code Examples
C > Strings Code Examples
Print Combination of two Words of two given Strings without any Repetition
/* Print Combination of two Words of two given Strings without any Repetition */
#include <stdio.h>
#include <string.h>
void main()
{
char string[100], str[10], c[10];
int z, occ = 0, x = 0, j = 0, count = 0, len = 0;
printf("Enter a string:");
scanf("%[^\n]s", string);
printf("Enter the word to check its occurence:");
scanf("%s", str);
len = strlen(str);
for (x = 0;string[x] != '\0';x++)
{
count = 0;
for (j = 0, z = x;j < len; j++, z++)
{
c[j] = string[z];
if (c[j] == str[j])
{
count++; /* Incrementing the count if the characters of the main string match with the characters of the given word */
}
}
if (count == len && string[z] == ' ')
{
occ++; /* Incrementing the occ if word matches completely and next character in string is space */
}
}
printf("The number of occ is %d\n", occ);
}
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.
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.
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.
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).
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.
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.
#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:
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,
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.
Take a string and a substring as input. Store it in the arrays 'str[] & str1[]' respectively. Using For Loop to 'compare' str1[] with the str[]. Do step2 until the end of main string. During the
C program print pascal triangle up to n rows. Pascal triangle is a triangular number pattern named after famous mathematician Pascal. Where n is row number and k is term of that
How to find maximum and minimum element in an array using recursion in C programming. Recursion works on the concept of divide and conquer. We break the problem in its smallest
Print only the 'raw' words. Read input from 'FILE', default is stdin. Start reading lines from file pointer, add all entrys to 'strarray'. Print the array of strings, jumping back each
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...
Using cricket, declare an array player with 10 elements and write C program code read the information about all the 10 players and print a team wise list containing Names of Players