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 > Data Structures Code Examples

Infix To Prefix Conversion

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
/* Infix To Prefix Conversion */ #include<stdio.h> #include<conio.h> #include<string.h> #define MAX 15 #define true 1 #define false 0 /*Structure Decvlaration*/ typedef struct { char data[MAX]; char top; }STK; /*Function Declarations*/ void input(char str[]); void intopre(char str1[],char pre[]); void intopost(char str1[],char post[]); int isoperand(char sym); int prcd(char sym); void push(STK *s1,char elem); int pop(STK *s1); int empty(STK *s2); int full(STK *s2); void dis(char str[]); void main() { STK s; int cs,ans; char str[MAX],pre[MAX],post[MAX]; clrscr(); do /*Using Do-while Loop*/ { clrscr(); printf(" -----Program for Expressions-----"); printf(" Input The String:"); printf(" MENU: "); printf("1.Infix to Prefix "); printf("2.Infix to Postfix"); printf(" 3.Exit"); cs=getche(); switch(cs) /*Using Switch Case*/ { case 1: intopre(str,pre); break; case 2: intopost(str,post); break; case 3: break; default: printf(" Enter a Valid Choise!"); /*Default Case*/ break; } printf(" Do you wish to Continue?(y/n)"); ans=getche(); }while(ans=='y'||ans=='Y'); /*Condition for Do-while loop*/ getch(); } /*To Input String*/ void input(char str) { printf("Enter the Infix String:"); scanf("%s",str); } /*To Covert Infix To Prefix*/ void intopre(STK s1,char str1[],char pre[]) { int len,flag; len=strlen(str1); int check=0,cnt=len-1,pos=0; char elem; while(cnt>=0) /*while condition*/ { flag=0; if(isoperand(str1[cnt])) /*Checking for Operand*/ { printf("%c",str1[cnt]); cnt--; pos++; } else { check=prcd(str1[cnt]); while(check==false) { pre[pos]=str1[cnt]; flag=1; pos++; cnt--; } if(flag==0) { elem=pop(&s1); printf("%c",elem); } } } } /*To Convert Infix To Postfix*/ void intopost(STK s1,char str1[],char post[]) { int len; len=strlen(str1); int check=0,cnt=len-1,pos=0; } /*To Check For Operand*/ int isoperand(char sym) { if('A'<sym<'Z'||'a'<sym<'z') return(true); return(false); } /*To Check The Precedence*/ int prcd(char sym) { } /*To Display String*/ void dis(char str[]) { } /*Push Function Definition*/ void push(STK *s1,char elem) { if(!full(s1)) { s1->top++; /*Incrementing top*/ s1->data[s1->top]=elem; /*Storing element*/ } else printf(" Stack is Full!"); } /*Full Function Definition*/ int full(STK *s2) { if(s2->top==MAX) /*Condition for Full*/ return(true); return(false); } /*Pop Function Definition*/ int pop(STK *s1) { char elem; if(!empty(s1)) { elem=s1->data[s1->top]; /*Storing top stack element in elem*/ s1->top--; /*Decrementing top*/ return(elem); } return(false); } /*Empty Function Definition*/ int empty(STK *s2) { if(s2->top==-1) /*Condition For Empty*/ return(true); return(false); }

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).

The typedef is a keyword used in C programming to provide some meaningful names to the already existing variable in the C program. It behaves similarly as we define the alias for the commands. In short, we can say that this keyword is used to redefine the name of an already existing variable.

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 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.

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.

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.

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

Generate random number. Returns a pseudo-random integral number in the range between 0 and RAND_MAX. This number is generated by an algorithm that returns a sequence of apparently non-related numbers each time it is called. This algorithm uses a seed to generate the series, which should be initialized to some distinctive value using function srand.

Function clrscr() clears the screen and moves the cursor to the upper left-hand corner of the screen. If you are using the GCC compiler, use system function to execute the clear/cls command. clrscr() function is also a non-standard function defined in "conio.h" header. This function is used to clear the console screen. It is often used at the beginning of the program (mostly after variable declaration but not necessarily) so that the console is clear for our output.

Switch statement in C tests the value of a variable and compares it with multiple cases. Once the case match is found, a block of statements associated with that particular case is executed. Each case in a block of a switch has a different name/number which is referred to as an identifier. The value provided by the user is compared with all the cases inside the switch block until the match is found. If a case match is NOT found, then the default statement is executed, and the control goes out of the switch block.

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.

The getch() is a predefined non-standard function that is defined in conio.h header file. It is mostly used by the Dev C/C++, MS- DOS's compilers like Turbo C to hold the screen until the user passes a single value to exit from the console screen. It can also be used to read a single byte character or string from the keyboard and then print. It does not hold any parameters. It has no buffer area to store the input character in a program.

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 the next keystroke from the console, and echo it. getche() function is a function in C programming language which waits for any character input from keyboard and it will also echo the input character on to the output screen. getche() function echoes the character to the screen whereas getch() does not do so. This is the only difference between both the functions. It can be remembered by the use of character 'e' at the end of getche() function.

C Program code to find area & circumference of circle. In this program we have to calculate the area and circumference of the circle. Two formulas for finding Circumference and Area




C Program code print all the Elements of nth level in single line. And adding node to binary tree. Prints all the nodes of all levels of the binary tree. Prints the nodes of a particular...


The LCM of two integers num1 and num2 is the smallest positive integer that is perfectly divisible by both num1 and num2 (without a remainder). For example: the LCM of 72 and


C Implement stack operations using dynamic memory allocation. Use malloc function to allocate memory. Define separate functions for the operations like push, pop and display.