0% found this document useful (0 votes)
19 views11 pages

11 String Functions

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 11

Department of Electrical Engineering. First Year / 2016-2017.

By: Salwa Adel Al-agha

Lecture 11

C++ Mathematical Functions:

The C++ originally supported by the set of 22 math functions. In C++,


the math functions require the header <math.h>. All angles are in radians.

sin(x) cos(x) tan(x) asin(x) acos(x)

atan(x) atan2(x) sinh(x) cosh(x) tanh(x)

ceil(x) floor(x) exp(x) fabs(x) log(x)

log10(x) pow(x,p) sqrt(x) fmod() frexp(x)

Example:

Write a program in C++ language to test the mathematical functions:


Ans:
#include<iostream.h>

#include<conio.h>

#include<math.h>

main()

float d,f,b=0.56,x=-1,y=0.33;

d=log(1000);

f=sin(b);

cout<<"\t The log of base 10 for 1000 is = "<< log10(1000)<<endl;

cout<<"\t The log of base 2 for 1000 is = "<<d<<endl;

69
Department of Electrical Engineering. First Year / 2016-2017. By: Salwa Adel Al-agha

Lecture 11

cout<<"\t The sin of b in radian is = "<<f<<endl;

cout<<"\t The sin inverse of f is = "<< asin(f)<<endl;

cout<<"\t The floor of 2.56 is = "<< floor(2.56)<<endl;

cout<<"\t The ceil of 2.56 is = "<< ceil(2.56)<<endl;

cout<<"\t The absolute of -78.6 is = "<< fabs(-78.6)<<endl;

cout<<"\t The power of d for 2.71828 is = "<< pow(2.71828,d)<<endl;

cout<<"\t The square root of 169 is = "<< sqrt(169)<<endl;

cout<<"\t The tan inverse of arg(x/y) is = "<< atan(x/y)<<endl;

getch();

The result is :

The log of base 10 for 1000 is = 3

The log of base 2 for 1000 is = 6.90776

The sin of b in radian is = 0.531186

The sin inverse of f = 0.56

The floor of 2,56 is = 2

The ceil of 2.56 is = 3

The absolute of -78.6 is = 78.6

The power of d for 2.71828 is = 999.995

The square root of 169 is = 13


70
Department of Electrical Engineering. First Year / 2016-2017. By: Salwa Adel Al-agha

Lecture 11

The tan inverse of arg(x/y) is = -1.25205

Example:

Write a program in C++ language to obtain the equation below, for


value of i (1-10), by using mathematical functions:

y = cos i - ⅝*i + atan i / i ²


Ans:
#include<iostream.h>

#include<conio.h>

#include<math.h>

main()

for(int i=1;i<=10;i++)

cout<<"The value of y for "<<i<<" is = "<<(cos(i) - 5/8*i + atan(i) /


pow(i,2))<<endl;

getch();

Example:

Write a program in C+

+ language to test the trigonometric identity below, for value of x (0-3)


with (0.2) step, by using mathematical functions:

Sin 2x = 2 * sin x * cos x


71
Department of Electrical Engineering. First Year / 2016-2017. By: Salwa Adel Al-agha

Lecture 11

Ans:
#include<iostream.h>

#include<conio.h>

#include<math.h>

main()

for(float x=0;x<=3.0;x+=0.2)

cout<<'\t'<<x<<'\t'<<sin(2*x)<<'\t'<<2*sin(x)*cos(x)<<endl;

getch();

The String Functions:

In C++ there is a need to manipulate the strings; string functions


require the header <string.h>.

Full Name keyword Meaning:

- String Length strlen: returns the length of the string.

- String Character strchr: returns the first occurrence of character

- String Copy strcpy – strncopy: function copies up characters from the


string pointed to by str2.

- String cat strcat – strncat: function concatenates a copy of str2 to str1 and
terminates str1 with a null.

72
Department of Electrical Engineering. First Year / 2016-2017. By: Salwa Adel Al-agha

Lecture 11

 strlen:

The strlen( ) function returns the length of the null-terminated string


pointed to by str.The null terminator is not counted.

Example:

Write a program in C++ language to find a length of strings:


Ans:
#include<iostream.h>

#include<conio.h>

#include<string.h>

main()

char A[]="qweasd", S[]="98765", Z[]="*(*(*(*(", E[10];

cin>>E;

