. ..
()
ѻ
..
140604
220301
( )
()
-
004
32.97
593
:
, ... ..
.. . . . , 2013, - 24.
.
..
.
充..
1. ꅅ
2. ⅅ..
..
" " , - " " . Turbo V3.0 BuilderC++ V5.0 ( V6.0) ++.
, , , , . .
, . . . .
++. .
|
|
: :
- ,
- ,
- ,
- ( ), ,
- (-).
1.
++ .
++ [ 10, 14], , , , () .
3. :
++ (), ( ), ++ . , (" ").
, '\0' ( , ). : "" ( ), , '' ( ), "" 2 .
++ ( ), char[]. , . . , " ". 1 , , '\0' (. 1).
. 1.
. '\' . '\' , .
++ :
- - ();
- ++ string.
- , 0 (-). string , , -. , :
- ;
- , string.h ( cstring).
.
:
1) ( );
2) () ;
3) .
, , , .
.
:
char <_>[<>]
, ( ):
|
|
const int len_s = 80;
char s[len_s]; // 79 + 0-
0-.
:
char *p = new char[m]; // p char
char *q = (char *)malloc(m * sizeof(char));
() .
1) :
:
char a[100] = ;
:
char str[10]={s, t, r, o, k, a};
, , :
char a[] = ; // 20
. ,
char *str = ;
, , , .
.
2) ( ).
- , ( : , , ) cin:
const int n = 80;
char s[n];
cin >> s;
- , , getline get istream, cin. , :
const int n = 80;
char s[n];
cin.getline(s,n); cout << s << endl;
cin.get(s,n); cout << s << endl;
getline (n-1) ( ) s. ( \n, Enter) () , -.
get , . -. get , \n . get :
cin.get(s,n); cout << s << endl;
cin.get(); // \n
cin.get(s,n); cout << s << endl;
- getline :
const int n = 80;
char s[n];
while (cin.getline(s,n)) {
cout << s << endl;
}
- ( ) scanf(), %s:
const int n = 80;
char s[n];
scanf(%s, s); printf(%s, s);
- %c :
scanf(%80, s);
- gets():
const int n = 80;
char s[n];
gets(s);
gets(s) s ( , -). s, NULL.
.
- () << ++:
const int n = 80;
char s[n];
cin >> s; cout << s << endl;
- printf(), , ; :
const int n = 80;
char s[n];
scanf(%s, s); printf(%s, s);
printf(%80s, s); //
|
|
- puts(s) s , - . EOF ( ) :
const int n = 80;
char s[n];
gets(s); puts(s);
- ++:
1) gets() puts():
1:
#include "stdafx.h"#include <iostream>using namespace std;int _tmain(int argc, _TCHAR* argv[]){ char s[20]; gets(s); puts(s); system("pause"); return 0;}
: "123 456 789" '\n', s "123 456 789\0" ( '\0' , ). puts() '\n', , "123 456 789\n" ( '\n' , ).
2. .
#include "stdafx.h"#include <iostream>using namespace std;int _tmain(int argc, _TCHAR* argv[]){ char st[100]; int i=0; puts(" :"); gets(st); while(st[i++]); printf(" = %i\n",i-1); system("pause"); return 0;
2) -:
3:
#include "stdafx.h"#include <iostream>using namespace std;int _tmain(int argc, _TCHAR* argv[]){ char s[20]; cin>>s; // cout<<s; // system("pause"); return 0;}
: "123 456 789" , s "123\0", , : "123".
4. 'A' 'a'. .
#include "stdafx.h"#include <iostream>using namespace std;int _tmain(int argc, _TCHAR* argv[]){ char st[80]; int i; cout << "\n : "; cin >> st; for(i=0;st[i]!='\0';i++) if (st[i]=='A') st[i]='a'; cout << "\n : "<< st; system("pause"); return 0;}3) -:
5:
#include "stdafx.h"#include <iostream>using namespace std;int _tmain(int argc, _TCHAR* argv[]){ char s[20]; scanf("%s",s); // & printf("%s",s); system("pause"); return 0;}
: "123 456 789", , s "123\0", , : "123". s , , & scanf .
6. .
#include "stdafx.h"#include <iostream>using namespace std;int _tmain(int argc, _TCHAR* argv[]){ char st[80]; char temp; int i,len=0; printf("\n > "); scanf("%s",st); while (st[len++]); // len-=2;// for(i=0;i<len;i++,len--){ temp=st[i]; // st[i]=st[len]; st[len]=temp; } printf("\n > %s",st); system("pause"); return 0;}.
|
|
. , stdlib.h string.h.
, , :
(); =();;
, ;
.
:
y=strlen(st); /* y st*/
.
, , .
++ int (unsigned), , 128 (255), .
. .
strcpy (s, "");
*s=0;
char s[10]="";
char s[10]="\0";,
.
( strncpy()) . - , 1 ( '\0').
1.
1.
stdlib.h | ||
atof | double atof (const char *str); | str double |
atoi | int atoi (const char *str); | str int |
atol | long atol (const char *str); | str long |
itoa | char *itoa (int v, char *str, int baz); | v str. baz (2<=baz<=36). baz =10 "" (). |
ltoa | char *ltoa (long v, char *str, int baz); | v str. baz (2<=baz<=36). |
ultoa | char *ultoa (unsigned long v, char *str, int baz); | v str |
string.h | ||
strcat | char *strcat (char *sp, const char *si); | si sp ( ) |
strchr | char *strchr (const char *str, int c); | str |
strcmp | int strcmp (const char *str1, const char *str2); | str1 str2. , str1<str2; , str1==str2, , str1>str2 ( ) |
strcpy | char *strcpy (char *sp, const char *si); | si sp |
strcspn | int strcspn (const char *str1, const char *str2); | str1, , str2 |
strdup | char *strdup (const char *str); | str |
strlen | unsignedstrlen (const char *str); | str |
strlwr | char *strlwr (char *str); | |
strncat | char * strncat ( char *sp, const char *si, int kol); | kol si sp () |
strncmp | int strncmp (const char *str1, const char *str2, int kol); | str1 str2, kol . , str1<str2; , str1==str2, , str1>str2 |
strncpy | char *strncpy (char *sp, const char *si, int kol); | kol si sp ("" ) |
strnicmp | int strnicmp (char *str1, const char *str2, int kol); | kol str1 str2, (. strncmp) |
strnset | char *strnset (char *str, int c, int kol); | kol str c |
strpbrk | char *strpbrk (const char *str1, const char *str2); | str1 , str2 |
strrchr | char *strrchr (const char *str, int c); | str |
strset | char *strset (char *str, int c); | str c |
strspn | int strspn (const char *str1, const char *str2); | str1, , str2 |
strstr | char *strstr (const char *str1, const char *str2); | str1 str2. str1, str2 |
strtod | double strtod (const char *str, char **endptr); | str . endptr NULL, *endptr , str |
strtok | char *strtok (char *str1, const char *str2); | str1 , |
strtol | long strtol (const char *str, char **endptr, int baz); | str " " baz (2<=baz<=36). endptr NULL, *endptr , str |
strupr | char *strupr (char *str); | str |
|
|
:
1) . , strcpy() strnpy(). strncpy() , , kol, - '\0'. .
char a[100] = ;
char *p = new char [m];
strcpy(p,a);// + \0
strncpy(p,a,strlen(a)+1); //
//(strlen(a)+1)
<string.h>.
strlen(a) -.
2) atoi(), atol(), atof().
:
char a[]= 10) 162 , 59.5 ;
int num;
long rost;
double ves;
num = atoi(a);
rost = atol(&a[11]);
ves = atof(&a[25]);
cout << num << << rost << << ves;
3) ( ) sizeof .
// #include "stdafx.h"#include <iostream>using namespace std;int _tmain(int argc, _TCHAR* argv[]) { char s1[10]="string1"; int k=sizeof(s1); cout<<s1<<"\t"<<k<<"\n"; char s2[]="string2"; k=sizeof(s2); cout<<s2<<"\t"<<k<<"\n"; char s3[]={'s','t','r','i','n','g','3','\0'}; /* '\0' , */ k=sizeof(s3); cout<<s3<<"\t"<<k<<"\n"; char *s4="string4"; // , k=sizeof(s4); cout<<s4<<"\t"<<k<<"\n"; system("pause"); return 0;}:
string1 10 10 , '\0'
string2 8 8 (7 + 1 '\0')
string3 8 8 (7 + 1 '\0')
4) strcmp() , . .
, . , . , , .
1.
// stdlib.h #include "stdafx.h"#include <iostream>using namespace std;int _tmain(int argc, _TCHAR* argv[]){ char sv[]="23.547", si[]="1234", sl[]="-112424", st1[15],st2[25],st3[15]; double v; int i; long l,t=457821; v=atof(sv); printf(" = %f\n",v); i=atoi(si); printf(" = %d\n",i); l=atol(sl); printf(" = %ld\n",l); printf(" = %s\n", ultoa(t,st1,10)); printf(" = %s\n", ultoa(t,st2,2)); printf(" = %s\n", ultoa(t,st3,16)); system("pause"); return 0;}2.
// string.h #include "stdafx.h"#include <iostream>using namespace std;int _tmain(int argc, _TCHAR* argv[]){ char st[50],sp[100],str[]="", si[]="qwerty", sl[]="qwerty", sw[]="qwertyu"; int len=0, sravn1, sravn2, sravn3, kol=5; printf(" : "); gets(st); len=strlen(st); printf(" = %d\n",len); printf(" : %s\n",strcat(st,"12cdb")); sravn1=strcmp(si,sl); printf(" : %s==%s %d\n", si, sl, sravn1); sravn2=strcmp(si,sw); printf(" : %s<%s %d\n",si, sw, sravn2); sravn3=strcmp(sw,si); printf(" : %s>%s %d\n", sw, si, sravn3); printf(" : %s\n", strcpy(sp,st)); printf(" : %s\n", strupr(st)); printf(" : %s\n", strlwr(st)); printf(" %d : %s\n", kol,strncpy(str,st,kol)); printf(" : %s\n",strpbrk(st,si)); printf(" : %s\n",strrchr(st,'t')); system("pause"); return 0;}
3.
// #include "stdafx.h"#include <iostream>using namespace std;int _tmain(int argc, _TCHAR* argv[]){ char st[80]; int i,j,flag,len; printf(" : "); gets(st); len=strlen(st); // printf(" : "); for (i=0;i<len;i++){ flag=0; // for (j=0;j<i;j++) // if (st[i]==st[j]) flag=1; if (flag==0)printf("%c",st[i]); } system("pause"); return 0;}4.
/* */#include "stdafx.h"#include <iostream>using namespace std;int _tmain(int argc, _TCHAR* argv[]){ char st[80]; int i=0,len; printf(" : "); gets(st); len=strlen(st);// printf(" : "); // while (st[i++]==' '); // for (--i;i<len;i++) if ((st[i]!=' ')||(st[i+1]!=' ')) printf("%c",st[i]); // system("pause"); return 0;}
++ . , , . ++ , <>, while(*str) , .
. .
, .
, , , .
char.
.
.
.
, , .
on_load_lecture();
:
/* s2 s1*/
char * strcpy_my (char *s1, char *s2){
char *ptrs1 = s1;
//
while ((*s1++ = *s2++)!= 0);
return ptrs1; // s1
}
, (-) .
/* */
char * strcat_my (char *s1, char *s2) {
char *p1, *p2;
p1 = s1; p2 = s2;
while (*p1!= '\0') p1++; // 1- .
// while (*p1) p1++;
while ((*p1 = *p2)!= 0) {
/* 2,
*/
p1++;
p2++; //
} /* : while ((*p1++ = *p2++)!= 0);
!=0 !=NULL /*
return s1;
}
1.
/* */
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[]){
char string[100], temp[100], *result, simvol;
int numresult, res;
/* "computer program"
strcpy strcat*/
strcpy(string, "computer");
result = strcat(string," program");
printf("1) \n%s\n",result);
/* , 'a'*/
simvol='a';
result = strchr(string,simvol);
printf("2) \'%c\'\n
%s\n",simvol,result);
/* */
result = strcpy(temp,string);
printf("3) \n%s\n",result);
/* "a","b","c" */
strcpy(string,"xyzabbc");
res = strcspn(string,"abc");
printf("4) \n%d\n",res);
/*
*/
result = strdup(string);
printf("5) \n%s\n",result);
system("pause");
return 0;
}
. '&' . . , :
2.
/* */
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[]){
char s[80], *p;
int i;
printf(" : ");
gets(s);
/* */
for(i=0; s[i] && s[i]!=' '; i++);
p = &s[i];
printf(p);
system("pause");
return 0;
}
p , , , . p , . , < ++>, printf() < ++>. p , .
3:
//
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[]){
char text[100],*p, *razd=".,";
int dlina;
puts (" ");
gets(text);
p=strtok(text,razd); //
while (p) { //
dlina=strlen(p); //
cout << "\n "<< p << " = " << dlina <<"\n";
p=strtok(NULL,razd);
// , , ..
}
system("pause");
return 0;
}
:
, .
char.
, . , , "", .
, . const : const char *p;.
, , , . .
. , .
:
char text[50]=" ";
char *p=text, *pp;
// text
pp=p;
// text
:
1) ,
2) ,
3) , .
, .
.
char. -, .
>> <<, get() get(char):
#include <iostream.h>
int main(){
char c, d, e;
cin >> c; //
cin >> d >> e;
cout << c << d << e;
c = cin.get(); //
cin.get(d); cin.get(e); // d
cout << c << e;
return 0;}
<stdio.h> (getchar()) (putchar()):
getchar() ( ) . 1 () int. , .
putchar(ch) , ch. ( , ), , .
char c, d;
c = getchar();
d = getchar(); putchar(d);
:
/* , */#include "stdafx.h"#include <iostream>using namespace std;int _tmain(int argc, _TCHAR* argv[]){ char ch; ch=getchar(); putchar(ch); system("pause"); return 0;}. , , , ( ).
#include "stdafx.h"#include <iostream>using namespace std;int _tmain(int argc, _TCHAR* argv[]){ char z; //z - int k; //k - printf(" :\n"); for (k=0; (z=getchar())!='.';) /* z=getchar() , , */ if (z!=' ') k++; printf("\n =%d",k); system("pause"); return 0;}
:
:1 2 3 4 5 6 7 8 9 0. =10, , :
1. .
, , . 80 , , .
, . 81 ( -).
, , 80 , . , 81 .
, .
#include <string.h>
int main(){
const int len = 81; //
char word[len], line[len]; //
//
cout<< 80 ;
cin.getline(line, len);
cout<< ; cin >> word;
if (strstr(line, word)) //
cout<<! << endl;
else
cout<<! << endl;
return 0; }
2. .
, . 80 . .
, , . . .
, . :
=
{ | | }
,
{ | | }
, , , .
, , , (, , , ..). , , , , . .
...
int main(){
const int len = 81;
char word[len], line[len];
cout<< 80 ;
cin.getline(line, len);
cout<< ; cin >> word;
int l_word = strlen(word); //
int count = 0; //
char *p = line; //
while(p = strstr(p, word)){
char *c = p; //
p += l_word; //
//
// ?
if (c!= line)
// ?
if(!ispunct(*(c-1)) &&!isspace(*(c-1)) continue;
// ?
if(ispunct(*p) || isspace(*p) || (*p ==\0)) count++;
}
cout << - << count << endl;
return 0;
}
. , , , <ctype.h>. (-).
strstr(). , . , .
. ++ strtok(), () . , , strtok(). .
...
int main(){
const int len = 81;
char word[len], line[len];
cout<< 80 ;
cin.getline(line, len);
cout<< ; cin >> word;
char razdel[] = ,.!/?<>)(|\*;:; //
int count = 0; //
char *token;
token = strtok(line, razdel); // 1-
while(token!= NULL){
/* strtok NULL , -
*/
if(!strcmp(token, word)) count++; //
token = strtok(NULL, razdel); //
}
cout << - << count << endl;
return 0;
}
.
++, .
.
5. :
:
1) ;
2) .
.
1., .
2. , , .
3. , , .
4. , ( ) .
5. , .
6. . 20 .
7. , .
8. , 60 , lat tal.
9. , , . , .
10. , , .
11. ( ) . .
12. , .
13. , .
14. .
15. .
16. , .
17. .
18. .
19. .
20. .
21. .
22. , .
23. .
24. ( ) , , .
25. 09 , , , .
26. , .
27. , .
28. .
29. , .
30. .
.
1. ?
2. .
3. ?
4. / ?
5. ?
2.