C Programming Code Examples
C > Small Programs Code Examples
Remove HTML markup
/* Remove HTML markup */
#include <stdio.h>
#include <string.h>
#define IN 0
#define OUT 1
int main(void) {
int c = 0;
int state = OUT;
int tstate = OUT;
char tagbuff[2048];
char *ptr1 = NULL;
ptr1 = tagbuff;
while((c = getchar()) != EOF) {
/* copy tag into tagbuff */
if(c == '<' || c == '&') state = IN;
if(state == IN) *ptr1++ = c;
if(c == '>' || c == ';') {
state = OUT; *ptr1++ = '\0';
/* search tagbuff, javascript, style tags */
if(strstr(tagbuff, "<s") != 0 || strstr(tagbuff, "<S") != 0)
tstate = IN;
if(strstr(tagbuff, "</") != 0)
tstate = OUT;
/* ? */
if(strstr(tagbuff, "nbsp") != 0 || strstr(tagbuff, "NBSP") != 0)
printf(" ");
ptr1 = tagbuff;
} /* end if */
/* not in a tag, print character */
if(state == OUT && tstate == OUT && c != '>' && c != ';')
printf("%c", c);
}
return 0;
}
#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:
Get character from stdin. Returns the next character from the standard input (stdin). It is equivalent to calling getc with stdin as argument. A getchar() function is a non-standard function whose meaning is already defined in the stdin.h header file to accept a single input from the user. In other words, it is the C library function that gets a single character (unsigned char) from the stdin. However, the getchar() function is similar to the getc() function, but there is a small difference between the getchar() and getc() function of the C programming language.
Locate substring. Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1. The matching process does not include the terminating null-characters, but it stops there. The function returns pointer to the first occurrence of the matched string in the given string. It is used to return substring from first match till the last character. The strstr() function searches the string str1 for the first occurrence of the string str2 (not counting str2's terminating null character). The return value is a pointer to the first character in the first occurrence in str1 of the sequence contained in str2, or a null pointer if there is no such occurrence. If str2 points to an empty string, then strstr() returns the value of its first argument, str1.
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,
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.
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.
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.
Program to check Vowel or Consonant using switch-case statement. I already explained in sample to print number of days in months, if switch case contains same action for multiple
C Programming copy all elements of an array into Another array. Accepting values into Array. Copying data from array 'a' to array 'b. Printing of all elements of array. Print the...
C program to find sum of all prime numbers between 1 to N. Input Lower and Upper limit from user. Find prime numbers in the range. Check if the current number x is Prime or not.