Qator toifali o’zgaruvchilar bilan ishlash C/C++ tilida qator toifali o’zgaruvchilar ustida bir qancha amallar bajarish mumkin. Buning uchun avvalo # include ulanishi kerak. 1.a) 2ta qator tipli o’zgaruvchilarni bir-biriga ulash strcat (s1, s2); (s2 qatorni s1 qatorga ulash) b) 2-qatorning n ta simvolini 1-qatorga ulash strncat (s1, s2, n); (s2 qatorning n ta simvolini s1 qatorga ulash) Masalan: # include < string.h > # include < iostream.h> void main( ) { char s1[10] = ”Gul”, s2[10] = “zoda”; strcat (s1, s2); cout << s1 << endl; // yoki cout << strcat (s1, s2)<< endl; } Натижа; Gulzoda Агар cout << strcat (s2, s1)<< endl; деб езсак натижа zodaGul чикади. # include < string.h > # include < iostream.h> void main( ) { char s1[10] = ”Gul”, s2[10] = “bahor”; strncat (s1, s2, 3); cout << s1 << endl; // yoki cout << strncat (s1, s2, 3)<< endl; } Натижа: Gulbah 2. a) Bir qatorning nusxasini ikkinchi qatorga qo’yish. strcpy ( s1, s2); b) 2-qatordan n ta simvolni 1-qatorga nusxasini qo’yish strncpy (s1, s2, n); Masalan: # include < string.h > # include < iostream.h> void main( ) { char s1[10] = ” Dilorom ”, s2[10] = “ Gul ”; strncpy (s1, s2, 3); cout << s1 << endl; // yoki cout << strncpy (s1, s2, 3)<< endl; } Натижа: Gulorom Агар strncpy (s2, s1, 3); деб ёзилса, натижа Dil чикади. 3. a) 2ta qator o’zgaruvchisini solishtirish strcmp (s1, s2); agar s1>s2 bo’lsa natija + son bo’ladi agar s2>s1 bo’lsa natija – son bo’ladi agar s1=s2 bo’lsa natija 0 bo’ladi. b) 1-qatorning n ta simvolini 2-qator bilan solishtirish. strncmp ( s1, s2, n); Masalan: char s1[10] = “ Gulnora “, s2[10]=” Guli “; int n; n=strcmp ( s1, s2 ); n + n=strcmp ( s2, s1 ); n – yoki: char s1[10] = “ Guli “, s2[10]=” Gulnora “; int n; n=strcmp ( s1, s2 ); n - n=strncmp ( s2, s1,3 ); n =0 4. Qator tipidagi o’zgaruvchining uzunligini hisoblash strlen (s); masalan: char *s = “Dasturchilar”; int n; n = strlen (s); cout << n ; Натижа : n=12 5. a) Qator o’zgaruvchisining boshidan simvollarni berilgan simvol bilan almashtirish strset (s,c); b) Qator o’zgaruvchisining boshidan n ta simvolni berilgan simvol bilan almashtirish strnset (s,c,n); Masalan: char *s = “Hilola”, c = ‘Z’; int n = 1; strnset ( s, c, n); cout << s<< endl; Натижа: Zilola yoki: char *s = “Hilola”, c = ‘Z’; strset ( s, c); cout << s<< endl; Натижа: ZZZZZZ 6. Satrdagi harflar registrini almashtirish funksiyasi strupr ( ); - kichik harflarni katta harflarga almashtirish strlwr ( ); - katta harflarni kichik harflarga almashtirish Masalan: char s[ ] =“INFORMATIKA”; strlwr ( s ); cout << s; 7. Satrni teskariga tartiblash funksiyasi: strrev ( ); Masalan: char s[ ] = “telefon”; cout << strrev ( s ); Natija: nofelet 8. Satrda ma’lum bir belgini izlash: strchr ( ); Izlash satr oxiridan boshlanadi. Natijada bir ko’rsatkichga belgining adresi (indeksi) o’zlashtiriladi. Masalan: char s[ ] = “0123456789012”, *p, *p1; p=strchr( s, ‘7’); p1=strchr (s, ‘1’); 1-misol Palindrom masalasi. (non,kiyik,aziza,…) # include # include void main ( ) { char s[20], sp[20]=” “; int n, i; cin >> s ; // berilgan so’z n = strlen (s); // so’z uzunligi for ( i=0; i < n; i++) sp [(n-1) – i ] = s [ i ]; if ( strcmp( s, sp) = = 0) // so’zlar bir hil bo’lsa cout <<”palindrom so’z=”<< sp < 2-misol. # include # include void main ( ) { char d[20], *b = “ “, *c = “Ergashev”, *a = “Davron”; strcpy ( d, a ); strcat ( d, b ); strcat ( d, c ); cout << d << endl; } Натижа: Davron Ergashev 3-misol. 2 sanoq sistemasidagi sonni 10 s.s ga o’tkazish uchun dastur. # include # include int main ( ) { char a[1000]; long s=0; int I, k; cout<<“ 2 s.s.dagi sonni kiriting=“; cin >> a; k=strlen ( a ); for( i=0; i< k; i++) s=s*2 + a[i] –’0’; cout<<“10 s.sga o’tkazildi=“<< s << endl; } Qator tipidagi o’zgaruvchilar bilan yana quyidagi amallarni ham bajarish mumkin. Bunda # include ulanishi kerak. 1. Qator tipini butun son tipiga o’tkazish, y’ani char → int atoi ( s ); Masalan: int n; char *s = “12345”; n = atoi ( s ); cout << n < 2.Qator tipidagi o’zgaruvchni haqiqiy son tipiga o’tkazish, y’ani char → float atof (s); Masalan: float n; char *s = “12345.67”; n = atof ( s ); cout << n <n = 12345.67; float n; char *s = “123e+30 ”; n = atof ( s ); cout << n <n = 1.23e+32; 3. Qator tipidagi o’zgaruvchini uzun butun tipiga o’tkazish, y’ani char → long atol (s); Masalan: long n; char *s = “1234567890”; n = atol ( s ); cout << n < 4. Butun tipdagi o’zgaruvchini qator tipiga o’tkazish, y’ani float → char itoa (a, b, c); Bu erda: a – int tipidagi o’zgaruvchi b – char tipidagi o’zgaruvchi c – sanoq sistemasining asosi Masalan: int a = 12.34, c = 10; char *b; itoa (a, b, c); cout << b < 5. Uzun butun tipdagi o’zgaruvchini qator tipiga o’tkazish. long → char ltoa (a, b, c); Masalan: long a = 123456789, c = 10; char *b; ltoa (a, b, c); cout << b <
Do'stlaringiz bilan baham: |