You are on page 1of 10

11. .

,
.
11.1. , n! ( n).
n!

n (n-1) :

n! = n*(n-1)!
(n-1)! = (n-1)*(n-2)!

n! , (n-1)!
. (n-1)!
, (n-2)! , ..
, , 1!=1 0!=1.
/* n! */
float fact (int n)
{ if (n<0)
{ puts ( fact);
exit (1); /* */
}
if (n==1 || n==0) return 1;
return n*fact(n-1);
}
:
int k;
scanf (%d, &k);
printf (%d! = %.0f, k, fact(k));
, k=4.
104

fact (4)

24

4 * fact (3)

4*6

3 * fact (2)

3*2

2 * fact (1)

2*1

(
), ..
.
.


,
- (\0 c 0).
.
-.
, :
char str[81];
char error[] = ; /* 7 , \0
*/
, :
char *s = ;

s = error;
s 1-
, .

error.
105

error=s; , ..
, .

gets(), puts().
:
char s1[81], s2[81];
char *s3 = !;
puts (s3);
puts ( );
gets (s1);
gets (s2);
Turbo C, Borland C++
:
- (strlen),
- (strcmp, strncmp),
- (strcpy, strncpy),
- (strcat, strncat),
- (strchr, strrchr, strpbrk),
- (strstr).

string.h, .
-
strcat(). :
char *strcat (char *s1, char *s2);
s2 ( s2)
s1 s1 - .
:
char *strcat (char *s1, char *s2)
{

char *rs;

/* */

rs=s1; /* s1 */
106

while (*s1!='\0') s1++; /* s1 */


/* s2 s1 */
while (*s2!='\0')
{ *s1=*s2; s1++; s2++; }
*s1='\0';
return rs;
}
, ,
.
.
( )
:
char *strcat (char *s1, char *s2)
{ char *rs;
rs=s1; /* s1 */
while (*s1!='\0') s1++;
/* s1 */
while ((*s1++ = *s2++) !='\0'); /* s2 s1,
- */
return rs;
}
,
while :
while (*s1++ = *s2++);
?
, s1 s2
1,

.e.

. , (..

).
:
107

.
:
char *strcat (char *s1, char *s2)
{
int i=0, j=0; /* s1 * s2 */
while (s1[i]) i++;
/* s1 */
while ((s1[i++] = s2[j++])); /* s2 s1,
- */
return s1;
}

,

(
) main(). ,
,
(
#include <string.h>).
strcat():
#include <stdio.h>
#include <conio.h>
/*****************************/
/* - strcat */
/*
-
*/
/*****************************/
void main()
{ char str1[81],str2[81];
puts (" ");
gets (str1);
gets (str2);
if (strlen(str1)+strlen(str2) < 81)
{ puts (":");
puts (strcat(str1,str2));
printf (" :\n%s\n%s\n",
str1,str2);
}
else puts (" ");
108

getch();
}

. Borland C++,
(,
strlen(), ) #include
<string.h>. ,
.

1. strlen - (
-).
:

int strlen(char *s);

:
char s[81];
gets(s);
printf (" = %d\n", strlen(s));
2. strcmp - .
:

int strcmp (char *s1, char *s2);

=
,

0, ;

, .

s1="abcde",

s2= "abca12",

3 (d a=100 - 97).
3. strncmp - n .
:

int strncmp (char *s1, char *s2, int n);

0, n ;

109

.
,

s1="123456",

s2= "12789", n=4,

-4 (3 7 =51 -55). n=2, 0.


4. strcpy - s2 s1.
:

char *strcpy (char *s1, char *s2);

s1 - .
:
char a[20], b[20];
strcpy (a, 4251);
strcpy (b, a); /* a b */
5. strncpy - n s2 s1.
:

char *strncpy (char *s1, char *s2, int n);

s1 - .
,

s1="1234567",

s2= "abcde", n=4,

s1="abcd567". n > s2, s1


s2 ( -)
. n=8, s2="abcde", s1="abcde".
6. strcat - s1 s2 (
).
:

char *strcat (char *s1, char *s2);

s1 -
.
, s1="abc",

s2="defgh",

s1="abcdefgh".
110

7.

strncat - s1 s2,

n .
:

char *strncat (char *s1, char *s2, int n);

s1 -
(n s2 s1. n >
s2, ).
, s1="abc",

s2="12345", n=3,

s1="abc123".
8. strchr - s c.
:

char *strchr (char *s, char c);

- c s

NULL ( ), .
:
char str[81], *p;
gets (str);
p = strchr(str, a);
if (p == NULL) puts ( a );
else *p= b; /* 1- a b */
9. strrchr - s c.
:

char *strrchr (char *s, char c);

- c s
NULL ( ), .
:
/* */
char str[81], *p;
...
while((p=strrchr(str, ))!=NULL)
111

strcpy (p, p+1);


10. strpbrk - s1 ,
s2.
:

char *strpbrk (char *s1, char *s2);

- s1,
s2, NULL ( ), s2 s1.
:
char str1[81], str2[81], *p;
...
p = strpbrk(str1, str2);
, str1=a1d2, str2=4321, p

str1

,
1- ).
11. strstr - s1 s2.
:

char *strstr (char *s1, char *s2);

- s2 s1 NULL
( ), s2 s1.
,

s1="ab1111234cd1123",

s2= "1123",

1 s1,
"1123".
:
char text []= " 4172", *p;
if ((p = strstr(text, "4172" ))!=NULL)
strcpy(p, "4272"); /*

text = " 4272" */

112


1. (
).
2. - .
3.
, .
.
. -
, , ,
.
4. .

113