Qator tipidagi o’zgaruvchilar bilan ishlash Irgasheva N. 2011 yil C/C++ tilida qator tipidagi 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] = “bahor”; strcat (s1, s2); cout << s1 << endl; // yoki cout << strcat (s1, s2)<< endl; } Натижа; Gulbahor Агар cout << strcat (s2, s1)<< endl; деб езсак натижа bahorGul чикади. # 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 << endl; Натижа ; n=11 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); 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 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 = “Usmonova”, *a = “Yulduz”; strcpy ( d, a ); strcat ( d, b ); strcat ( d, c ); cout << d << endl; } Натижа: Yulduz Usmonova 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 int → 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: |