Untitled

 avatar
unknown
plain_text
4 years ago
1.3 kB
4
Indexable
#include<iostream>
#include <string.h>

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

int main() {
	int soNguoi = 0;
	char maxName[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);
		soNguoi += 1;
		
		chuanHoaChuoi(str);
		fputs(str, g);
		
		if(strlen(str) > strlen(maxName))
			strcpy(maxName, str);
	}
	
	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);
	
	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;
}
Editor is loading...