The document discusses various string handling, mathematical, and random number generation functions available in C++ library. It provides examples of functions like strlen(), strcpy(), strcmp(), sqrt(), pow(), randomize(), random(). It also provides programs to demonstrate the use of these functions for tasks like checking palindromes, searching strings, toggling case, generating random numbers in a given range.
2. In built or Library Functions
In Built
Functions
Numeric
Mathematical
General
Purpose
Character String
standard
input/output
3. String Functions in C++
• C++ provides us with many functions to handle strings.
• string.h header file provides these functions.
Function Operation
strlen(str1); Finds the length of s string
strcpy(str1, str2); Copies string str2 to str1. Here str1 is the destination string and str2 is the source string.
strcat(str1, str2); Concatenates or joins str1 and str2. The concatenated string is stored in str1.
strcmp(str1, str2); Compares str1 and str2. It takes the case into consideration / For eg “Best” = “best”
If str1 and str2 are exactly equal, it returns 0
If str1> str2 , it returns -1 or negative number
If str1< str2 , it returns 1 or positive number
4. String Functions in C++……contd
Function Operation
strcmpi(str1, str2); Does the same as strcmp, but simply ignores the case of the characters. Ie
“Best”=“best”. The ‘i’ in the end signifies ‘ignore the case’.
strlwr(str1); Converts the entire string to lowercase
strupr(str1); Converts the entire string to uppercase
strrev(str1); Reverses the entire string
5. String functions……. examples
Function Example Output
strlen(S) char str[]=”Opera Winfrey”;
cout<<strlen(str);
13
strcat(S1, S2) char name[]=”Kiran Gupta”;
char title[]=”Miss”;
char str[25];
strcat(str, title);
strcat(str, name);
cout<<str;
Miss Kiran Gupta
strcpy(S1, S2) char a[20], b[20];
strcpy(a, “good”);
strcpy(b, “Morning”);
cout<<a<<”n”<<b;
// Now a will contain the string “good” and b will
contain “Morning”
good
Morning
6. String functions…..examples
strcmp(S1, S2)
strcmpi(S1, S2)
char str1[20]=” My Cookbook”;
if (strcmp(str1,”My Cookbook”)= =0)
cout<<”Found”;
else
cout<<”Not found”;
Found
char str1[20]=” MY CookbooK”;
if (strcmp(str1,”My Cookbook”)= =0)
cout<<”Found”;
else
cout<<”Not found”;
Not Found
This is because of letters in different cases
strrev(s) char ch[ ]=”ComPUterS”
strrev(ch);
cout<<ch;
SretUPmoC
strupr(s) char ch[ ]=”Palm oil”;
strupr(ch);
cout<<ch;
PALM OIL
strlwr(s) char ch[ ]=”pAlM Oil”;
strlwr(ch);
cout<<ch;
palm oil
7. Programs to check whether a string is a
palindrome.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
clrscr();
char str1[20],str2[20];
cout<<"|n Enter the string";
gets(str1); // input the string
strcpy(str2,str1); // store a copy of the original string
strrev(str1); // reverse the string
if(strcmp(str2,str1)==0) // compare the strings and display accordingly
{cout<<"n String is a palindrome";
else
cout<<"n Not a palindrome";
getch(); }
8. Program to search for a string in a list of
strings
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
clrscr();
char str[15][20], str1[20]; int n,flag=0;
cout<<“nHow many strings?”;
cin>>n;
cout<<“nEnter strings”;
for(int i=0; i<n;i++)
gets(str[i]);
cout<<"|n Enter the string to search";
gets(str1);
for(i=0;i<n;i++)
if(strcmp(str[i],str1)==0)
{cout<<"n String fouind at “<<i+1;
flag++;
}
If(flag==0 )
cout<<"n Not found";
getch();
}
9. Character functions
• C++ provides us with functions that work on single characters.
• These are contained in the header file ctype.h
function Purpose Domain Working
isalpha(char) Checks if the character in the brackets is an alphabet. a-z, A-Z Returns 1 if true else
returns 0
isalnum(char) Checks if the character is alphanumeric. a-z, A- Z, 0-9 “
isupper(char) Checks if the character is an upper case character A-Z “
islower (char) Checks if the character is a lower case character a-z “
isdigit(char) Checks if the character is a digit 0-9 “
toupper(char) Coverts a character to uppercase
tolower(char) Coverts a character to lowercase
Note : these functions work with a single character only
10. A sample program with explanation
#include<iostream.h>
#include<ctype.h>
{
char word[20]=”Welcome 2017”;
for(int i=0; word[i]!=’0’;i++)
if (isupper(word[i])
word[i]=tolower(word[i]);
else
if (islower(word[i])
word[i]= toupper(word[i]);
else
if(isdigit(word[i])
word[i]=word[i]+2;
else
word[i]=”@”;
cout<<word;}
Here each letter of the character array, word is evaluated and
modified accordingly. The diagram below shows how this works.
W e l c o m e 2 0 1 7 0
w E L C O M E @ 4 2 3 9 0
wELCOME@4239
Output
11. Program to demonstrate the use of character
functions
#include<iostream.h>
#include<ctype.h>
void main()
{
if(isalpha( ‘z’))
cout<<“nalphabet”;
else
cout<<“nnot an alphabet”;
if(isalnum( ‘#’))
cout<<“nalphanumeric”;
else
cout<<“nnot an alphanumeric”;
if(isdigit( ‘9’))
cout<<“na digit”;
else
cout<<“nnot a digit”;}
alphabet
not an alphanumeric
a digit
Output
12. Program to toggle the case of a sentence
#include<iostream.h>
#include<ctype.h>
#include<stdio.h>
#include<conio.h>
void main()
{
char string[20];
cout<<”Input a string”;
gets(string);
for(int i=0;string[i[!=’0’; i++)
if (isupper(string[i])
string[i]=tolower(string[i]);
else
if(islower(string[i])
string[i]=toupper(string[i];
cout<<”n The string after toggle case is:n”<<string;
}
Input a string:
WelCome TO thE MAze
The string after toggle case is:
wELcOME to The maZE
Output
13. Mathematical functions
• C++ provides us with various mathematical functions
• These are contained in the header file math.h
function Purpose Example Output
sqrt(x) Finds the square root of x cout<<sqrt(81); 9
pow(x,y) Raises x to the power of y cout<<pow(4,2); 16
sin(x) Returns Sine of an angle x (measured in
radians)
cout<<sin(1.57); 1
cos(x) Returns Cosine of an angle x (measured in
radians)
cout<<cos(0); 1
sqrt(x) Returns square root of a number cout<<sqrt(36); 4
pow(x,y) Returns x raised to the power y cout<<pow(2,5); 32
fabs(x) Returns Absolute value of real number x Cout<<fabs(-80); 80
14. Mathematical functions : Usage
We can use the function call as a part of the expression or in a cout statement. Also the arguments or
parameters can either be (i)constants, (ii) variables or (iii)expression
(i) Using constants as arguments
int x;
x=sqrt(16);
cout<<x;
Answer : 4
(ii) Using variables as arguments
int a,b;
a=2, b=4;
cout<<pow(m,n)
Answer: 16
(iii) Using expressions
int p, q;
p=10, q=15;
int k=sqrt(p+q);
cout<<k;
Answer : 5
15. Generating random numbers
The stdlib.h file contains some library functions. We shall discuss the randomize () and the random() function
void randomize(void);
The randomize function initiates the random function. It is implemented as a macro that calls the time()
function. The function randomize has to be called before random() to initiate the random function.
The randomize function has no return value and no arguments.
int random(int x);
The function random is used to generate a random number. The function takes a single integer argument say n and
generates any number in the range 0 to n-1. So in order to obtain a number in the range 0- 31 we must write the
expression as:
int x= random(32); // x will have a no in the range 0-31
16. Program to generate random numbers between any
two values input by the user.
#include<iostream.h>
void main()
{
int x, y;
cout<<”n Enter the starting and ending values”;
cin>>x>>y;
randomize();
int random_no=random(y-x+1)+x;
cout<<random_no;
}