Viết một chương trình để đếm số nguyên âm trong tệp văn bản trong C.
Input
Hello! there
My name is John Smith
What's your name?
How you doing?
Output
19
Các bước
- Yêu cầu người dùng nhập tên tệp.
- Khởi tạo một biến & nbsp; Count = 0 & nbsp; để lưu trữ số nguyên âm.
- Đọc nội dung của ký tự tệp theo ký tự. Sử dụng câu lệnh chuyển đổi để kiểm tra xem một ký tự có phải là nguyên âm hay không. Nếu một ký tự là nguyên âm, tăng & nbsp; đếm.
- Số lượng nguyên âm trong tệp đầu vào bằng & NBSP; đếm.
#include // returns the number of vowels in the file int no_of_vowels[char file[256]] { FILE* fp; char c; int count = 0; // variable to store vowel count fp = fopen[file, "r"]; if [fp == NULL] { printf["UNABLE TO OPEN THE FILE"]; return -1; } while [[c = fgetc[fp]] != EOF] { switch[c] { case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': case 'O': case 'U': ++count; } } return count; } // function to print content of a file void print_file[char file[256]] { FILE* fp; char c; fp = fopen[file, "r"]; if [fp == NULL] { printf["UNABLE TO OPEN THE FILE"]; return; } while [[c = fgetc[fp]] != EOF] { printf["%c", c]; } } int main[] { char file[256]; printf["Enter file name: "]; gets[file]; printf["\n***CONTENT OF THE INPUT FILE***\n"]; print_file[file]; printf["\n"]; printf["Number of Vowels = %d", no_of_vowels[file]]; }
Đầu ra
Enter file name: text.txt
***CONTENT OF THE INPUT FILE***
Hello! there
My name is John Smith
What's your name?
How you doing?
Number of Vowels = 19
Ở đây, chúng tôi sẽ đếm số lượng nguyên âm [A, E, I, O, U] trong một tệp.
Chúng tôi xem xét cùng một tệp văn bản từ chương trình C trước đây của chúng tôi.
Hồ sơ là tyger.txt
, bao gồm các khổ thơ đầu tiên của bài thơ "The Tyger" của William Blake.
Tyger Tyger, burning bright,
In the forests of the night;
What immortal hand or eye,
Could frame thy fearful symmetry?
Chương trình cũng tương tự như chương trình C trước đó, chỉ là chúng tôi khai báo thêm một biến unsigned short int
để đếm số nguyên âm. Và bên trong vòng lặp
#include // returns the number of vowels in the file int no_of_vowels[char file[256]] { FILE* fp; char c; int count = 0; // variable to store vowel count fp = fopen[file, "r"]; if [fp == NULL] { printf["UNABLE TO OPEN THE FILE"]; return -1; } while [[c = fgetc[fp]] != EOF] { switch[c] { case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': case 'O': case 'U': ++count; } } return count; } // function to print content of a file void print_file[char file[256]] { FILE* fp; char c; fp = fopen[file, "r"]; if [fp == NULL] { printf["UNABLE TO OPEN THE FILE"]; return; } while [[c = fgetc[fp]] != EOF] { printf["%c", c]; } } int main[] { char file[256]; printf["Enter file name: "]; gets[file]; printf["\n***CONTENT OF THE INPUT FILE***\n"]; print_file[file]; printf["\n"]; printf["Number of Vowels = %d", no_of_vowels[file]]; }0, ký tự được đọc bởi con trỏ tệp được kiểm tra cả nguyên âm chữ thường và hoa văn bản.
#include
int main[] {
unsigned short vowels = 0;
char c, file[50];
FILE *fp;
printf["FILENAME: "];
scanf["%[^\n]", file];
fp = fopen[file, "r"]; // 'r' opens the file in read mode
printf["READING THE CONTENTS OF THE FILE [ %s ]\n", file];
while[[c = fgetc[fp]] != EOF] {
if[c == 'a' || c == 'A' || c == 'e' || c == 'E' || c == 'i' || c == 'I' || c == 'o' || c == 'O' || c == 'u' || c == 'U'] {
vowels++;
}
printf["%c", c];
}
printf["\n"];
printf["NUMBER OF VOWELS: %hu \n", vowels];
fclose[fp];
return 0;
}
Khi chạy chương trình và nhập tyger.txt
để nhắc vào
#include // returns the number of vowels in the file int no_of_vowels[char file[256]] { FILE* fp; char c; int count = 0; // variable to store vowel count fp = fopen[file, "r"]; if [fp == NULL] { printf["UNABLE TO OPEN THE FILE"]; return -1; } while [[c = fgetc[fp]] != EOF] { switch[c] { case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': case 'O': case 'U': ++count; } } return count; } // function to print content of a file void print_file[char file[256]] { FILE* fp; char c; fp = fopen[file, "r"]; if [fp == NULL] { printf["UNABLE TO OPEN THE FILE"]; return; } while [[c = fgetc[fp]] != EOF] { printf["%c", c]; } } int main[] { char file[256]; printf["Enter file name: "]; gets[file]; printf["\n***CONTENT OF THE INPUT FILE***\n"]; print_file[file]; printf["\n"]; printf["Number of Vowels = %d", no_of_vowels[file]]; }2
$ ./a.out
FILENAME: tyger.txt
chúng tôi nhận được
Trong ví dụ này, số lượng nguyên âm, phụ âm, chữ số và không gian trắng trong một chuỗi được nhập bởi người dùng được tính.
Để hiểu ví dụ này, bạn nên có kiến thức về các chủ đề lập trình C sau:
- C mảng
- C chuỗi lập trình
Chương trình để đếm nguyên âm, phụ âm, v.v.
#include
int main[] {
char line[150];
int vowels, consonant, digit, space;
// initialize all variables to 0
vowels = consonant = digit = space = 0;
// get full line of string input
printf["Enter a line of string: "];
fgets[line, sizeof[line], stdin];
// loop through each character of the string
for [int i = 0; line[i] != '\0'; ++i] {
// convert character to lowercase
line[i] = tolower[line[i]];
// check if the character is a vowel
if [line[i] == 'a' || line[i] == 'e' || line[i] == 'i' ||
line[i] == 'o' || line[i] == 'u'] {
// increment value of vowels by 1
++vowels;
}
// if it is not a vowel and if it is an alphabet, it is a consonant
else if [[line[i] >= 'a' && line[i] = '0' && line[i]