Thứ Năm, 24 tháng 12, 2015

Chương trình xử lí chuỗi cơ bản.


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

void info(char a[], int &leng)
{
    char ten[20], que[20];
    int i = 0;
    leng = 0;
    printf("\nNhap ten: ");
    gets(ten);
    printf("\nNhap que: ");
    gets(que);

    while (ten[i] != '\0')
    {
        if (ten[i] != ' ')
            a[leng++] = tolower(ten[i++]);
        else
        {
            i++;
            continue;
        }
    }
    i = 0;
    while (que[i] != '\0')
    {
        if (que[i] != ' ')
            a[leng++] = tolower(que[i++]);
        else
        {
            i++;
            continue;
        }
    }
    a[leng] = '\0';

    printf("\nChuyen het thanh 1 day ki tu thuong: %s", a);
}

void swap(char &a, char &b)
{
    char c = a;
    a = b;
    b = c;
}

void sxtang(char a[], int leng)
{
    for (int i = 0; i < leng - 1; i++)
        for (int j = i + 1; j < leng; j++)
            if (a[i]>a[j])
                swap(a[i], a[j]);
    printf("\nDay ki sau khi sx tang: %s", a);
}

int dem(char a[], char x)
{
    int dem = 0, i = 0;
    while (a[i] != '\0')
    {
        if (a[i++] == x)
            dem++;
    }
    return dem;
}

void solanxuathien(char a[], int leng)
{
    int x;
    int max = dem(a, a[0]);
    for (int i = 0; i < leng; i++)
    {
        if (a[i] == a[i + 1])
            continue;
        x = dem(a, a[i]);
        printf("\n%c xuat hien %d lan", a[i], x);
          
        if (max < x)
            max = x;
    }

    for (int i = 0; i < leng; i++)
    {
        if (a[i] == a[i + 1])
            continue;
        x = dem(a, a[i]);
        if (x == max)
            printf("\nKi tu xuat hien nhieu nhat la: %c", a[i]);
    }
}

void chuyenhoa(char a[], int leng)
{
    for (int i = 0; i < leng; i++)
        a[i] = toupper(a[i]);
    printf("\nChuoi chuyen thanh ki tu hoa: %s", a);
}

void main(void)
{
    char a[30];
    int leng;
    info(a, leng);
    sxtang(a, leng);
    solanxuathien(a, leng);
    chuyenhoa(a, leng);
    getch();
}

Không có nhận xét nào:

Đăng nhận xét