cout<<strlen(A)<<'\t'<<strlen(S)<<'\t'<<strlen(Z)<<'\t'<<strlen(E);

getch();

The result is:

6 5 8 6

73
Department of Electrical Engineering. First Year / 2016-2017. By: Salwa Adel Al-agha

Lecture 11

 strchr:

The strchr( ) function returns a pointer to the first occurrence of the


low-order byte of ch in the string pointed to by str. If no match is found, a
null pointer is returned.

Example:

Write a program in C++ language to test the (strchr) string:

Ans:
#include<iostream.h>

#include<conio.h>

#include<string.h>

main()

char A[]="Ministry of Higher Education", S[]="University of Technology",


Z[]="Electrical Engineering";

cout<<strchr(A,'H')<<endl;

cout<<strchr(S,'o')<<endl;

cout<<strchr(S,'k')<<endl;

cout<<strchr(Z,'c')<<" "<<strchr(Z,'n')<<endl;

getch();

74
Department of Electrical Engineering. First Year / 2016-2017. By: Salwa Adel Al-agha

Lecture 11

The result is:

Higher Education

of Technology

ctrical Engineering ngineering

 strcpy - strncpy:

The strcpy( ) function copies the contents of str2 into str1. str2 must be
a pointer to a null terminated string. The strcpy( ) function returns a pointer
to str1.

The strncpy( ) function copies the part of str2 into str1, depending on
the number of character which must be given from str2.

Strncpy(st1,st2,no.)

Example:

Write a program in C++ language to test the (strcpy-strncpy) strings:


Ans:

#include<iostream.h>

#include<conio.h>

#include<string.h>

main()

char A[]="Ministry of Higher Education", S[]="University of Technology",


Z[]="Electrical Engineering";

75
Department of Electrical Engineering. First Year / 2016-2017. By: Salwa Adel Al-agha

Lecture 11

cout<<strcpy(A,S)<<endl;

cout<<A<<endl;

cout<<strncpy(A,Z,5)<<endl;

cout<<strncpy(S,Z,2)<<endl;

cout<<strcpy(A,S)<<endl;

cout<<A<<endl;

getch();

The result is:

University of Technology

University of Technology

Electrsity of Technology

Eliversity of Technology

Eliversity of Technology

Eliversity of Technology

Example:

Write a program in C++ language to test the (strcpy-strncpy) strings:


Ans:

#include<iostream.h>

#include<conio.h>
76
Department of Electrical Engineering. First Year / 2016-2017. By: Salwa Adel Al-agha

Lecture 11

#include<string.h>

main()

char A[]="qweasdfesu", S[]="98765", D[]="+#$%";

strncpy(A,S,3);

cout<<A<<'\t'<<S<<endl;

strncpy(A,S,8);

cout<<A<<'\t'<<S<<endl;

strcpy(A,D);

cout<<A<<'\t'<<D<<endl;

getch();

The result is:

987asdfesu 98765

98765 98765

+#$% +#$%

 strcat - strncat:

The strcat() function concatenates a copy of str2 to str1 and terminates


str1 with a null. The null terminator originally ending str1 is overwritten by
the first character of str2. The string str2 is untouched by the operation.

77
Department of Electrical Engineering. First Year / 2016-2017. By: Salwa Adel Al-agha

Lecture 11

The strncat( ) function concatenates part of str2 with str1, depending on


the number of character which must be give from str2.

Strncat(st1,st2,no.)

Example:

Write a program in C++ language to test the (strcat-strncat) strings:


Ans:

#include<iostream.h>

#include<conio.h>

#include<string.h>

main()

char A[]="qweasdfesu", S[]="98765";

cout<< strncat(A,S,3)<<endl;

cout<< strcat(A,S)<<endl;

getch();

The result is:

qweasdfesu987

qweasdfesu98798765

78
Department of Electrical Engineering. First Year / 2016-2017. By: Salwa Adel Al-agha

Lecture 11

Example:

Write a program in C++ language to test the (strcat-strncat) strings:


Ans:

#include<iostream.h>

#include<conio.h>

#include<string.h>

main()

char A[]="Ministry", S[]="University", Z[]="Electrical";

cout<<strcat(A,S)<<endl;

cout<<strncat(S,Z,4)<<endl;

cout<<strcat(S,Z)<<endl;

getch();

The result is:

MinistryUniversity

UniversityElec

UniversityElecElectrical

79

You might also like