bölüm 8 - karakterler ve dizgiler
DESCRIPTION
Bölüm 8 - Karakterler ve Dizgiler. İndex 8.1Giri ş 8.2Dizgiler ve Karakterler in kuralları 8.3 K arakter İşleme Kütüphane si 8.4Dizgi Değiştirme Fonksiyonlar ı 8.5Standar t Input/Output Kütüphane Fonksiyonlar ı 8.6Dizgi işleme Kütüphane sinin Dizgi işletme Fonksiyonlar ı - PowerPoint PPT PresentationTRANSCRIPT
1
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
Bölüm 8 - Karakterler ve Dizgiler
İndex8.1 Giriş8.2 Dizgiler ve Karakterlerin kuralları8.3 Karakter İşleme Kütüphanesi8.4 Dizgi Değiştirme Fonksiyonları8.5 Standart Input/Output Kütüphane Fonksiyonları8.6 Dizgi işleme Kütüphanesinin Dizgi işletme Fonksiyonları 8.7 Dizgi Karşılaştırma Fonksiyonları8.8 Dizgi işleme Kütüphanesinin Arama Fonksiyonları8.9 Dizgi işleme Kütüphanesinin Hafıza Fonksiyonları 8.10 Dizgi işleme Kütüphanesinin Diğer Fonksiyonları
2
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
Amaçlar
• Bu Bölümde Öğreneceklerimiz:– Karakter işleme Kütüphanesinin Fonksiyonlarını
kullanabilmek (ctype).
– Standart giriş/çıkış Kütüphanesinin (stdio) Dizgi ve karakter input/output Fonksiyonlarını kullanabilmek
– Standart Kütüphanesinin (stdlib) Dizgi Değiştirme Fonksiyonlarını kullanabilmek
– Dizgi işleme Kütüphanesinin (Dizgi/String) Dizgi işlem Fonksiyonlarını kullanabilmek
– Fonksiyon kütüphanelerinin gücünü yazılımların tekrar kullanılabilmesi olarak anlayabilme.
3
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
8.1 Giriş
• Bazı Standart Kütüphane Fonksiyonları tanıma– KolayDizgi ve karakter işlemleri
– Programlar Karakterleri, Dizgileri, metinleri, ve Hafıza bloklarını işleyebilirler
• Bu teknikler – Kelime işlemcileri
– Sayfa planı yazılımları
– Dizgicilik programları için kullanılırlar
4
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
8.2 Dizgiler ve Karakterler Kuralları
• Karakterler– Program bloklarını inşa etme
• Her program anlamlı Karakter gruplarının birbiri ardına gelmesidir
– karakter sabiti• Bir int değeri tek tırnak içerisinde karakter olarak referans edilir• 'z' , z’ nin (int) tamsayı değerini temsil eder
• Dizgiler– Karakterlerin serileri tek birim olarak ele alınır
• Harfleri, sayıları ve özel Karakterleri (*, /, $) içerebilir
– Dizgi gerçeği /literatürü (Dizgi sabiti) – çift tırnak içerisinde yazılır• “Merhaba"
– Dizgiler Karakterlerin dizileridir• Dizgi , ilk karakteri gösteren bir göstergeçtir
• Dizginin değeri ilk karakterin adresidir
5
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
8.2 Kurallar of Dizgiler ve Karakterler
• Dizgi Tanımlamaları– Define as bir karakter dizisi veya bir char * tipinin
değişkeni olarak tanımlamachar color[] = “mavi";char *colorPtr = “mavi”;
– Dizgilerin, sonu '\0' ile iten karakter dizileri olarak temsil edildiğini hatırlayın• color 5 elemana sahiptir
• Dizgilere Giriş yapma– Scanf kullanımı
scanf("%s", word);
• Kopyaları word[] içerisine yerleştirir
• & ihtiyaç yoktur (çünkü bir Dizgi bir göstergeçtir)
– '\0‘ için dizideki yerini terk etmesini hatırlayın
6
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
8.3 karakter işleme Kütüphane
• Karakter işleme Kütüphanesi– Faydalı testleri ve karakter verilerinin kullanımlarını
uygulamak için Fonksiyonlar içerir
– Her Fonksiyon, bir karakter (bir int) veya EOF argüman olarak içerir
• Bir sonraki slayt bütün fonksiyonların bir tablosunu içeriyor <ctype.h>
7
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
8.3 karakter işleme Kütüphane
Prototipi Tanımılamaları
int isdigit( int c ); Eğer C bir sayı ise true diğer halde false döndürür int isalpha( int c ); Eğer C bir harf ise true diğer halde false döndürür int isalnum( int c ); Eğer C bir sayı veya harf ise true diğer hallerde false döndürür int isxdigit( int c ); Eğer C bir hexadecimal sayı veya karakter ise true diğer halde false döndürür int islower( int c ); Eğer C bir küçük harf ise true diğer halde false döndürür int isupper( int c ); Eğer C bir büyük harf veya karakter ise true diğer halde false döndürür int tolower( int c ); Eğer c bir büyük harf ise, tolower c yi bir küçük harf olarak geriye döndürür diğer
hallerde, tolower değişmeyen argümanı geriye dödürür. int toupper( int c ); Eğer c bir küçük harf ise, toupper c yi büyük harf olarak geriye dödürür diğer hallerde,
toupper değişmeyen argümanı geriye dödürür. int isspace( int c ); Eğer c bir beyaz-boşluk karakteri, —yeni satır('\n'), konum ilerletme('\f'), saryo
dönüsü('\r'), yatay tab('\t'), düşey tab('\v')— ise geriye true diğer hallerde false döndürür.
int iscntrl( int c ); Eğer c bir kontrol karakteri ise true diğer halde false döndürür. int ispunct( int c ); Eğer c bir baskı karakteri ‘boşluk, sayı, veya harf ‘ten farklı’ ise true diğer halde false
döndürür int isprint( int c ); Eğer c bir baskı karakteri ‘boşluk karakteri(' ') içeren’ ise true diğer halde false
döndürür. int isgraph( int c ); Eğer c bir baskı karakteri ‘boşluk karakteri(' ') içermeyen’ ise true diğer halde false
döndürür.
İndex8
İndex
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
fig08_02.c (Part 1 of 2)
1 /* Fig. 8.2: fig08_02.c
2 isdigits, isalpha, isalnum, and isxdigit fonksiyonlarının kullanımı */
3 #include <stdio.h>
4 #include <ctype.h>
5
6 int main()
7 {
8 printf( "%s\n%s%s\n%s%s\n\n", "isdigit’e göre: ",
9 isdigit( '8' ) ? "8 is a " : "8 is not a ", "digit",
10 isdigit( '#' ) ? "# is a " : "# is not a ", "digit" );
11
12 printf( "%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n",
13 "isalpha’e göre:",
14 isalpha( 'A' ) ? "A, a harfidir " : "A, a harfi değildir "
15 isalpha( 'b' ) ? "b, a harfidir " : "b, a harfi değildir "
16 isalpha( '&' ) ? "&, a harfidir " : "&, a harfi değildir "
17 isalpha( '4' ) ? "4, a harfidir " : "4, a harfi değildir ");
18
19 printf( "%s\n%s%s\n%s%s\n%s%s\n\n",
20 "isalnum’e göre:",
21 isalnum( 'A' ) ?"A,a sayı veya harfidir":"A, a sayı veya harfi değildir",
22
23 isalnum( '8' ) ?"8,a sayı veya harfidir ":"8, a sayı veya harfi değildir",
24
25 isalnum( '#' ) ?"#,a sayı veya harfidir ":"#, a sayı veya harfi değildir");
26
27
İndex9
İndex
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
fig08_02.c (Part 2 of 2)
28 printf( "%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n",
29 "isxdigit’e göre:",
30 isxdigit( 'F' ) ? "F, hexadecimal sayidir " : "F, a hexadecimal
31 sayi değildir",
32 isxdigit( 'J' ) ? "J, hexadecimal sayidir " : "J, a hexadecimal
33 sayi değildir",
34 isxdigit( '7' ) ? "7, hexadecimal sayidir " : "7, a hexadecimal
35 sayi değildir ",
36 isxdigit( '$' ) ? "$, hexadecimal sayidir " : "$, a hexadecimal
37 sayi değildir ",
38 isxdigit( 'f' ) ? "f, hexadecimal sayidir " : "f, a hexadecimal
39 sayi değildir ",);
40
41 return 0;
42
43 } /*main fonksiyonunun sonu*/
İndex10
İndex
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
Program Output
isdigit’e göre:8 , sayisidir# , sayisi değildir isalpha ’e göre :A , harfidirb , harfidir& , harfi değildir4 , harfi değildir isalnum ’e göre :A , sayi veya harfdir8 , sayi veya harfdir# , sayi veya harfi değildir isxdigit ’e göre :F , hexadecimal sayidirJ , hexadecimal sayi değildir7 , hexadecimal sayidir$ , hexadecimal sayi değildirf , hexadecimal sayidir
İndex11
İndex
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
fig08_03.c (Part 1 of 2)
1 /* Fig. 8.3: fig08_03.c
2 Using functions islower, isupper, tolower, toupper */
3 #include <stdio.h>
4 #include <ctype.h>
5
6 int main()
7 {
8 printf( "%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n",
9 "islower ‘e göre:",
10 "p, küçük harfi", islower( 'p' ) ? "dir " : "değildir ",
11
12 "P, küçük harfi", islower( 'P' ) ? "dir " : " değildir ",
13
14 "5, küçük harfi", islower( '5' ) ? "dir " : " değildir ",
15 "lowercase letter",
16 "!, küçük harfi", islower( '!' ) ? "dir" : " değildir ",
17 );
18
19 printf( "%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n",
20 "isupper ‘e göre:",
21 "D büyük harf“, isupper( 'D' ) ? " dir ”: "değildir ",
22
23 "d büyük harf“, isupper( 'd' ) ? "dir " : "değildir ",
24
25 "8 büyük harf“, isupper( '8' ) ? "dir " : "değildir ",
26
27 "$ büyük harf“, isupper( '$' ) ? "dir " : "değildir ",
28 );
29
İndex12
İndex
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
fig08_03.c (Part 2 of 2)
Program OutputIslower ‘e göre:p küçük harftirP küçük harf değildir5 küçük harf değildir! küçük harf değildir
isupper ‘e göre :D büyük harftird büyük harf değildir8 büyük harf değildir$ büyük harf değildir U, büyük harf U’ya dönüştürüldü7, büyük harf 7’ye dönüştürüldü$, büyük harf $’a dönüştürüldüL, küçük harf l’ye dönüştürüldü
30 printf( "%s%c\n%s%c\n%s%c\n%s%c\n",
31 "u, büyük harf", toupper( 'u' ), " ya dönüştürüldü",
32 "7, büyük harf", toupper( '7' ), " ye dönüştürüldü",
33 "$, büyük harf", toupper( '$' ), " a dönüştürüldü",
34 "L, küçük harf", tolower( 'L' ), " ye dönüştürüldü", );
35
36 return 0;
37
38 }
İndex13
İndex
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
fig08_04.c (Part 1 of 2)
1 /* Fig. 8.4: fig08_04.c
2 Using functions isspace, iscntrl, ispunct, isprint, isgraph */
3 #include <stdio.h>
4 #include <ctype.h>
5
6 int main()
7 {
8 printf( "%s\n%s%s%s\n%s%s%s\n%s%s\n\n",
9 " isspace ‘e göre:", "yenisatir“ ,
10 “beyaz-boşluk karakteri ", isspace( '\n' ) ? "dir " : "değidir ",
11 "yatay tab",
12 “beyaz-boşluk karakteri ", isspace( '\t' ) ? "dir " : "değildir ",
13
14 “% beyaz-boşluk karakteri ", isspace( '%' ) ? "dir" : "değildir",
15 );
16
17 printf( "%s\n%s%s%s\n%s%s\n\n", "iscntrl ‘e göre:",
18 "yeni satir", "kontrol karakteri", iscntrl( '\n' ) ? "dir " : "değildir ",
19 "$ kontrol karakteri,", iscntrl( '$' ) ? "dir " :
20 "değildir ");
21
İndex14
İndex
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
fig08_04.c (Part 2 of 2)
22 printf( "%s\n%s%s\n%s%s\n%s%s\n\n",
23 "ispunct’e göre :",
24 "; noktalı virgül karakteri",ispunct( ';' ) ? "dir" : "değildir ",
25
26 "Y noktalı virgül karakteri",ispunct( 'Y' ) ? "dir” : "değildir ",
27
28 "# noktalı virgül karakteri ",ispunct( '#' ) ? "dir" : "değildir ",
29 );
30
31 printf( "%s\n%s%s\n%s%s%s\n\n", "isprint’e göre:",
32 "$ baskı karakteri",isprint( '$' ) ? "dir " : "değildir",
33
34 "Alert, baskı karakteri", isprint( '\a' ) ? "dir " : "değildir ",
35 );
36
37 printf( "%s\n%s%s\n%s%s%s\n", "isgraph’e göre:",
38 "Q boşlutan farklı baskı karakteri” ,isgraph( 'Q' ) ? "dir" : "değildir",
39
40 "Space boşlutan farklı baskı karakteri”, isgraph( ' ' ) ? "dir" : "değildir",
41 );
42
43 return 0;
44
45 }
İndex15
İndex
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
Program Output
Isspace’e göre:Yeni satir bir beyaz-bosluk karakteridiryatay tab is a beyaz-bosluk karakter% is not a beyaz-bosluk karakter Iscntrl’e göre:yeni satir kontrol karakterdir$ kontrol karakteri değildir Ispunct’e göre:; noktalı virgül karakteridirY noktalı virgül karakter değildir# noktalı virgül karakter değildir Isprint’e göre:$ baski karakterdirAlert baski karakteri değildir Isgraph’e göre:Q boşluk karakterinden farklı baski karakteriSpace boşluk karakterinden farklı baski karakteri değildir
16
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
8.4 Dizgi Değiştirme Fonksiyonlar
• Değiştirme Fonksiyonlar– In<stdlib.h> (Standart Kütüphanesi)
• Sayıların Dizgilerini tamsayi ve virgüllü sayı değerlerine çevirme
Fonksiyon Prototipi Fonksiyon Tanımları double atof( const char *nPtr ); nPtr dizgisini double’a çevirir
int atoi( const char *nPtr ); nPtr dizgisini int’e çevirir long atol( const char *nPtr ); nPtr to long int’e çevirir double strtod( const char *nPtr, char **endPtr );
nPtr to double’a çevirir
long strtol( const char *nPtr, char **endPtr, int base );
nPtr to long’ a çevirir
unsigned long strtoul( const char *nPtr, char **endPtr, int base );
nPtr to unsigned long’a çevirir
İndex17
İndex
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
1 /* Fig. 8.6: fig08_06.c
2 Using atof */
3 #include <stdio.h>
4 #include <stdlib.h>
5
6 int main()
7 {
8 double d;
9
10 d = atof( "99.0" );
11
12 printf( "%s%.3f\n%s%.3f\n",
13 " \"99.0\" dizgisini double ", d, "tutar " ,
14 "tutulan değerin 2 ye bölümü ",
15 d / 2.0 );
16
17 return 0;
18
19 }
fig 08_06.c
Program OutputDizgi "99.0“, 99.000 olarak tutulurTutulan değerin 2’ye bölümü 49.500
İndex18
İndex
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
1 /* Fig. 8.7: fig08_07.c
2 atoi kullanımı */
3 #include <stdio.h>
4 #include <stdlib.h>
5
6 int main()
7 {
8 int i;
9
10 i = atoi( "2593" );
11
12 printf( "%s%d\n%s%d\n",
13 "\"2593\" dizgisi (int) tamsayisina", i, “dönüştürüldü" ,
14 "dönüstürülen değer eksi 593 ", i - 593 );
15
16 return 0;
17
18 }
fig08_07.c
Program Output2593 dizgisi (int) tamsayisina 2593’e dönüstürüldü dönüstürülen deger eksi 593 esittir 2000
İndex19
İndex
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
1 /* Fig. 8.8: fig08_08.c
2 atol kullanımı */
3 #include <stdio.h>
4 #include <stdlib.h>
5
6 int main()
7 {
8 long l;
9
10 l = atol( "1000000" );
11
12 printf( "%s%ld\n%s%ld\n",
13 "\"1000000\" dizgisi long’a ", l, “olarak dönüstürüldü"
14 "dönüstürülen degerin 2’ye bölümü ", l / 2 );
15
16 return 0;
17
18 }
fig08_08.c
Program Output“1000000" dizgisi long’a 1000000 olarak dönüstürüldü dönüstürülen degerin 2’ye bölümü 500000
İndex20
İndex
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
1 /* Fig. 8.9: fig08_09.c
2 strtod kullanımı */
3 #include <stdio.h>
4 #include <stdlib.h>
5
6 int main()
7 {
8
9 const char *string = "51.2% kabul edilir";
10
11 double d;
12 char *stringPtr;
13
14 d = strtod( string, &stringPtr );
15
16 printf( " \"%s\"dizgisi \n”, string, ’e dönüstürüldü" );
17 printf( "double %.2f degeri ve \"%s\" dizgisi \n", d, stringPtr );
18
19 return 0;
20
21 }
fig08_09.c
Program Output"51.2% kabul edilir" Dizgisi double deger 51.20 ve "% kabul edilir“ Dizgisine dönüstürülmüstür
İndex21
İndex
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
1 /* Fig. 8.10: fig08_10.c
2 Using strtol */
3 #include <stdio.h>
4 #include <stdlib.h>
5
6 int main()
7 {
8 const char *string = "-1234567abc";
9
10 char *remainderPtr;
11 long x;
12
13 x = strtol( string, &remainderPtr, 0 );
14
15 printf( "%s\"%s\"\n%s%ld\n%s\"%s\"\n%s%ld\n",
16 "Orjinal dizgi ", string, "dir”,
17 "Dönüstürülen değer ", x, "dir” ,
18 "Orjinal dizginin kalanı ",
19 remainderPtr, "dir”,
20 "Dönüstürülen değer artı 567 ", x + 567, "dir” );
21
22 return 0;
23
24 }
fig08_10.c
Program OutputOrjinal dizgi "-1234567abc" dirDönüstürülen değer -1234567 dirOrjinal dizginin kalanı "abc“ dirDönüstürülen değer artı 567 -1234000 dir
İndex22
İndex
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
1 /* Fig. 8.11: fig08_11.c
2 Using strtoul */
3 #include <stdio.h>
4 #include <stdlib.h>
5
6 int main()
7 {
8 const char *string = "1234567abc";
9 unsigned long x;
10 char *remainderPtr;
11
12 x = strtoul( string, &remainderPtr, 0 );
13
14 printf( "%s\"%s\"\n%s%ld\n%s\"%s\"\n%s%ld\n",
15 "Orjinal dizgi ", string, "dir”,
16 "Dönüstürülen değer ", x, "dir” ,
17 "Orjinal dizginin kalanı ",
18 remainderPtr, "dir”,
29 "Dönüstürülen değer artı 567 ", x + 567, "dir” );
20
21 return 0;
22
23 }
fig08_11.c
Program OutputOrjinal dizgi "-1234567abc" dirDönüstürülen değer -1234567 dirOrjinal dizginin kalanı "abc“ dirDönüstürülen değer artı 567 -1234000 dir
23
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
8.5 Standard Input/Output Kütüphane Fonksiyonları
• <stdio.h> Fonksiyonları• karakter ve veri dizgisi kullanma
Function prototype Function description int getchar( void ); Standart input’tan bir sonraki karakteri girer ve onu tam
sayı olarak geri dödürür
char *gets( char *s ); Yenibir satır veya EOF son karaktere ulaşıncaya kadar Karakterleri Standart input’tan s dizisine gönderir. Diziye bir işlemi durdurma (null) karakteri gönderilir.
int putchar( int c ); c de tutulan değeri yazar int puts( const char *s ); Yeni satır karakterinden sonra gelen s dizgisini yazar int sprintf( char *s, const char *format, ... );
Printf ile yanıdır, yalnız çıkış ekran yerine s dizisine yönlendirilmiştir..
int sscanf( char *s, const char *format, ... );
Scanf ile aynıdır, yalnız giriş klavye yerine s dizisinden alınır.
İndex24
İndex
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
fig08_13.c (Part 1 of 2)
1 /* Fig. 8.13: fig08_13.c
2 gets and putchar kullanımı */
3 #include <stdio.h>
4
5 int main()
6 {
7 char sentence[ 80 ];
8
9 void reverse( const char * const sPtr );
10
11 printf( "bir metin girin:\n" );
12
13
14 gets( sentence );
15
16 printf( "\n metnin terden yazılımı:\n" );
17 reverse( sentence );
18
19 return 0;
20
21 }
22
İndex25
İndex
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
fig08_13.c (Part 1 of 2)
Program OutputBir metin girin:Karakterler ve Dizgiler Metnin tersten yazılımı:religziD ev relretkaraK
Bir metin girin :able was I ere I saw elba Metnin tersten yazılımıable was I ere I saw elba
23
24 void reverse( const char * const sPtr )
25 {
26
27 if ( sPtr[ 0 ] == '\0' ) {
28 return;
29 }
30 else {
31 reverse( &sPtr[ 1 ] );
32
33 putchar( sPtr[ 0 ] );
34 }
35
36 }
İndex26
İndex
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
fig8_14.c
1 /* Fig. 8.14: fig08_14.c
2 getchar and puts kullanımı */
3 #include <stdio.h>
4
5 int main()
6 {
7 char c;
8 char sentence[ 80 ];
9 int i = 0;
10
11
12 puts( "Bir metin girin:" );
13
14
15 while ( ( c = getchar() ) != '\n') {
16 sentence[ i++ ] = c;
17 }
18
19 sentence[ i ] = '\0';
20
21
22 puts( "\ngirilen metin :" );
23 puts( sentence );
24
25 return 0;
26
27 }
İndex27
İndex
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
Program Output
Bir metin girin:Bu bir denemedir. Girilen metin:Bu bir denemedir.
İndex28
İndex
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
fig08_15.c
Program Outputbir (int) ve (double) sayi girin :298 87.375s dizisinde tutulan biçimlendirilmiş cıktı: :İnt : 298double: 87.38
1 /* Fig. 8.15: fig08_15.c
2 sprintf kullanımı*/
3 #include <stdio.h>
4
5 int main()
6 {
7 char s[ 80 ];
8 int x;
9 double y;
10
11 printf( "bir (int) ve (double) sayi girin:\n" );
12 scanf( "%d%lf", &x, &y );
13
14 sprintf( s, "int :%6d\ndouble:%8.2f", x, y );
15
16 printf( "%s\n%s\n",
17 "s dizisinde tutulan biçimlendirilmiş cıktı:", s );
18
19 return 0;
20
21 }
İndex29
İndex
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
1 /* Fig. 8.16: fig08_16.c
2 sscanf kullanımı */
3 #include <stdio.h>
4
5 int main()
6 {
7 char s[] = "31298 87.375";
8 int x;
9 double y;
10
11 sscanf( s, "%d%lf", &x, &y );
12
13 printf( "%s\n%s%6d\n%s%8.3f\n",
14 "s karakter dizisinde tutulan değerler:",
15 "int:", x, "double:", y );
16
17 return 0;
18
19 }
fig08_16.c
Program Outputs karakter dizisinde tutulan değerler :int : 31298double: 87.375
30
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
8.6 Dizgi işleme Kütüphanesinin Dizgi Elde etme Fonksiyonları
• Dizgi işleme Kütüphane şu Fonksiyonlara sahiptir– Veri Dizgisi elde etme
– Arama Dizgileri
– Dizgiyi mantıklı parçalara bölmek
– Dizgi boyu tanımlamaFunction prototype Function description char *strcpy( char *s1, const char *s2 )
Copies string s2 into array s1. The value of s1 is returned.
char *strncpy( char *s1, const char *s2, size_t n )
Copies at most n characters of string s2 into array s1. The value of s1 is returned.
char *strcat( char *s1, const char *s2 )
Appends string s2 to array s1. The first character of s2 overwrites the terminating null character of s1. The value of s1 is returned.
char *strncat( char *s1, const char *s2, size_t n )
Appends at most n characters of string s2 to array s1. The first character of s2 overwrites the terminating null character of s1. The value of s1 is returned.
İndex31
İndex
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
1 /* Fig. 8.18: fig08_18.c
2 strcpy and strncpy kullanımı */
3 #include <stdio.h>
4 #include <string.h>
5
6 int main()
7 {
8 char x[] = "Mutlu yıllar";
9 char y[ 25 ];
10 char z[ 15 ];
11
12
13 printf( "%s%s\n%s%s\n",
14 " x dizisindeki dizgi: ", x,
15 " y dizisindeki dizgi: ", strcpy( y, x ) );
16
17
18
19 strncpy( z, x, 14 );
20
21 z[ 14 ] = '\0';
22 printf( "z dizisindeki dizgi: %s\n", z );
23
24 return 0;
25
26 }
fig08_18.c
Program Outputx dizisindeki Dizgi: Mutlu yıllary dizisindeki Dizgi : Mutlu yıllarz dizisindeki Dizgi : Mutlu yıllar
İndex32
İndex
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
fig08_19.c
1 /* Fig. 8.19: fig08_19.c
2 strcat and strncat kullanımı */
3 #include <stdio.h>
4 #include <string.h>
5
6 int main()
7 {
8 char s1[ 20 ] = "Mutlu ";
9 char s2[] = "yeni yıllar ";
10 char s3[ 40 ] = "";
11
12 printf( "s1 = %s\ns2 = %s\n", s1, s2 );
13
14
15 printf( "strcat( s1, s2 ) = %s\n", strcat( s1, s2 ) );
16
17
18
19 printf( "strncat( s3, s1, 6 ) = %s\n", strncat( s3, s1, 6 ) );
20
21
22 printf( "strcat( s3, s1 ) = %s\n", strcat( s3, s1 ) );
23
24 return 0;
25
26 }
İndex33
İndex
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
Program Output
s1 = Mutlus2 = yeni yıllarstrcat( s1, s2 ) = Mutlu yeni yıllarstrncat( s3, s1, 6 ) = Mutlustrcat( s3, s1 ) = Mutlu Mutlu yeni yıllar
34
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
8.7 Dizgi işleme Kütüphanesinin Karşılaştırma Fonksiyonlar
• Karşılaştırma Dizgileri– Bilgisayar Dizgi içindeki Karakterlerin sayısal ASCII
kodlarını karşılaştırır
– Bölüm D karakter kodlarının bir listesine sahiptir
int strcmp( const char *s1, const char *s2 );
– s1 Dizgisini s2 ile karşılaştırır
– Eğer s1 < s2 ise bir negatif sayıyı, s1 == s2 ise sıfır veya s1 > s2 ise bir pozitif sayıyı geriye döndürür
int strncmp( const char *s1, const char *s2, size_t n );
– s1 ile s2 Dizginin n Karakterlerine kadar karşılaştırır
– Geriye yukarıdaki gibi değer döndürür.
İndex35
İndex
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
fig08_21.c
1 /* Fig. 8.21: fig08_21.c
2 strcmp and strncmp kullanımı*/
3 #include <stdio.h>
4 #include <string.h>
5
6 int main()
7 {
8 const char *s1 = "mutlu yeni yıllar";
9 const char *s2 = "mutlu yeni yıllar ";
10 const char *s3 = "mutlu tatiller";
11
12 printf("%s%s\n%s%s\n%s%s\n\n%s%2d\n%s%2d\n%s%2d\n\n",
13 "s1 = ", s1, "s2 = ", s2, "s3 = ", s3,
14 "strcmp(s1, s2) = ", strcmp( s1, s2 ),
15 "strcmp(s1, s3) = ", strcmp( s1, s3 ),
16 "strcmp(s3, s1) = ", strcmp( s3, s1 ) );
17
18 printf("%s%2d\n%s%2d\n%s%2d\n",
19 "strncmp(s1, s3, 6) = ", strncmp( s1, s3, 6 ),
20 "strncmp(s1, s3, 7) = ", strncmp( s1, s3, 7 ),
21 "strncmp(s3, s1, 7) = ", strncmp( s3, s1, 7 ) );
22
23 return 0;
24
25 }
İndex36
İndex
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
Program Output
s1 = Mutlu yeni yıllars2 = Mutlu yeni yıllars3 = Mutlu tatiller strcmp(s1, s2) = 0strcmp(s1, s3) = 1strcmp(s3, s1) = -1
strncmp(s1, s3, 6) = 0strncmp(s1, s3, 7) = 1strncmp(s3, s1, 7) = -1
37
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
8.8 Dizgi işleme Kütüphanesinin Arama Fonksiyonları
Function prototype Function description
char *strchr( const char *s, int c );
Locates the first occurrence of character c in string s. If c is found, a pointer to c in s is returned. Otherwise, a NULL pointer is returned.
size_t strcspn( const char *s1, const char *s2 );
Determines and returns the length of the initial segment of string s1 consisting of characters not contained in string s2.
size_t strspn( const char *s1, const char *s2 );
Determines and returns the length of the initial segment of string s1 consisting only of characters contained in string s2.
char *strpbrk( const char *s1, const char *s2 );
Locates the first occurrence in string s1 of any character in string s2. If a character from string s2 is found, a pointer to the character in string s1 is returned. Otherwise, a NULL pointer is returned.
char *strrchr( const char *s, int c );
Locates the last occurrence of c in string s. If c is found, a pointer to c in string s is returned. Otherwise, a NULL pointer is returned.
char *strstr( const char *s1, const char *s2 );
Locates the first occurrence in string s1 of string s2. If the string is found, a pointer to the string in s1 is returned. Otherwise, a NULL pointer is returned.
char *strtok( char *s1, const char *s2 );
A sequence of calls to strtok breaks string s1 into “tokens”—logical pieces such as words in a line of text—separated by characters contained in string s2. The first call contains s1 as the first argument, and subsequent calls to continue tokenizing the same string contain NULL as the first argument. A pointer to the current token is returned by each call. If there are no more tokens when the function is called, NULL is returned.
İndex38
İndex
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
fig08_23.c (Part 1 of 2)
1 /* Fig. 8.23: fig08_23.c
2 strchr kullanımı */
3 #include <stdio.h>
4 #include <string.h>
5
6 int main()
7 {
8 const char *string = "bu bir metindir";
9 char character1 = 'i';
10 char character2 = 'z';
11
12
13 if ( strchr( string, character1 ) != NULL ) {
14 printf( "\'%s\'icerisinde \"%c\" karaketeri bulundu.\n",
15 string, character1);
16 }
17 else {
18 printf( "\'%s\' icerisinde \"%c\"karakteri bulunamadı.\n",
19 string,character1);
20 }
21
İndex39
İndex
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
22
23 if ( strchr( string, character2 ) != NULL ) {
24 printf( "\'%s\'icerisinde \"%c\"karakteri bulundu.\n",
25 string, character2);
26 }
27 else {
28 printf("\'%s\'icerisinde \"%c\"karakteri bulunamadı.\n",
29 string, character2);
30 }
31
32 return 0;
33
34 }
fig08_23.c (Part 2 of 2)
Program Output“bu bir metindir“ icesinde ‘i’ karakteri bulundu.“bu bir metindir“ icesinde ‘z’ karakteri bulunamadı.
İndex40
İndex
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
1 /* Fig. 8.24: fig08_24.c
2 strcspn kullanımı */
3 #include <stdio.h>
4 #include <string.h>
5
6 int main()
7 {
8
9 const char *string1 = "deger 3.14159 dir";
10 const char *string2 = "1234567890";
11
12 printf( "%s%s\n%s%s\n\n%s\n%s%u",
13 "string1 = ", string1, "string2 = ", string2,
14 "string1’in baslangıc uzunlugu ",
15 "string2 den hiçbir karakter icermemektedir = ",
16 strcspn( string1, string2 ) );
17
18 return 0;
19
20 }
fig08_24.c
Program Output
string1 = deger 3.14159 dirstring2 = 1234567890 string1’in baslangıc uzunlugu string2 den hiçbir karakter icermemektedir = 13
İndex41
İndex
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
1 /* Fig. 8.25: fig08_25.c
2 strpbrk kullnamı */
3 #include <stdio.h>
4 #include <string.h>
5
6 int main()
7 {
8 const char *string1 = "bu bir metindir";
9 const char *string2 = "sakınmak";
10
11 printf( "%s\"%s\"\n'%c'%s\n\"%s\"\n",
12 string2, "’icindeki karakterlerden ",
13 *strpbrk( string1, string2 ),
14 string1, "’de görülen ilk karakterdir");
15
16 return 0;
17
18 }
fig08_25.c
Program Output“sakımak“icindeki karakterlerden m“bu bir metindir” ’de görülen ilk karakterdir
İndex42
İndex
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
1 /* Fig. 8.26: fig08_26.c
2 strrchr kullanımı*/
3 #include <stdio.h>
4 #include <string.h>
5
6 int main()
7 {
8
9 const char *string1 = "Hayvanat bahcesinde bir cok hayvan bulunur "
10 "including zebras";
11 int c = 'z'; /* initialize c */
12
13 printf( "%s\n%s'%c'%s\"%s\"\n",
14 "The remainder of string1 beginning with the",
15 "last occurrence of character ", c,
16 " is: ", strrchr( string1, c ) );
17
18 return 0; /* indicates successful termination */
19
20 } /* end main */
fig08_26.c
Program OutputThe remainder of string1 beginning with thelast occurrence of karakter 'z' is: "zebras"
İndex43
İndex
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
1 /* Fig. 8.27: fig08_27.c
2 strspn kullanımı */
3 #include <stdio.h>
4 #include <string.h>
5
6 int main()
7 {
8
9 const char *string1 = "The value is 3.14159 ";
10 const char *string2 = " aehi lsTuv ";
11
12 printf( "%s%s\n%s%s\n\n%s\n%s%u",
13 "string1 = ", string1, "string2 = ", string2,
14 "string1’in baslangıc uzunlugu ",
15 "string2 den sadece= “ , strcspn( string1, string2 ),
16 " karakter icermektedir " );
17
18 return 0;
19
20 }
fig08_27.c
Program Outputstring1 = The value is 3.14159string2 = aehi lsTuv
string1’in baslangıc uzunlugustring2 den sadece = 13 karakter icermektedir
İndex44
İndex
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
1 /* Fig. 8.28: fig08_28.c
2 strstr kullanımı */
3 #include <stdio.h>
4 #include <string.h>
5
6 int main()
7 {
8 const char *string1 = "abcdefabcdef";
9 const char *string2 = "def";
10
11 printf( "%s%s\n%s%s\n\n%s\n%s%s\n",
12 "string1 = ", string1, "string2 = ", string2,
13 "string1’in kalanı string2’nin ilk olusumu ile",
14 " baslıyor: ",
15 strstr( string1, string2 ) );
16
17 return 0;
18
19 }
fig08_28.c
Program Outputstring1 = abcdefabcdefstring2 = def string1’in kalanı string2’nin ilk olusumu ile baslıyor : defabcdef
İndex45
İndex
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
fig08_29.c
1 /* Fig. 8.29: fig08_29.c
2 strtok kullanımı */
3 #include <stdio.h>
4 #include <string.h>
5
6 int main()
7 {
8
9 char string[] = "bu 7 belirtke ile birlesmis bir cumledir ";
10 char *tokenPtr;
11
12 printf( "%s\n%s\n\n%s\n",
13 "dizginin belirtgeci :", string,
14 "belirkeler :" );
15
16 tokenPtr = strtok( string, " " );
17
18
19 while ( tokenPtr != NULL ) {
20 printf( "%s\n", tokenPtr );
21 tokenPtr = strtok( NULL, " " );
22 }
23
24 return 0;
25
26 }
İndex46
İndex
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
Program Output
Dizginin belirtgeci :Bu 7 belirtke ile birlesmis bir cumledir belirtkeler:Bu7belirtkeilebirleşmişbircumledir
47
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
8.9 Dizgi- işleme Kütüphanesinin Hafıza Fonksiyonları
• Hafıza Fonksiyonları– <stdlib.h> içinde
– İşleme, karşılaştırma, ve Hafıza’nın Arama blokları
– Herhangi veri bloklarını işleyebilir
• Gösterici parametreleri void * dir– Any pointer can be assigned to ve vice versa– Herhangi bir gösterici void * ve tam tersi olarak atanabilir
– void * cannot be dereferenced• Her Fonksiyon işlemciye bit (Karakterler) sayıları belirtilmiş
bir argümanı alır
48
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
8.9 Dizgi işleme Kütüphanesinin Hafıza Fonksiyonları
Function prototype Function description void *memcpy( void *s1, const void *s2, size_t n );
Copies n characters from the object pointed to by s2 into the object pointed to by s1. A pointer to the resulting object is returned.
void *memmove( void *s1, const void *s2, size_t n );
Copies n characters from the object pointed to by s2 into the object pointed to by s1. The copy is performed as if the characters were first copied from the object pointed to by s2 into a temporary array and then from the temporary array into the object pointed to by s1. A pointer to the resulting object is returned.
int memcmp( const void *s1, const void *s2, size_t n );
Compares the first n characters of the objects pointed to by s1 and s2. The function returns 0, less than 0 or greater than 0 if s1 is equal to, less than or greater than s2.
void *memchr( const void *s, int c, size_t n );
Locates the first occurrence of c (converted to unsigned char) in the first n characters of the object pointed to by s. If c is found, a pointer to c in the object is returned. Otherwise, NULL is returned.
void *memset( void *s, int c, size_t n );
Copies c (converted to unsigned char) into the first n characters of the object pointed to by s. A pointer to the result is returned.
İndex49
İndex
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
1 /* Fig. 8.31: fig08_31.c
2 memcpy kullanımı*/
3 #include <stdio.h>
4 #include <string.h>
5
6 int main()
7 {
8 char s1[ 17 ];
9 char s2[] = "Bu dizgiyi kopyala";
10
11 memcpy( s1, s2, 17 );
12 printf( "%s\n%s\"%s\"\n",
13 " memcpy ile s2 s1’e kopyalandıktan sonra,",
14 "s1 içerir ", s1 );
15
16 return 0;
17
18 }
fig08_31.c
Program Output
After s2 is copied into s1 with memcpy,s1 contains "Copy this Dizgi"
İndex50
İndex
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
1 /* Fig. 8.32: fig08_32.c
2 memmove kullanımı */
3 #include <stdio.h>
4 #include <string.h>
5
6 int main()
7 {
8 char x[] = "Home Sweet Home";
9
10 printf( "%s%s\n","Memmove ‘dan önce x dizisindeki Dizgi: ", x );
11 printf( "%s%s\n","Memmove ‘dan sonra x dizisindeki Dizgi: ",
12 memmove( x, &x[ 5 ], 10 ) );
13
14 return 0;
15
16 }
fig08_32.c
Program OutputMemmove ‘dan önce x dizisindeki Dizgi: Home Sweet HomeMemmove ‘dan sonra x dizisindeki Dizgi : Sweet Home Home
İndex51
İndex
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
1 /* Fig. 8.33: fig08_33.c
2 memcmp kullanımı */
3 #include <stdio.h>
4 #include <string.h>
5
6 int main()
7 {
8 char s1[] = "ABCDEFG";
9 char s2[] = "ABCDXYZ";
10
11 printf( "%s%s\n%s%s\n\n%s%2d\n%s%2d\n%s%2d\n",
12 "s1 = ", s1, "s2 = ", s2,
13 "memcmp( s1, s2, 4 ) = ", memcmp( s1, s2, 4 ),
14 "memcmp( s1, s2, 7 ) = ", memcmp( s1, s2, 7 ),
15 "memcmp( s2, s1, 7 ) = ", memcmp( s2, s1, 7 ) );
16
17 return 0;
18
19 }
fig08_33.c
Program Outputs1 = ABCDEFGs2 = ABCDXYZ memcmp( s1, s2, 4 ) = 0memcmp( s1, s2, 7 ) = -1memcmp( s2, s1, 7 ) = 1
İndex52
İndex
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
1 /* Fig. 8.34: fig08_34.c
2 memchr kullanımı */
3 #include <stdio.h>
4 #include <string.h>
5
6 int main()
7 {
8 const char *s = "bu bir dizgidir";
9
10 printf( "%s\'%c\'%s\"%s\"\n",
11 'g', " karakterinden sonra s den kalan ",
12 memchr( s, 'r', 16 ) );
13
14 return 0;
15
16 }
Fig8_34.c
Program Output‘g‘ karakterinden sonra s den kalan “gidir“
53
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
8.10 Dizgi işleme Kütüphanesinin Diğer Fonksiyonları
• char *strerror( int errornum ); – errornum temelli hata mesajına bağlı bir sistem oluşturur.
– Dizgiye bir gösterici döndürür
• size_t strlen( const char *s ); – Dizgisi içerisindeki Karakterlerin (NULL dan önce)
sayısını geriye döndürür
İndex54
İndex
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
fig08_35.c
Program Output
1 /* Fig. 8.35: fig08_35.c
2 memset kullnaımı */
3 #include <stdio.h>
4 #include <string.h>
5
6 int main()
7 {
8 char string1[ 15 ] = "BBBBBBBBBBBBBB";
9
10 printf( "string1 = %s\n", string1 );
11 printf( " memset’ten sonra string1= %s\n", memset( string1, 'b', 7 ) );
12
13 return 0;
14
15 }
string1 = BBBBBBBBBBBBBBmemset’ten sonra string1 = bbbbbbbBBBBBBB
İndex55
İndex
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
fig08_37.c
Program Output
No such file or directory
1 /* Fig. 8.37: fig08_37.c
2 strerror kullanımı */
3 #include <stdio.h>
4 #include <string.h>
5
6 int main()
7 {
8 printf( "%s\n", strerror( 2 ) );
9
10 return 0;
11
12 }
İndex56
İndex
© Copyright 1992–2004 by Deitel & Associates, Inc. ve Pearson Education Inc. All Rights Reserved.
1 /* Fig. 8.38: fig08_38.c
2 strlen kullanımı */
3 #include <stdio.h>
4 #include <string.h>
5
6 int main()
7 {
8
9 const char *string1 = "abcdefghijklmnopqrstuvwxyz";
10 const char *string2 = "four";
11 const char *string3 = "Boston";
12
13 printf("%s\"%s\"%s%lu\n%s\"%s\"%s%lu\n%s\"%s\"%s%lu\n",
14 string1, "’in uzunlugu ", " is ",
15 ( unsigned long ) strlen( string1 ),
16 string2, "’in uzunlugu ", " is ",
17 ( unsigned long ) strlen( string2 ),
18 string3, "’in uzunlugu ", " is ",
19 ( unsigned long ) strlen( string3 ) );
20
21 return 0;
22
23 }
fig08_38.c
Program Output"abcdefghijklmnopqrstuvwxyz" ’in uzunlugu 26"four“ ’in uzunlugu 4"Boston" ’in uzunlugu 6