Home | | Computer Science 11th std | C++ Header Files and Built-in Functions

Chapter: 11th Computer Science : Chapter 11 : Functions

C++ Header Files and Built-in Functions

Header files provide function prototype and definitions for library functions.

C++ Header Files and Built-in Functions

 

Header files provide function prototype and definitions for library functions. Data types and constants used with the library functions are also defined in them. A header file can be identified by their file extension .h. A single header file may contain multiple built-in functions.

For example: stdio.h is a header file contains pre-defined “standard input/output” functions.

 

Standard input/output (stdio.h)

 

This header file defines the standard I/O predefined functions getchar(), putchar(), gets(), puts() and etc.

 

1. getchar() and putchar() functions

 

The predefined function getchar() is used to get a single character from keyboard and putchar() function is used to display it.

 

Program 11.1 C++ code to accept a character and displays it

#include<iostream>

#include<stdio.h>

using namespace std;

int main()

{

      cout<<"\n Type a Character : ";

      char ch = getchar();

      cout << "\n The entered Character is: ";

      putchar(ch);

      return 0;

}

Output:

Type a Character : T

The entered Character is: T

 

2. gets() and puts() functions

 

Function gets() reads a string from standard input and stores it into the string pointed by the variable. Function puts() prints the string read by gets() function in a newline.

 

Program 11.2 C++ code to accepts and display a string

#include<iostream>

#include<stdio.h>

using namespace std;

int main()

{

      char str[50];

      cout<<"Enter a string : ";

      gets(str);

      cout<<"You entered: "

      puts(str);

      return(0);

}

Output :

Enter a string : Computer Science

You entered: Computer Science


Character functions (ctype.h)

 

This header file defines various operations on characters. Following are the various character functions available in C++. The header file ctype.h is to be included to use these functions in a program.

 

1. isalnum()

 

This function is used to check whether a character is alphanumeric or not. This function returns non-zero value if c is a digit or a letter, else it returns 0.

 

Syntax:

int isalnum (char c)

 

Example :

int r = isalnum(‘5’);

cout << isalnum('A') <<’\t’<<r;

But the statements given below assign 0 to the variable n, since the given character is neither an alphabet nor a digit.

char c = '$';

int n = isalnum(c);

cout<<c;

Output:

0

 

Program 11.3

#include<iostream>

#include<stdio.h>

#include<ctype.h>

using namespace std;

int main()

{

      char ch;

      int r;

      cout<<"\n Type a Character :";

      ch = getchar();

      r = isalnum(ch);

      cout<<"\nThe Return Value of isalnum(ch) is :"<<r;

}

Output-1:

Type a Character :A

The Return Value of isalnum(ch) is :1

Output-2:

Type a Character :?

The Return Value of isalnum(ch) is :0

 

2. isalpha()

 

The isalpha() function is used to check whether the given character is an alphabet or not.

 

Syntax:

int isalpha(char c);

This function will return 1 if the given character is an alphabet, and 0 otherwise 0. The following statement assigns 0 to the variable n, since the given character is not an alphabet.

int n = isalpha(‘3’);

But, the statement given below displays 1, since the given character is an alphabet.

cout << isalpha('a');

 

Program 11.4

#include<iostream>

#include<stdio.h>

#include<ctype.h>

using namespace std;

int main()

{

      char ch;

      cout << "\n Enter a charater: ";

      ch = getchar();

      cout<<"\n The Return Value of isalpha(ch) is :" << isalpha(ch) ;

}

Output-1:

Enter a charater: A

The Return Value of isalpha(ch) is :1

Output-2:

Enter a charater: 7

The Return Value of isalpha(ch) is :0

 

3. isdigit()

 

This function is used to check whether a given character is a digit or not. This function will return 1 if the given character is a digit, and 0 otherwise.

 

Syntax:

int isdigit(char c);

When the following program is executed, the value of the variable n will be 1,since the given character is not a digit.

 

Program 11.5

using namespace std;

#include<iostream>

#include<ctype.h>

int main()

{

      char ch;

      cout << "\n Enter a Character: ";

      cin >> ch;

      cout<<"\n The Return Value of isdigit(ch) is :" << isdigit(ch) ;

}

