[Bài tập C]Chuẩn hóa xâu ký tự nhập vào

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

//Hàm xóa một phần tử trong xâu

void del(char* pStr, int pos) {
    for(pos; pos < strlen(pStr) - 1; pos++) {
        pStr[pos] = pStr[pos + 1];
    }
    pStr[strlen(pStr) - 1] = NULL;
}

//Hàm xác định các khoảng trắng trong xâu

void del_space(char *pStr) {
    int i;
    for(i = 0; i < strlen(pStr); i++) {
        if(pStr[i] == ' ' && pStr[i + 1] == ' ') {
            del(pStr, i);
            i--;
        }
    }
    if(pStr[0] == ' ') del(pStr, 0); //Xoa space dau ky tu
    if(pStr[strlen(pStr)] == ' ') del(pStr, strlen(pStr));//Xoa space cuoi ky tu
}

//Viết hoa ký tự đầu mỗi từ

void cap_word(char* pStr) {
    int i;
    if(pStr[0] > 96) pStr[0] -= 32;
    for(i = 1; i < strlen(pStr); i++) {
        if(pStr[i] > 96 && pStr[i - 1] == ' ') pStr[i] -= 32;
    }
}

//-------------------------MAIN----------------------------------

int main()
{
    int i, pos;
    printf("Nhap vao chuoi khong qua 80 ky tu: ");
    char input[81];
    fgets(input, 81, stdin);
    del_space(&input);
    cap_word(&input);
    for(i = 0; i < strlen(input); i++){
        printf("%c", input[i]);
    }
    return 0;
}

Nhận xét

Bài đăng phổ biến