Hướng dẫn write a program to count the number of vowels present in a text file in c++ - viết chương trình đếm số nguyên âm có trong tệp văn bản bằng c ++

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

  1. Yêu cầu người dùng nhập tên tệp.
  2. Khởi tạo một biến & nbsp; Count = 0 & nbsp; để lưu trữ số nguyên âm.
  3. Đọ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.
  4. 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ướng dẫn write a program to count the number of vowels present in a text file in c++ - viết chương trình đếm số nguyên âm có trong tệp văn bản bằng c ++

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.

Hướng dẫn write a program to count the number of vowels present in a text file in c++ - viết chương trình đếm số nguyên âm có trong tệp văn bản bằng c ++
				
				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

Hướng dẫn write a program to count the number of vowels present in a text file in c++ - viết chương trình đếm số nguyên âm có trong tệp văn bản bằng 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] <= 'z')) {
      ++consonant;
    }

    // check if the character is a digit
    else if (line[i] >= '0' && line[i] <= '9') {
      ++digit;
    }

    // check if the character is an empty space
    else if (line[i] == ' ') {
      ++space;
    }
  }

  printf("Vowels: %d", vowels);
  printf("\nConsonants: %d", consonant);
  printf("\nDigits: %d", digit);
  printf("\nWhite spaces: %d", space);

  return 0;
}

Đầu ra

Enter a line of string: C++ 20 is the latest version of C++ yet.
Vowels: 9
Consonants: 16
Digits: 2
White spaces: 8

Ở đây, chuỗi được nhập bởi người dùng được lưu trữ trong biến dòng.

Ban đầu, các biến nguyên âm, phụ âm, chữ số và không gian được khởi tạo thành 0.0.

Sau đó, một vòng

#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));
}
3 được sử dụng để lặp lại các ký tự của chuỗi & nbsp; chuỗi. Trong mỗi lần lặp, chúng tôi:

  • Chuyển đổi ký tự thành chữ thường bằng hà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));
    }
    4
  • Kiểm tra xem ký tự là nguyên âm, phụ âm, một chữ số hay không gian trống. Giả sử nhân vật là một phụ âm. Sau đó, biến
    #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));
    }
    5 được tăng thêm 1.1.

Khi vòng lặp kết thúc, số lượng nguyên âm, phụ âm, chữ số và không gian trắng được lưu trữ trong các biến nguyên âm, phụ âm, chữ số và không gian tương ứng.

Lưu ý: Chúng tôi đã sử dụng hàm tolower () để đơn giản hóa chương trình của chúng tôi. Để sử dụng chức năng này, chúng tôi cần nhập tệp tiêu đề Ctype.h. We have used the tolower() function to simplify our program. To use this function, we need to import the ctype.h header file.

Làm thế nào để bạn đếm số lượng nguyên âm trong một tệp văn bản?

Đếm các nguyên âm trong in tệp văn bản (end = "Nhập tên của tệp:") fileName = str (input ()) thử: fileHandle = open (fileName, "r") tot = 0 nguyên âm = ['a', 'e',' i ',' o ',' u ',' a ',' e ',' i ',' o ',' u '] cho char trong fileHandle.Đọc (): Nếu char trong nguyên âm: TOT = TOT+1 FILEHANDLE.print(end="Enter the Name of File: ") fileName = str(input()) try: fileHandle = open(fileName, "r") tot = 0 vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'] for char in fileHandle. read(): if char in vowels: tot = tot+1 fileHandle.

Làm thế nào để bạn đếm số lượng nguyên âm?

Để đếm số nguyên âm trong một câu nhất định:..
Đọc một câu từ người dùng ..
Tạo một biến (đếm) Khởi tạo nó với 0 ;.
So sánh từng ký tự trong câu với các ký tự {'a', 'e', 'i', 'o', 'u'}.
Nếu một trận đấu xảy ra tăng số lượng ..
Cuối cùng thì in số ..

Có bao nhiêu nguyên âm trong một chuỗi trong C?

'A', 'e', 'i', 'o', 'u' là năm nguyên âm trong số 26 ký tự trong chữ cái tiếng Anh.Lập trình C nhạy cảm trường hợp, và do đó các ký tự chữ thường và chữ hoa được coi là khác nhau, vì vậy chúng tôi sẽ phải kiểm tra cả hai trường hợp.five vowels out of 26 characters in English alphabet letters. C programming is case sensitive, and hence lowercase and uppercase characters are considered differently, so we will have to check for both the cases.

Làm thế nào chúng ta có thể đếm nguyên âm trong một chuỗi?

Có hai phương pháp để đếm tổng số nguyên âm trong một chuỗi ...
Iterative..
Recursive..