C Programming Code Examples
C > If Else and Switch Case Code Examples
Program to check uppercase or lowercase characters using library functions
/* Program to check uppercase or lowercase characters using library functions
Logic to check uppercase and lowercase alphabets
Step by step descriptive logic to check uppercase and lowercase alphabets.
Input a character from user. Store it in some variable say ch.
Character is uppercase alphabet if(ch >= 'A' and ch <= 'Z').
Character is lowercase alphabet if(ch >= 'a' and ch <= 'z').
If none of the above conditions met, then character is not alphabet. */
#include <stdio.h>
#include <ctype.h> /* Used for isupper() and islower() */
int main()
{
char ch;
/* Input character from user */
printf("Enter any character: ");
scanf("%c", &ch);
if(isupper(ch))
{
printf("'%c' is uppercase alphabet.", ch);
}
else if(islower(ch))
{
printf("'%c' is lowercase alphabet.", ch);
}
else
{
printf("'%c' is not an alphabet.", ch);
}
return 0;
}
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.
Checks whether c is a lowercase letter. Notice that what is considered a letter may depend on the locale being used; In the default "C" locale, a lowercase 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. Other locales may consider a different selection of characters as lowercase characters, but never characters that returns true for iscntrl, isdigit, ispunct or isspace. For a detailed chart on what the different ctype functions return for each character of the standard ANSII character set, see the reference for the <cctype> header.
Checks if parameter c is an uppercase alphabetic letter. The isupper() function checks whether a character is an uppercase alphabet (A-Z) or not. Function isupper() takes a single argument in the form of an integer and returns a value of type int. Even though, isupper() takes integer as an argument, character is passed to the function. Internally, the character is converted to its ASCII for the check. 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. Other locales may consider a different selection of characters as uppercase characters, but never characters that returns true for iscntrl, isdigit, ispunct or isspace.
#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 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.
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,
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.
Write a C program to find minimum occurring character in a string. Initialize frequency of all characters to 0 and then "Finds Frequency" of each characters. "Finds minimum frequency".
If ptr is null, realloc() allocates size bytes of memory and returns a pointer. If size is zero, the memory pointed to by ptr is freed. There is an error give message: Allocation Error...
C programming code to set local information. Refers to all localization categories. Affects the operation of the strcoll() function. Alters the way the character functions work and...
C Code creates a file & store information. We frequently use files for storing information which can be processed by our programs. In order to store information permanently and