Cau_3

mail@pastecode.io avatar
unknown
c_cpp
3 years ago
1.8 kB
3
Indexable
Never
#include<iostream>
#include <string.h>

void chuanHoaChuoi(char str[]);
int demSoKiTu(char str[]);
void tanSuatKiTu(char arr[]);

int main() {
	int soNguoi = 0;
	char maxName[1000] = "";
	char arrName[1000];
	
	FILE *f = fopen("danhsach.inp", "rt");
	FILE *g = fopen("Ds_new.out", "wt");
	fputs("", g);
	g = fopen("Ds_new.out", "a");
	
	while(!feof(f)) {		
		char str[1000];
		fgets(str, 1000, f);
		
		chuanHoaChuoi(str);
		fputs(str, g);
		
		if(strlen(str) > strlen(maxName))
			strcpy(maxName, str);
		
		for(int i = strlen(str); i >= 0; i--) {
			if(str[i-1] == 32) {
				arrName[strlen(arrName)] = str[i];
				break;
			}
		}
		
		soNguoi += 1;
	}
	
	std::cout << "- So luong Sinh vien: " << soNguoi << std::endl << std::endl;
	std::cout << "- Sinh vien co ten dai nhat: " << maxName; 
	std::cout << " -> So ki tu trong ten: " << demSoKiTu(maxName) << std::endl;
	tanSuatKiTu(arrName);
	
	
	fclose(f);
	fclose(g);
}

void chuanHoaChuoi(char str[]) {
	int len = strlen(str);
	
	for(int i = 0; i < len; i++) {
		//kiem tra neu la ky tu hoa chuyen ve thuong
		str[i] >= 65 && str[i] <= 90 ? str[i] += 32 : 0;
		
		// kiem tra ki tu truoc no la khoang trang thi in hoa, hoac chu cai dau tien thi in hoa
		i == 0 ? str[i] -= 32 :
			str[i - 1] == 32 ?
				str[i] -= 32 : 0;
	}
}

int demSoKiTu(char str[]) {
	int len = strlen(str) - 1; // -1 ki tu \n
	int result = 0;
	for(int i = 0; i < len; i++)
		str[i] != ' ' ? result += 1 : 0;
	return result;
}

void tanSuatKiTu(char arr[]) {
	int len = strlen(arr), i;
    int count[256] = {0};	// khai bao mang dem, gan cho no ==0 het
	for(i = 0; i < len; i++) 
		count[arr[i]]++; // thuc hien dem so lan xuat hien
	for(i = 0; i < 256; i++) // tien hanh xuat
    	if(count[i]) 
			printf("%c: %d\n", i, count[i]);
}