Output-1

Enter a Character: 3

The Return Value of isdigit(ch) is :1

Output-2

Enter a Character: A

The Return Value of isdigit(ch) is :0

 

*Return 0; (Not Compulsory in latest compilers)


4. islower()

 

This function is used to check whether a character is in lower case (small letter) or not. This functions will return a non-zero value, if the given character is a lower case alphabet, and 0 otherwise.

 

Syntax:

int islower(char c);

After executing the following statements, the value of the variable n will be 1 since the given character is in lower case.

char ch = 'n';

int n = islower(ch);

But the statement given below will assign 0 to the variable n, since the given character is an uppercase alphabet.

int n = islower('P');

 

5. isupper()

 

This function is used to check the given character is uppercase. This function will return 1 if true otherwise 0. For the following examples value 1 will be assigned to n and 0 for m.

int n=isupper(‘A’);

int m=isupper(‘a’);

 

6. toupper()

 

This function is used to convert the given character into its uppercase. This function will return the upper case equivalent of the given character. If the given character itself is in upper case, the output will be the same.

 

Syntax:

char toupper(char c);

The following statement will assign the character constant 'K' to the variable c.

char c = toupper('k’);

But, the output of the statement given below will be 'B' itself.

cout <<toupper('B');


7. tolower()

 

This function is used to convert the given character into its lowercase. This function will return the lower case equivalent of the given character. If the given character itself is in lower case, the output will be the same.

 

Syntax:

char tolower(char c);

The following statement will assign the character constant 'k' to the variable c.

char c = tolower('K’);

But, the output of the statement given below will be 'b' itself.

cout <<tolower('b');

 

String manipulation (string.h)

 

The library string.h (also referred as cstring) has several common functions for dealing with strings stored in arrays of characters. The string.h header file to be included before using any string function.

 

1. strcpy()

 

The strcpy() function takes two arguments: target and source. It copies the character string pointed by the source to the memory location pointed by the target. The null terminating character (\0) is also copied.

 

Program 11.6

#include <string.h>

#include <iostream>

using namespace std;

int main()

{

      char source[] = "Computer Science";

      char target[20]="target";

      cout<<"\n String in Source Before Copied :"<<source;

      cout<<"\n String in Target Before Copied :"<<target; strcpy(target,source);

      cout<<"\n String in Target After strcpy function Executed :"<<target; return 0;

}

Output:

String in Source Before Copied :Computer Science

String in Target Before Copied :target

String in Target After strcpy function Executed :Computer Science

 

2. strlen()

 

The strlen() takes a null terminated byte string source as its argument and returns its length. The length does not include the null(\0) character.

Program 11.7

#include <string.h>

#include <iostream>

using namespace std;

int main()

{

      char source[ ] = "Computer Science";

      cout<<"\nGiven String is "<<source<<" its Length is "<<strlen(source);

      return 0;

}

Output:

Given String is Computer Science its Length is 16

 

3. strcmp()

 

The strcmp() function takes two arguments: string1 and string2. It compares the contents of string1 and string2 lexicographically.

 

The strcmp() function returns a:

• Positive value if the first differing character in string1 is greater than the corresponding character in string2. (ASCII values are compared)

• Negative value if the first differing character in string1 is less than the corresponding character in string2.

• 0 if string1 and string2 are equal.

 

Program 11.8

#include <string.h>

#include <iostream>

using namespace std;

int main()

{

      char string1[] = "Computer";

      char string2[] = "Science";

      int result;

      result = strcmp(string1,string2);

      if(result==0)

      {

      cout<<"String1 : "<<string1<<" and String2 : "<<string2 <<"Are Equal";

      }

      if (result<0)

      {

      cout<<"String1 :"<<string1<<" and String2 : "<<string2 <<" Are Not Equal";

      }

}

Output

String1 : Computer and String2 : Science Are Not Equal

 

4. strcat()

 

The strcat() function takes two arguments: target and source. This function appends copy of the character string pointed by the source to the end of string pointed by the target.

 

Program 11.9

#include <string.h>

#include <iostream>

using namespace std;

int main()

{

      char target[50] = "Learning C++ is fun";

      char source[50] = " , easy and Very useful";

      strcat(target, source);

      cout << target ;

      return 0;

}

Output

Learning C++ is fun , easy and Very useful

 

5. strupr()

 

The strupr() function is used to convert the given string into Uppercase letters.

 

Program 11.10

using namespace std;

#include<iostream>

#include<ctype.h>

#include<string.h>

int main()

{

      char str1[50];

      cout<<"\nType any string in Lower case :";

      gets(str1);

      cout<<"\n Converted the Source string “<<str1<<into Upper Case is "<<strupr(str1); return 0;

}

Output:

Type any string in Lower case : computer science

Converted the Source string computer science into Upper Case is COMPUTER SCIENCE

 

6. strlwr()

 

The strlwr() function is used to convert the given string into Lowercase letters.

 

Program 11.11

using namespace std;

#include<iostream>

#include<ctype.h>

#include<string.h>

int main()

{

      char str1[50];

      cout<<"\nType any string in Upper case :";

      gets(str1);

      cout<<"\n Converted the Source string “<<str1<<into Lower Case is "<<strlwr(str1);

}

Output:

Type any string in Upper case : COMPUTER SCIENCE

Converted the Source string COMPUTER SCIENCE into lower Case is computer science


Mathematical functions (math.h)

 

Most of the mathematical functions are defined in math.h header file which includes basic mathematical functions.

 

1. cos() function

 

The cos() function takes a single argument in radians. The cos() function returns the value in the range of [-1, 1]. The returned value is either in double, float, or long double.

 

Program 11.12

#include <iostream>

#include <math.h>

using namespace std;

int main()

{

      double x = 0.5, result;

      result = cos(x);

      cout << "COS("<<x<<")= "<<result;

}

Output:

COS(0.5)= 0.877583

 

2. sqrt() function

 

The sqrt() function returns the square root of the given value of the argument. The sqrt() function takes a single non-negative argument. If a negative value is passed as an argument to sqrt() function, a domain error occurs.

 

Program 11.13

#include <iostream>

#include <math.h>

using namespace std;

int main()

{

      double x = 625, result;

      result = sqrt(x);

      cout << "sqrt("<<x<<") = "<<result;

      return 0;

}

Output:

sqrt(625) = 25


3. sin() function

 

The sin() function takes a single argument in radians. The sin() function returns the value in the range of [-1, 1]. The returned value is either in double, float, or long double.

 

4. pow() function

 

The pow() function returns base raised to the power of exponent. If any argument passed to pow() is long double, the return type is promoted to long double. If not, the return type is double. The pow() function takes two arguments:

base - the base value

exponent - exponent of the base

 

Program 11.14

#include <iostream>

#include <math.h>

using namespace std;

int main ()

{

      double base, exponent, result;

      base = 5;

      exponent = 4;

      result = pow(base, exponent);

      cout << "pow("<<base << "^" << exponent << ") = " << result;

      double x = 25;;

      result = sin(x);

      cout << "\nsin("<<x<<")= "<<result;

      return 0;

}

Output:

pow(5^4) = 625

sin(25)= -0.132352

 

Generating Random Numbers

 

The srand() function in C++ seeds the pseudo random number generator used by the rand() function. The seed for rand() function is 1 by default. It means that if no srand() is called before rand(), the rand() function behaves as if it was seeded with srand(1). The srand() function takes an unsigned integer as its parameter which is used as seed by the rand() function. It is defined in<cstdlib>or <stdlib.h>header file.

 

Program 11.15

#include<iostream>

#include<cstdlib.h>

using namespace std;

int main()

{

      int random = rand(); /* No srand() calls before rand(), so seed = 1*/

      cout << "\nSeed = 1, Random number = " << random; srand(10);

      /* Seed = 10 */

      random = rand();

      cout << "\n\nSeed = 10, Random number = " << random;

      return 0;

}

OUTPUT :

Seed = 1, Random number = 41

Seed = 10, Random number = 71

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
11th Computer Science : Chapter 11 : Functions : C++ Header Files and Built-in Functions |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.