Skip to content

10-dars: Character va string asoslari

Dars haqida

Davomiyligi: 90 daqiqa Maqsad: Talaba char tushunchasini, ASCII bilan ishlashni, string asoslarini va string.h funksiyalarini bilishi kerak.

1. char — bitta belgi

c
char c = 'A';
char digit = '7';
char symbol = '$';

Apostrof — bitta harf uchun.

char aslida kichik int (1 byte). ASCII raqam bo'yicha saqlanadi.

2. char va ASCII

c
char c = 'A';
printf("%c\n", c);   // A
printf("%d\n", c);   // 65 (ASCII)

Char — raqam ham bo'lishi mumkin:

c
int n = 'A';  // n = 65
char c = 65;  // c = 'A'

printf("%c\n", n);   // A
printf("%d\n", c);   // 65

3. ASCII jadval (asosiy)

'0' = 48    'A' = 65    'a' = 97
'1' = 49    'B' = 66    'b' = 98
'2' = 50    'C' = 67    'c' = 99
...
'9' = 57    'Z' = 90    'z' = 122

Bo'shliq ' ' = 32
'!' = 33
'.' = 46

4. Char matematikasi

c
char c = 'A';
c = c + 1;       // 'B'

char digit = '5';
int n = digit - '0';  // 5 (char → int)

char from_int = 3 + '0';  // '3' (int → char)

5. Katta va kichik harf

Katta → kichik: +32 (yoki + ('a' - 'A')) Kichik → katta: -32

c
char lower = 'a';
char upper = lower - 32;  // 'A'

char upper2 = 'M';
char lower2 = upper2 + 32;  // 'm'

6. ctype.h — char funksiyalari

#include <ctype.h> qo'shing.

c
isalpha(c)    // harfmi?
isdigit(c)    // raqammi?
isalnum(c)    // harf yoki raqam?
isspace(c)    // bo'shliq, tab, newline?
isupper(c)    // KATTA harfmi?
islower(c)    // kichik harfmi?
ispunct(c)    // tinish belgisi?

toupper(c)    // KATTAga aylantirish
tolower(c)    // kichikka aylantirish
c
#include <stdio.h>
#include <ctype.h>

int main(void) {
    char c = 'A';
    
    printf("Char: %c\n", c);
    printf("isalpha: %d\n", isalpha(c));  // 1 (true)
    printf("isdigit: %d\n", isdigit(c));  // 0
    printf("isupper: %d\n", isupper(c));  // 1
    printf("tolower: %c\n", tolower(c));  // a
    
    return 0;
}

7. String nima?

String — char massivi, null character (\0) bilan tugaydi.

c
char ism[] = "Akmal";

Bu aslida:

['A', 'k', 'm', 'a', 'l', '\0']

6 ta belgi: 5 harf + null terminator.

8. String e'lon qilish

Variant 1:

c
char str[] = "Salom";   // hajmi avtomatik (6)

Variant 2:

c
char str[10] = "Salom";  // 10 byte, 5 ishlatilgan

Variant 3:

c
char str[] = {'S', 'a', 'l', 'o', 'm', '\0'};

Variant 4:

c
char *str = "Salom";   // pointer, faqat o'qish

String hajmi

String hajmi — kerakli belgilar soni + 1 (\0 uchun).

c
char str[5] = "Salom";  // XATO! 6 kerak

9. printf bilan string

c
char ism[] = "Akmal";
printf("Salom, %s!\n", ism);

%s — string format specifier.

10. scanf bilan string

c
char ism[100];
scanf("%s", ism);  // & yo'q!

Diqqat: stringda & yo'q — string'ning o'zi pointer.

Limit qo'ying:

c
scanf("%99s", ism);  // maksimum 99 (1 ta \0 uchun)

11. String uzunligi

c
#include <stdio.h>
#include <string.h>

int main(void) {
    char str[] = "Akmal";
    int len = strlen(str);
    printf("Uzunligi: %d\n", len);  // 5
    return 0;
}

strlen — null character'gacha sanaydi.

Qo'lda yozish:

c
int my_strlen(char *str) {
    int len = 0;
    while (str[len] != '\0') {
        len++;
    }
    return len;
}

12. string.h funksiyalari

c
#include <string.h>
FunksiyaVazifa
strlen(s)Uzunlik
strcpy(dest, src)Nusxalash
strncpy(dest, src, n)n belgi nusxalash
strcat(dest, src)Qo'shish
strcmp(s1, s2)Taqqoslash
strncmp(s1, s2, n)n belgi taqqoslash
strchr(s, c)Belgini topish
strstr(s, sub)Substring topish

