I am currently working on a program that inputs a line from a file, then reads the vowels in the file, next I am going to count the number of letter a within the program.

Below is the program I have done but it is not working. It opens the file, reads the line, closes the file. Next it opens the file, and read the vowels within the file. Next it is suppose to open the file and count the number of letter a.

The line "Hgj a dsfsIwgEfeEw a weUfewUfewIfOO" is the frist line of .txt n the hello.txt is the file.

My void count_a_fnc() part seems to have some problems.
Thanks

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

char next;
char *str = "Hgj a dsfsIwgEfeEw a weUfewUfewIfOO";
char *ptr;
FILE *file_ptr;
int count_a=0;
int i;
int a;
int read_vowels;
void read_from_file();
void print_vowels_fnc();
void count_a_fnc();

int main(int argc, char *argv[])
{
  read_from_file();
  
  printf("vowels read\n\n");
  
  print_vowels_fnc();
  
  printf("letter a\n\n");
  
  count_a_fnc();
  
  printf("\n\n");
  
  system("PAUSE");	
  return 0;
}  
void read_from_file()
{
file_ptr = fopen("Hello.txt","r");
  if( file_ptr !=NULL);
  {
      while(1)
      {
              next = fgetc(file_ptr);
              if (next != EOF) printf( "%c", next);
              else break;
              
              //*ptr= fgetc(file_ptr);
              //if(next == 'a' || next == 'e'|| next == 'i' || next == 'o' || next == 'u')
              //        printf( "%c", next);
                
      }    
      fclose( file_ptr );
  }
}

void print_vowels_fnc()
{
file_ptr = fopen("Hello.txt","r");
  if( file_ptr !=NULL);
  {
      while(1)
      {
              next = fgetc(file_ptr);
              if (next != EOF) 
              {
                       if(next == 'a' || next == 'e'|| next == 'i' || next == 'o' || next == 'u')
                                printf( "%c", next);
                       if(next == 'A' || next == 'E'|| next == 'I' || next == 'O' || next == 'U')
                                printf( "%c", next);
              }
              else break;
              
              //*ptr= fgetc(file_ptr);
              //if(next == 'a' || next == 'e'|| next == 'i' || next == 'o' || next == 'u')
              //        printf( "%c", next);
                
      }    
      fclose( file_ptr );
  }
}
 void count_a_fnc()
{file_ptr = fopen("Hello.txt","r");
  if( file_ptr !=NULL);
  {
      while(1)
      {
              next = fgetc(file_ptr);
              if (next != EOF) 
              
     
      ptr=str;
      for(i=1;i<=strlen(str);i++){
                                  count_a_fnc();
                                  ptr++;
                                  if (*ptr == a);
                                  count_a++;
                                  system ("PAUSE");
                                  }
                                  
      
                    
      }    
      fclose( file_ptr );
  }
}

There is recursion in your count_a_fnc() function (it is calling itself) with no base case. Did you really want to do that? All you need to do is to read one char at a time just like your print_vowels_fnc() function and instead of checking for vowels, check for 'a', and instead of printing just increment your count_a variable and you are done. Of course you will have to print the final result count_a just before you exit the function.

And yes, please put all your code in between code tags to make it easy to read.

It seems to me as if your problem is when you try to call your function within your function. I commented it out and the program no longer freezes, although the count is not correct.

Maybe you can work with this, and get it working the way you want:

ptr=str;
for(i=1;i<=strlen(str);i++){                          
//count_a_fnc();
ptr++; //why increment this?  
if (*ptr == 'a'); // added the apostrophe's
count_a++;
}
printf( "%d", count_a); //just to see what your count is...it's not correct though
system ("PAUSE"); //placed outside of loop
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.