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++ > Algorithms Code Examples

Frequency words, characters

/* Frequency words, characters */ // ISO/ANSI C program // 0 Errors and 0 Warnings on turboC 3.0 compilers // borland 3.0 #include<stdio.h> #include<conio.h> #include<string.h> main() { int ch,i,j,k,l,fc[50][2],flag; char line[50],fw[50][50]; clrscr(); do { printf("\n<1> Input a line"); printf("\n<2> Number of characters"); printf("\n<3> Number of words"); printf("\n<4> Number of vowals"); printf("\n<5> Frequency of characters"); printf("\n<6> Frequency of words"); printf("\n<7> Exit\n"); do { printf("\n enter your choice "); scanf("%d",&ch); }while(ch>7 || ch<1); switch (ch) { case 1: //input a line printf("\n Enter a line "); scanf("\n%[^\n]",line); break; case 2: //number of characters j=0; for(i=0;i<strlen(line);++i) { if(line[i]>64 && line[i]<93) ++j; else if(line[i]>96 && line[i]<123) ++j; } printf("total number of characters are %d",j); break; case 3: //number of words j=0; for(i=0;i<strlen(line);++i) { if(line[i]==' ' || line[i]=='\t') continue; else { for(k=i;k<strlen(line);++k) { if(line[k]==' ' || line[k]=='\t') break; else ++i; } } ++j; } printf("\n total number of words are %d",j); break; case 4: //number of vowels j=0; for(i=0;i<strlen(line);++i) { if(line[i]=='a' || line[i]=='e' || line[i]=='i' || line[i]=='o' || line[i]=='u') ++j; else if(line[i]=='A' || line[i]=='E' || line[i]=='I' || line[i]=='O' || line[i]=='U') ++j; } printf("total number of vowels are %d",j); break; case 5: //frequency of characters for(i=0;i<strlen(line);++i) { fc[i][1]=0; flag=0; for(j=0;j<strlen(line);++j) { if(line[i]==fc[j][0]) { fc[j][0]=line[i]; ++fc[j][1]; flag=1; break; } } if(flag==0) fc[i][0]=line[i],fc[i][1]=1; } printf("\n Frequency of each characters"); for(i=0;i<strlen(line);++i) if(fc[i][1]!=0) printf("\n%c %d",fc[i][0],fc[i][1]); break; case 6: //frequency of words j=0; for(i=0;i<strlen(line);++i) { if(line[i]==' ' || line[i]=='\t') continue; else { l=0; for(k=i;k<strlen(line);++k) { if(line[k]==' ' || line[k]=='\t') break; else { fw[j][l]=line[k],fw[j][++l]='\0'; ++i; } } } ++j; } for(i=0;i<j;++i) { fc[0][0]=0; for(k=i;k<j;++k) { flag=1; for(l=0;l<j;++l) if(fw[i][l]!=fw[k][l]) flag=0; if(flag==1) { ++fc[0][0]; if(fc[0][0]>1) fw[k][0]='\0'; } } if(fc[0][0]>0) { flag=0; do { printf("%c",fw[i][flag]); } while(fw[i][flag++]!='\0'); if(fw[i][0]!='\0') printf("%d\n",fc[0][0]); } fc[0][0]=0; } break; } }while(ch!=7); printf("\n\n i will wait for your mails "); getch(); return ; }

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.

Relational operators for string. Performs the appropriate comparison operation between the string objects lhs and rhs. The functions use string::compare for the comparison. These operators are overloaded in header <string>. If strings are compared using relational operators then, their characters are compared lexicographically according to the current character traits, means it starts comparison character by character starting from the first character until the characters in both strings are equal or a NULL character is encountered.

A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.

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. A loop inside another loop is called a nested loop. The depth of nested loop depends on the complexity of a problem. We can have any number of nested loops as required. Consider a nested loop where the outer loop runs n times and consists of another loop inside it. The inner loop runs m times. Then, the total number of times the inner loop runs during the program execution is n*m.

Break statement in C++ is a loop control statement defined using the break keyword. It is used to stop the current execution and proceed with the next one. When a compiler calls the break statement, it immediately stops the execution of the loop and transfers the control outside the loop and executes the other statements. In the case of a nested loop, break the statement stops the execution of the inner loop and proceeds with the outer loop. The statement itself says it breaks the loop. When the break statement is called in the program, it immediately terminates the loop and transfers the flow control to the statement mentioned outside the loop.

Logical Operators are used to compare and connect two or more expressions or variables, such that the value of the expression is completely dependent on the original expression or value or variable. We use logical operators to check whether an expression is true or false. If the expression is true, it returns 1 whereas if the expression is false, it returns 0. Assume variable A holds 1 and variable B holds 0:

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. • The expression can be integer expression or a character expression. • Value-1, 2, n are case labels which are used to identify each case individually. Remember that case labels should not be same as it may create a problem while executing a program. Suppose we have two cases with the same label as '1'. Then while executing the program, the case that appears first will be executed even though you want the program to execute a second case. This creates problems in the program and

In computer programming, we use the if statement to run a block code only when a certain condition is met. An if statement can be followed by an optional else statement, which executes when the boolean expression is false. There are three forms of if...else statements in C++: • if statement, • if...else statement, • if...else if...else statement, The if statement evaluates the condition inside the parentheses ( ). If the condition evaluates to true, the code inside the body of if is executed. If the condition evaluates to false, the code inside the body of if is skipped.

Get string length. Returns the length of the C string str. C++ strlen() is an inbuilt function that is used to calculate the length of the string. It is a beneficial method to find the length of the string. The strlen() function is defined under the string.h header file. The strlen() takes a null-terminated byte string str as its argument and returns its length. The length does not include a null character. If there is no null character in the string, the behavior of the function is undefined.

In while loop, condition is evaluated first and if it returns true then the statements inside while loop execute, this happens repeatedly until the condition returns false. When condition returns false, the control comes out of loop and jumps to the next statement in the program after while loop. The important point to note when using while loop is that we need to use increment or decrement statement inside while loop so that the loop variable gets changed on each iteration, and at some point condition returns false. This way we can end the execution of while loop otherwise the loop would execute indefinitely. A while loop that never stops is said to be the infinite while loop, when we give the condition in such a way so that it never returns false, then the loops becomes infinite and repeats itself indefinitely.

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. (2D) array in C++ programming is also known as matrix. A matrix can be represented as a table of rows and columns. In C/C++, we can define multi dimensional arrays in simple words as array of arrays. Data in multi dimensional arrays are stored in tabular form (in row major order).

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. The getch() function does not accept any parameter from the user. It returns the ASCII value of the key pressed by the user as an input.

In computer programming, loops are used to repeat a block of code. For example, when you are displaying number from 1 to 100 you may want set the value of a variable to 1 and display it 100 times, increasing its value by 1 on each loop iteration. When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop. A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

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

Continue statement is used inside loops. Whenever a continue statement is encountered inside a loop, control directly jumps to the beginning of the loop for next iteration, skipping the execution of statements inside loop's body for the current iteration. The continue statement works somewhat like the break statement. Instead of forcing termination, however, continue forces the next iteration of the loop to take place, skipping any code in between. For the for loop, continue causes the conditional test and increment portions of the loop to execute. For the while and do...while loops, program control passes to the conditional tests.

It is a predefined function in "conio.h" (console input output header file) used to clear the console screen. It is a predefined function, by using this function we can clear the data from console (Monitor). Using of clrscr() is always optional but it should be place after variable or function declaration only. 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.

Example of using this 'pointer' is to return the "Reference of current object" so that you can chain "function calls", this way you can call all the functions for the current object in one go.