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

Using functions isspace, iscntrl, ispunct, isprint, isgraph

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
/* Using functions isspace, iscntrl, ispunct, isprint, isgraph */ #include <iostream> #include <ctype.h> using namespace std; int main(){ cout << "According to isspace:\nNewline " << ( isspace( '\n' ) ? "is a" : "is not a" ) << " whitespace character\nHorizontal tab " << ( isspace( '\t' ) ? "is a" : "is not a" ) << " whitespace character\n" << ( isspace( '%' ) ? "% is a" : "% is not a" ) << " whitespace character\n"; cout << "\nAccording to iscntrl:\nNewline " << ( iscntrl( '\n' ) ? "is a" : "is not a" ) << " control character\n" << ( iscntrl( '$' ) ? "$ is a" : "$ is not a" ) << " control character\n"; cout << "\nAccording to ispunct:\n" << ( ispunct( ';' ) ? "; is a" : "; is not a" ) << " punctuation character\n" << ( ispunct( 'Y' ) ? "Y is a" : "Y is not a" ) << " punctuation character\n" << ( ispunct('#') ? "# is a" : "# is not a" ) << " punctuation character\n"; cout << "\nAccording to isprint:\n" << ( isprint( '$' ) ? "$ is a" : "$ is not a" ) << " printing character\nAlert " << ( isprint( '\a' ) ? "is a" : "is not a" ) << " printing character\n"; cout << "\nAccording to isgraph:\n" << ( isgraph( 'Q' ) ? "Q is a" : "Q is not a" ) << " printing character other than a space\nSpace " << ( isgraph(' ') ? "is a" : "is not a" ) << " printing character other than a space" << endl; return 0; }
Namespaces in C++ Language
Consider a situation, when we have two persons with the same name, jhon, in the same class. Whenever we need to differentiate them definitely we would have to use some additional information along with their name, like either the area, if they live in different area or their mother's or father's name, etc. Same situation can arise in your C++ applications. For example, you might be writing some code that has a function called xyz() and there is another library available which is also having same function xyz(). Now the compiler has no way of knowing which version of xyz() function you are referring to within your code. A namespace is designed to overcome this difficulty and is used as additional information to differentiate similar functions, classes, variables etc. with the same name available in different libraries. Using namespace, you can define the context in which names are defined. In essence, a namespace defines a scope.
Defining a Namespace
A namespace definition begins with the keyword namespace followed by the namespace name as follows:
namespace namespace_name { // code declarations }
To call the namespace-enabled version of either function or variable, prepend (::) the namespace name as follows:
name::code; // code could be variable or function.
Using Directive
You can also avoid prepending of namespaces with the using namespace directive. This directive tells the compiler that the subsequent code is making use of names in the specified namespace.
Discontiguous Namespaces
A namespace can be defined in several parts and so a namespace is made up of the sum of its separately defined parts. The separate parts of a namespace can be spread over multiple files. So, if one part of the namespace requires a name defined in another file, that name must still be declared. Writing a following namespace definition either defines a new namespace or adds new elements to an existing one:
namespace namespace_name { // code declarations }
Nested Namespaces
Namespaces can be nested where you can define one namespace inside another name space as follows:
namespace namespace_name1 { // code declarations namespace namespace_name2 { // code declarations } }
• Namespace is a feature added in C++ and not present in C. • A namespace is a declarative region that provides a scope to the identifiers (names of the types, function, variables etc) inside it. • Multiple namespace blocks with the same name are allowed. All declarations within those blocks are declared in the named scope. • Namespace declarations appear only at global scope. • Namespace declarations can be nested within another namespace. • Namespace declarations don't have access specifiers. (Public or private) • No need to give semicolon after the closing brace of definition of namespace. • We can split the definition of namespace over several units.
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
/* namespaces in C++ language */ // A C++ code to demonstrate that we can define // methods outside namespace. #include <iostream> using namespace std; // Creating a namespace namespace ns { void display(); class happy { public: void display(); }; } // Defining methods of namespace void ns::happy::display() { cout << "ns::happy::display()\n"; } void ns::display() { cout << "ns::display()\n"; } // Driver code int main() { ns::happy obj; ns::display(); obj.display(); return 0; }
Locale Library iscntrl() Function in C++
Check if character is a control character. Checks whether c is a control character. A control character is a character that does not occupy a printing position on a display (this is the opposite of a printable character, checked with isprint). For the standard ASCII character set (used by the "C" locale), control characters are those between ASCII codes 0x00 (NUL) and 0x1f (US), plus 0x7f (DEL).
Syntax for iscntrl() Function in C++
#include <ctype.h> int iscntrl ( int c );
c
Character to be checked, casted to an int, or EOF. Function returns a value different from zero (i.e., true) if indeed c is a control character. Zero (i.e., false) otherwise. This function is used to check if the argument contains any control characters. There are many types of control characters in C++ such as: • Horizontal tab -- '\t' • Line feed -- '\n' • Backspace -- '\b' • Carriage return -- '\r' • Form feed -- '\f' • Escape 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. In C++, a locale-specific template version of this function (iscntrl) exists in header <locale>.
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
/* In C++, iscntrl() is a predefined function used for string and character handling. cstring is the header file required for string functions and cctype is the header file required for character functions. A control character is one which is not a printable character i.e, it does not occupy a printing position on a display. */ /* Check if character is a control character by iscntrl() function code example */ // CPP program to count control characters in a string #include <iostream> #include <cstring> #include <cctype> using namespace std; // function to calculate control characters void space(string& str) { int count = 0; int length = str.length(); for (int i = 0; i < length; i++) { int c = str[i]; if (iscntrl(c)) count++; } cout << count; } // Driver Code int main() { string str = "My name \n is \n Clementine"; space(str); return 0; }
Locale Library isprint() Function in C++
This function checks if character is printable. Checks whether c is a printable character. A printable character is a character that occupies a printing position on a display (this is the opposite of a control character, checked with iscntrl). For the standard ASCII character set (used by the "C" locale), printing characters are all with an ASCII code greater than 0x1f (US), except 0x7f (DEL). isgraph returns true for the same cases as isprint except for the space character (' '), which returns true when checked with isprint but false when checked with isgraph.
Syntax for isprint() Function in C++
#include <cctype> int isprint ( int c );
c
Character to be checked, casted to an int, or EOF. Function returns a value different from zero (i.e., true) if indeed c is a printable character. Zero (i.e., false) otherwise. The isprint() function checks if ch is printable as classified by the current C locale. By default, the following characters are printable: • Digits (0 to 9) • Uppercase letters (A to Z) • Lowercase letters (a to z) • Punctuation characters (!"#$%&'()*+,-./:;<=>[email protected][\]^_`{|}~) • Space 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. In C++, a locale-specific template version of this function (isprint) exists in header <locale>.
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
/* The C++ <cctype> isprint() function is used to check if the given character is a printable character. A printable character is a character that occupies a printing position on a display. In the default "C" locale, a printable character are those having ASCII code greater than 0x1f (US), except 0x7f (DEL). Other locales may consider a different selection of characters as printable characters. */ /* Check if character is printable by isprint() function code example */ // CPP program to count printable characters in a string #include <iostream> #include <cstring> #include <cctype> using namespace std; // function to calculate printable characters void space(string& str) { int count = 0; int length = str.length(); for (int i = 0; i < length; i++) { int c = str[i]; if (isprint(c)) count++; } cout << count; } // Driver Code int main() { string str = "My name is Clementine"; space(str); return 0; }
ispunct() Function in C++
Check if character is a punctuation character. Checks whether c is a punctuation character. The standard "C" locale considers punctuation characters all graphic characters (as in isgraph) that are not alphanumeric (as in isalnum). Other locales may consider a different selection of characters as punctuation characters, but in any case they are isgraph but not isalnum. 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. In C++, a locale-specific template version of this function (ispunct) exists in header <locale>.
Syntax for ispunct() Function in C++
#include <cctype> int ispunct ( int c );
c
Character to be checked, casted to an int, or EOF. Function returns a value different from zero (i.e., true) if indeed c is a punctuation character. Zero (i.e., false) otherwise. Checks if the given character is a punctuation character as classified by the current C locale. The default C locale classifies the characters !"#$%&'()*+,-./:;<=>[email protected][\]^_`{|}~ as punctuation. The behavior is undefined if the value of ch is not representable as unsigned char and is not equal to EOF.
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
/* The C++ <cctype> ispunct() function is used to check if the given character is a punctuation character. In the default "C" locale, a punctuation character are all those graphic characters (as in isgraph) which are not alphanumeric (as in isalnum). */ /* Checks whether c is a punctuation character by ispunct() function code example. */ #include <iostream> #include <cctype> using namespace std; int main (){ char str[50] = "Hello, World!"; //replacing all punctuation //characters with @ in str int i = 0; while(str[i]) { if(ispunct(str[i])) str[i] = '@'; i++; } //displaying the output cout<<"str contains: "<<str; return 0; }
Locale Library isgraph() Function in C++
Check if character has graphical representation. Checks whether c is a character with graphical representation. The characters with graphical representation are all those characters than can be printed (as determined by isprint) except the space character (' '). For a detailed chart on what the different ctype functions return for each character of the standard ASCII character set, see the reference for the <cctype> header.
Syntax for isgraph() Function in C++
#include <cctype> int isgraph ( int c );
c
Character to be checked, casted to an int, or EOF. Function returns a value different from zero (i.e., true) if indeed c has a graphical representation as character. Zero (i.e., false) otherwise. The isgraph() function checks if ch has a graphical representation as classified by the current C locale. By default, the following characters are graphic: • Digits (0 to 9) • Uppercase letters (A to Z) • Lowercase letters (a to z) • Punctuation characters (!"#$%&'()*+,-./:;<=>[email protected][\]^_`{|}~) In C++, a locale-specific template version of this function (isgraph) exists in header <locale>.
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
/* The C++ <cctype> isgraph() function is used to check if the given character has a graphical representation. A character with graphical representation are those characters than can be printed (as determined by isprint) except the space character (' '). In the default "C" locale, the following characters are graphic: Digits (0123456789) Uppercase letters (ABCDEFGHIJKLMNOPQRSTUVWXYZ) Lowercase letters (abcdefghijklmnopqrstuvwxyz) Punctuation characters (!"#$%&'()*+,-./:;<=>[email protected][\]^_`{|}~) */ /* Check if character has graphical representation by isgraph() function code example */ #include <iostream> #include <cctype> using namespace std; int main (){ char str[50] = "Hello \nWorld!"; //replacing all characters which has no //graphical representation with @ in str int i = 0; while(str[i]) { if(!isgraph(str[i])) str[i] = '@'; i++; } //displaying the output cout<<"str contains: "<<str; return 0; }
isspace() Function in C++
Checks whether c is a white-space character. In C++, isspace is a predefined function used for string and character handling. cstring is the header file required for string functions and cctype is the headerfile required for character functions. isspace() function is used to check whether the argument contains white-space characters like space, horizontal, vertical tab, formfeed, carriage return or a newline. It returns zero if these white-spaces are not found.
Syntax for isspace() Function in C++
#include <cctype> int isspace ( int c );
c
Character to be checked, casted to an int, or EOF. Function returns a value different from zero (i.e., true) if indeed c is a white-space character. Zero (i.e., false) otherwise. There are many types of whitespace characters in c++ such as: space (0x20, ' '), form feed (0x0c, '\f'), line feed (0x0a, '\n'), carriage return (0x0d, '\r'), horizontal tab (0x09, '\t'), vertical tab (0x0b, '\v'). The behaviour of isspace() is undefined if the value of c is not representable as unsigned char or is not equal to EOF. It is defined in <cctype> header file. ch: The character to check.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* check if character is a white-space by isspace() function code example */ #include <iostream> #include <cctype> using namespace std; int main (){ char str[50] = "To\rbe,\nor not to\tbe"; //replacing the whitespace character //with new line character in str int i = 0; while(str[i]) { if(isspace(str[i])) str[i] = '\n'; i++; } //displaying the output cout<<"str contains:\n"<<str; return 0; }
main() Function in C++
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.
Syntax for main() Function in C++
void main() { ............ ............ }
void
void is a keyword in C++ language, void means nothing, whenever we use void as a function return type then that function nothing return. here main() function no return any value.
main
main is a name of function which is predefined function in C++ library. In place of void we can also use int return type of main() function, at that time main() return integer type value. 1) It cannot be used anywhere in the program a) in particular, it cannot be called recursively b) its address cannot be taken 2) It cannot be predefined and cannot be overloaded: effectively, the name main in the global namespace is reserved for functions (although it can be used to name classes, namespaces, enumerations, and any entity in a non-global namespace, except that a function called "main" cannot be declared with C language linkage in any namespace). 3) It cannot be defined as deleted or (since C++11) declared with C language linkage, constexpr (since C++11), consteval (since C++20), inline, or static. 4) The body of the main function does not need to contain the return statement: if control reaches the end of main without encountering a return statement, the effect is that of executing return 0;. 5) Execution of the return (or the implicit return upon reaching the end of main) is equivalent to first leaving the function normally (which destroys the objects with automatic storage duration) and then calling std::exit with the same argument as the argument of the return. (std::exit then destroys static objects and terminates the program). 6) (since C++14) The return type of the main function cannot be deduced (auto main() {... is not allowed). 7) (since C++20) The main function cannot be a coroutine.
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
/* simple code example by main() function in C++ */ #include <iostream> using namespace std; int main() { int day = 4; switch (day) { case 1: cout << "Monday"; break; case 2: cout << "Tuesday"; break; case 3: cout << "Wednesday"; break; case 4: cout << "Thursday"; break; case 5: cout << "Friday"; break; case 6: cout << "Saturday"; break; case 7: cout << "Sunday"; break; } return 0; }
#include Directive in C++
#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.
Syntax for #include Directive in C++
#include "user-defined_file"
Including using " ": When using the double quotes(" "), the preprocessor access the current directory in which the source "header_file" is located. This type is mainly used to access any header files of the user's program or user-defined files.
#include <header_file>
Including using <>: While importing file using angular brackets(<>), the the preprocessor uses a predetermined directory path to access the file. It is mainly used to access system header files located in the standard system directories. Header File or Standard files: This is a file which contains C/C++ function declarations and macro definitions to be shared between several source files. Functions like the printf(), scanf(), cout, cin and various other input-output or other standard functions are contained within different header files. So to utilise those functions, the users need to import a few header files which define the required functions. User-defined files: These files resembles the header files, except for the fact that they are written and defined by the user itself. This saves the user from writing a particular function multiple times. Once a user-defined file is written, it can be imported anywhere in the program using the #include preprocessor. • In #include directive, comments are not recognized. So in case of #include <a//b>, a//b is treated as filename. • In #include directive, backslash is considered as normal text not escape sequence. So in case of #include <a\nb>, a\nb is treated as filename. • You can use only comment after filename otherwise it will give error.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* using #include directive in C language */ #include <stdio.h> int main() { /* * C standard library printf function * defined in the stdio.h header file */ printf("I love you Clementine"); printf("I love you so much"); printf("HappyCodings"); return 0; }


Program stores the information (name, roll & marks) of 10 students using structures. In this program, a structure, student is created. This structure has three members: name ('string'),
Bubble sort algorithm sort data by comparing 2 consecutive numbers. The time Complexity of this algorithm is O(n^2). And compare two consecutive number. Switch values if number
Implement the binary search to find a peak in the array. If the 'middle element' is more than its both Neighbors, 'it is the Peak'. Otherwise, split the array and check the same. A function
C++ Program to generate N passwords each of 'length M'. Problem focuses on finding the N permutations each of length M. Generates random number between 1 and 10. Enter the