13. strcpy — nusxalash

c
char src[] = "Akmal";
char dest[100];
strcpy(dest, src);
printf("%s\n", dest);  // Akmal

strcpy xavfsiz emas

strcpydest hajmini tekshirmaydi. Buffer overflow xavfi.

strncpy xavfsizroq:

c
strncpy(dest, src, sizeof(dest) - 1);
dest[sizeof(dest) - 1] = '\0';

14. strcat — qo'shish

c
char str[100] = "Salom ";
strcat(str, "dunyo!");
printf("%s\n", str);  // Salom dunyo!

15. strcmp — taqqoslash

c
char a[] = "Akmal";
char b[] = "Akmal";
char c[] = "Aziza";

if (strcmp(a, b) == 0) {
    printf("Teng\n");
}

if (strcmp(a, c) < 0) {
    printf("a alifboda oldin\n");
}

Natija:

  • 0 — teng
  • < 0 — birinchi alifboda oldin
  • > 0 — birinchi alifboda keyin

== ishlatmang

c
char a[] = "Akmal";
char b[] = "Akmal";
if (a == b) { ... }  // XATO! pointer'lar taqqoslanadi

Stringlar uchun — strcmp.

16. String harflari aylantirish

c
char str[] = "Salom Dunyo";

// Hammasini katta
for (int i = 0; str[i] != '\0'; i++) {
    str[i] = toupper(str[i]);
}
printf("%s\n", str);  // SALOM DUNYO

// Yoki kichik
for (int i = 0; str[i] != '\0'; i++) {
    str[i] = tolower(str[i]);
}

17. String — belgilariga indeks orqali murojaat

c
char str[] = "Hello";

// Birinchi belgi
printf("%c\n", str[0]);  // H

// Oxirgi belgi
int len = strlen(str);
printf("%c\n", str[len - 1]);  // o

// O'zgartirish
str[0] = 'J';
printf("%s\n", str);  // Jello

18. String teskari

c
#include <stdio.h>
#include <string.h>

void reverse(char *str) {
    int len = strlen(str);
    for (int i = 0; i < len / 2; i++) {
        char temp = str[i];
        str[i] = str[len - 1 - i];
        str[len - 1 - i] = temp;
    }
}

int main(void) {
    char str[] = "Akmal";
    reverse(str);
    printf("%s\n", str);  // lamkA
    return 0;
}

19. Belgi sanash

c
#include <stdio.h>
#include <string.h>

int count_char(const char *str, char c) {
    int count = 0;
    for (int i = 0; str[i] != '\0'; i++) {
        if (str[i] == c) count++;
    }
    return count;
}

int main(void) {
    char str[] = "programming";
    printf("'m' soni: %d\n", count_char(str, 'm'));  // 2
    printf("'g' soni: %d\n", count_char(str, 'g'));  // 2
    return 0;
}

20. Unli harflar

c
int count_vowels(const char *str) {
    int count = 0;
    for (int i = 0; str[i] != '\0'; i++) {
        char c = tolower(str[i]);
        if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
            count++;
        }
    }
    return count;
}

21. Palindrom (string)

c
int is_palindrome(const char *str) {
    int len = strlen(str);
    for (int i = 0; i < len / 2; i++) {
        if (tolower(str[i]) != tolower(str[len - 1 - i])) {
            return 0;
        }
    }
    return 1;
}

int main(void) {
    char str[] = "level";
    if (is_palindrome(str)) {
        printf("%s — palindrome\n", str);
    } else {
        printf("%s — palindrome emas\n", str);
    }
    return 0;
}

22. To'liq misol: Password validator

c
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main(void) {
    char password[100];
    
    printf("Parol kiriting: ");
    scanf("%99s", password);
    
    int len = strlen(password);
    int has_upper = 0, has_lower = 0, has_digit = 0, has_special = 0;
    
    for (int i = 0; i < len; i++) {
        if (isupper(password[i])) has_upper = 1;
        else if (islower(password[i])) has_lower = 1;
        else if (isdigit(password[i])) has_digit = 1;
        else if (ispunct(password[i])) has_special = 1;
    }
    
    printf("\nParol tahlili:\n");
    printf("Uzunligi: %d %s\n", len, len >= 8 ? "✓" : "✗ (kamida 8)");
    printf("KATTA harf: %s\n", has_upper ? "✓" : "✗");
    printf("kichik harf: %s\n", has_lower ? "✓" : "✗");
    printf("Raqam: %s\n", has_digit ? "✓" : "✗");
    printf("Maxsus belgi: %s\n", has_special ? "✓" : "✗");
    
    int strong = (len >= 8) && has_upper && has_lower && has_digit && has_special;
    printf("\nNatija: %s\n", strong ? "KUCHLI parol" : "ZAIF parol");
    
    return 0;
}

