11 String Functions
11 String Functions
11 String Functions
Lecture 11
Example:
#include<conio.h>
#include<math.h>
main()
float d,f,b=0.56,x=-1,y=0.33;
d=log(1000);
f=sin(b);
69
Department of Electrical Engineering. First Year / 2016-2017. By: Salwa Adel Al-agha
Lecture 11
getch();
The result is :
Lecture 11
Example:
#include<conio.h>
#include<math.h>
main()
for(int i=1;i<=10;i++)
getch();
Example:
Write a program in C+
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();
- 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:
Example:
#include<conio.h>
#include<string.h>
main()
cin>>E;
cout<<strlen(A)<<'\t'<<strlen(S)<<'\t'<<strlen(Z)<<'\t'<<strlen(E);
getch();
6 5 8 6
73
Department of Electrical Engineering. First Year / 2016-2017. By: Salwa Adel Al-agha
Lecture 11
strchr:
Example:
Ans:
#include<iostream.h>
#include<conio.h>
#include<string.h>
main()
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
Higher Education
of Technology
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:
#include<iostream.h>
#include<conio.h>
#include<string.h>
main()
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();
University of Technology
University of Technology
Electrsity of Technology
Eliversity of Technology
Eliversity of Technology
Eliversity of Technology
Example:
#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()
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();
987asdfesu 98765
98765 98765
+#$% +#$%
strcat - strncat:
77
Department of Electrical Engineering. First Year / 2016-2017. By: Salwa Adel Al-agha
Lecture 11
Strncat(st1,st2,no.)
Example:
#include<iostream.h>
#include<conio.h>
#include<string.h>
main()
cout<< strncat(A,S,3)<<endl;
cout<< strcat(A,S)<<endl;
getch();
qweasdfesu987
qweasdfesu98798765
78
Department of Electrical Engineering. First Year / 2016-2017. By: Salwa Adel Al-agha
Lecture 11
Example:
#include<iostream.h>
#include<conio.h>
#include<string.h>
main()
cout<<strcat(A,S)<<endl;
cout<<strncat(S,Z,4)<<endl;
cout<<strcat(S,Z)<<endl;
getch();
MinistryUniversity
UniversityElec
UniversityElecElectrical
79