C Programming Code Examples
C > Strings Code Examples
Print Smallest and Biggest possible Word which is Palindrome in a given String
/* Print Smallest and Biggest possible Word which is Palindrome in a given String */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int x = 0, l = 0, j, k, space = 0, count = 0, init = 0, min = 0, max = 0, len = 0, flag;
char a[100], b[30][20], c[30], d[30], minP[30], maxP[30];
printf("Read a string:\n");
fflush(stdin);
scanf("%[^\n]s", a);
for (x = 0;a[x] != '\0';x++)
{
if (a[x] == ' ')
space++;
}
x = 0;
for (j = 0;j<(space+1);x++, j++)
{
k = 0;
while (a[x] != '\0')
{
if (a[x] == ' ')
{
break;
}
else
{
b[j][k++] = a[x];
x++;
}
}
b[j][k] = '\0';
}
for (j = 0;j < space + 1;j++)
printf("%s ", b[j]);
printf("\n");
for (x = 0;x < space + 1;x++)
{
strcpy(c, b[x]);
count = strlen(b[x]);
k = 0;
for (l = count - 1;l >= 0;l--)
d[k++] = b[x][l];
d[k] = '\0';
if (strcmp(d, c) == 0) {
flag = 1;
if (init < 1)
{
strcpy(minP, d);
strcpy(maxP, d);
min = strlen(minP);
max = strlen(maxP);
init++;
}
printf("String %s is a Palindrome\n", d);
len = strlen(d);
if (len >= max)
strcpy(maxP, d);
else if (len <= min)
strcpy(minP, d);
else
printf("");
}
}
if (flag == 1)
{
printf("The minimum palindrome is %s\n", minP);
printf("The maximum palindrome is %s\n", maxP);
}
else
printf("given string has no pallindrome\n");
}
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.
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.
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).
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).
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.
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,
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 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.
Copy string. Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point). To avoid overflows, the size of the array pointed by destination shall be long enough to contain the same C string as source (including the terminating null character), and should not overlap in memory with source.
An array is a collection of data items, all of the same type, accessed using a common name. A one-dimensional array is like a list; A two dimensional array is like a table; The C language places no limits on the number of dimensions in an array, though specific implementations may. Some texts refer to one-dimensional arrays as vectors, two-dimensional arrays as matrices, and use the general term arrays when the number of dimensions is unspecified or unimportant.
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.
#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:
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.
C Program input two numbers from user and find maximum between two numbers using switch case. Input two numbers. Store it in a variable say j1 & j2. Switch expression j1 > j2
Program 'takes a number' as input and finds a Greater number of the entered number using same digits. Take a number as input. Reverse the number and store it in the array. Using for
Program to display Binary equivalent of an input Hexadecimal Number (Hex to Binary Converter). Number will be accepted if you press Ctrl-Z & then ENTER after inputting...
Step by step descriptive logic to find roots of Quadratic Equation using Switch Case. Input coefficients of quadratic equation. Store it in some variable say a, b, c. Find discriminant of
C Program code creates a support for Infinite Precision Arithmetic which allows storage of Large Integers which is beyond the Range of the integral limit. C is architecture dependent
C Program to Find the Summation of Node values at Row or Level. Create a new node with the data from the user. Check for root node and then create it. Searching for the...