23. String split — qadamlar

c
#include <stdio.h>
#include <string.h>

int main(void) {
    char str[] = "Akmal,Aziza,Botir,Dilshod";
    char *token = strtok(str, ",");
    
    while (token != NULL) {
        printf("%s\n", token);
        token = strtok(NULL, ",");
    }
    return 0;
}

strtok — string'ni belgilar bilan ajratadi.

Natija:

Akmal
Aziza
Botir
Dilshod

24. Common pitfalls

1. Buffer overflow

c
char str[5];
strcpy(str, "Hello World!");  // XATO! 5 ta byte yetmaydi

2. \0 unutish

c
char str[5] = {'H', 'e', 'l', 'l', 'o'};  // \0 yo'q!
printf("%s\n", str);  // noma'lum natija (axlat)

To'g'ri:

c
char str[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

3. String'larni == bilan taqqoslash

c
if (str1 == str2) { ... }   // XATO
if (strcmp(str1, str2) == 0) { ... }  // To'g'ri

4. const ishlatmaslik

c
char *str = "Hello";
str[0] = 'J';  // CRASH! string literal — read-only

Yechim:

c
char str[] = "Hello";  // massiv — o'zgartirish mumkin

Darsdagi topshiriqlar

Topshiriq 1 — Char asoslari

char-basics.c:

c
#include <stdio.h>

int main(void) {
    char c = 'A';
    
    printf("Belgi: %c\n", c);
    printf("ASCII: %d\n", c);
    
    // Bir harf oshirish
    c = c + 1;
    printf("Keyingi: %c\n", c);  // B
    
    // Kichik harfga
    c = c + 32;
    printf("Kichik: %c\n", c);  // b
    
    // ASCII'dan harf
    for (int i = 65; i <= 90; i++) {
        printf("%c ", (char)i);
    }
    printf("\n");
    
    return 0;
}

Topshiriq 2 — String asoslari

string-basics.c:

c
#include <stdio.h>
#include <string.h>

int main(void) {
    char str1[] = "Salom";
    char str2[100];
    char str3[100];
    
    // strcpy
    strcpy(str2, str1);
    printf("str2: %s\n", str2);
    
    // strcat
    strcpy(str3, "Salom ");
    strcat(str3, "dunyo!");
    printf("str3: %s\n", str3);
    
    // strlen
    printf("Uzunligi: %d\n", (int)strlen(str3));
    
    // strcmp
    if (strcmp(str1, "Salom") == 0) {
        printf("str1 va 'Salom' teng\n");
    }
    
    return 0;
}

Topshiriq 3 — String funksiyalar

my-string.cstring.h o'rniga o'zinikilar yarating:

c
#include <stdio.h>

int my_strlen(const char *s) {
    int len = 0;
    while (s[len] != '\0') len++;
    return len;
}

void my_strcpy(char *dest, const char *src) {
    int i = 0;
    while (src[i] != '\0') {
        dest[i] = src[i];
        i++;
    }
    dest[i] = '\0';
}

int my_strcmp(const char *s1, const char *s2) {
    while (*s1 && (*s1 == *s2)) {
        s1++;
        s2++;
    }
    return *s1 - *s2;
}

void my_strcat(char *dest, const char *src) {
    int i = my_strlen(dest);
    int j = 0;
    while (src[j] != '\0') {
        dest[i + j] = src[j];
        j++;
    }
    dest[i + j] = '\0';
}

int main(void) {
    char str[100] = "Salom ";
    my_strcat(str, "dunyo!");
    printf("%s\n", str);
    printf("Uzunligi: %d\n", my_strlen(str));
    
    return 0;
}

Topshiriq 4 — Palindrome

palindrome.c:

c
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int is_palindrome(const char *str) {
    int len = strlen(str);
    for (int i = 0; i < len / 2; i++) {
        if (tolower(str[i]) != tolower(str[len - 1 - i])) {
            return 0;
        }
    }
    return 1;
}

int main(void) {
    char str[100];
    printf("So'z: ");
    scanf("%99s", str);
    
    if (is_palindrome(str)) {
        printf("%s — palindrome\n", str);
    } else {
        printf("%s — palindrome emas\n", str);
    }
    return 0;
}

Sinab ko'ring: level, deed, hello, racecar, mom.

Topshiriq 5 — Vowel/consonant counter

vowel-count.c:

c
#include <stdio.h>
#include <ctype.h>

int main(void) {
    char str[200];
    printf("Matn: ");
    scanf("%199s", str);
    
    int vowels = 0, consonants = 0, others = 0;
    
    for (int i = 0; str[i] != '\0'; i++) {
        char c = tolower(str[i]);
        if (isalpha(c)) {
            if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
                vowels++;
            } else {
                consonants++;
            }
        } else {
            others++;
        }
    }
    
    printf("Unli: %d\n", vowels);
    printf("Undosh: %d\n", consonants);
    printf("Boshqa: %d\n", others);
    
    return 0;
}

Topshiriq 6 — Caesar cipher

caesar.c — har harfni siljitish:

c
#include <stdio.h>
#include <string.h>
#include <ctype.h>

void encrypt(char *str, int shift) {
    // +26 va % 26 — manfiy shift (dekript) ham to'g'ri ishlashi uchun
    for (int i = 0; str[i] != '\0'; i++) {
        if (isupper(str[i])) {
            str[i] = ((str[i] - 'A' + shift % 26 + 26) % 26) + 'A';
        } else if (islower(str[i])) {
            str[i] = ((str[i] - 'a' + shift % 26 + 26) % 26) + 'a';
        }
    }
}

int main(void) {
    char message[100];
    int shift;
    
    printf("Xabar: ");
    scanf("%99s", message);
    
    printf("Shift: ");
    scanf("%d", &shift);
    
    encrypt(message, shift);
    printf("Shifrlangan: %s\n", message);
    
    encrypt(message, -shift);
    printf("Qayta tiklangan: %s\n", message);
    
    return 0;
}

Topshiriq 7 — Password validator

Dars matnidagi to'liq parol tekshiruvchini yarating.

Bonus — qo'shimcha tekshiruvlar:

  • Foydalanuvchi nomini ichmasligi
  • "password", "123456" kabi keng tarqalganlardan saqlanish
  • Takrorlanuvchi belgilar (aaa, 111) yo'qligi

Topshiriq 8 — Word counter

word-count.c:

c
#include <stdio.h>
#include <ctype.h>

int main(void) {
    char str[1000];
    printf("Matn (Enter — tugatish):\n");
    fgets(str, sizeof(str), stdin);
    
    int words = 0;
    int in_word = 0;
    
    for (int i = 0; str[i] != '\0'; i++) {
        if (isspace(str[i])) {
            in_word = 0;
        } else if (!in_word) {
            in_word = 1;
            words++;
        }
    }
    
    printf("So'zlar soni: %d\n", words);
    return 0;
}

Topshiriq 9 — Uppercase/lowercase

case-converter.c:

c
#include <stdio.h>
#include <ctype.h>

void to_upper(char *str) {
    for (int i = 0; str[i] != '\0'; i++) {
        str[i] = toupper(str[i]);
    }
}

void to_lower(char *str) {
    for (int i = 0; str[i] != '\0'; i++) {
        str[i] = tolower(str[i]);
    }
}

void swap_case(char *str) {
    for (int i = 0; str[i] != '\0'; i++) {
        if (isupper(str[i])) {
            str[i] = tolower(str[i]);
        } else if (islower(str[i])) {
            str[i] = toupper(str[i]);
        }
    }
}

int main(void) {
    char str[] = "Hello World";
    
    to_upper(str);
    printf("Upper: %s\n", str);
    
    to_lower(str);
    printf("Lower: %s\n", str);
    
    swap_case(str);
    printf("Swap: %s\n", str);
    
    return 0;
}

Topshiriq 10 — GitHub'ga

bash
$ cd ~/c-darslari
$ mkdir 4-oy-dars-10
$ # fayllarni shu joyga
$ git add .
$ git commit -m "feat: dars 10 - strings"
$ git push

Asosiy tushunchalar (lug'at)

TerminQisqacha izoh
charBitta belgi
ASCIIBelgi → raqam jadval
StringChar massivi, \0 bilan tugaydi
Null terminator\0
strlenUzunlik
strcpy / strncpyNusxalash
strcatQo'shish
strcmpTaqqoslash
strtokBo'lish
ctype.hChar funksiyalari
isalpha / isdigit / isspaceTekshirish
toupper / tolowerAylantirish
Buffer overflowXotira oshib ketishi
String literal"matn" (o'qish uchun)
Caesar cipherKlassik shifrlash

Keyingi dars

11-dars: Mini loyiha →

Master IT o'quv markazi — o'qitish rejasi