943,096 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 377
  • C RSS
Mar 3rd, 2010
0

Count Letter A

Expand Post »
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
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. char next;
  5. char *str = "Hgj a dsfsIwgEfeEw a weUfewUfewIfOO";
  6. char *ptr;
  7. FILE *file_ptr;
  8. int count_a=0;
  9. int i;
  10. int a;
  11. int read_vowels;
  12. void read_from_file();
  13. void print_vowels_fnc();
  14. void count_a_fnc();
  15.  
  16. int main(int argc, char *argv[])
  17. {
  18. read_from_file();
  19.  
  20. printf("vowels read\n\n");
  21.  
  22. print_vowels_fnc();
  23.  
  24. printf("letter a\n\n");
  25.  
  26. count_a_fnc();
  27.  
  28. printf("\n\n");
  29.  
  30. system("PAUSE");
  31. return 0;
  32. }
  33. void read_from_file()
  34. {
  35. file_ptr = fopen("Hello.txt","r");
  36. if( file_ptr !=NULL);
  37. {
  38. while(1)
  39. {
  40. next = fgetc(file_ptr);
  41. if (next != EOF) printf( "%c", next);
  42. else break;
  43.  
  44. //*ptr= fgetc(file_ptr);
  45. //if(next == 'a' || next == 'e'|| next == 'i' || next == 'o' || next == 'u')
  46. // printf( "%c", next);
  47.  
  48. }
  49. fclose( file_ptr );
  50. }
  51. }
  52.  
  53. void print_vowels_fnc()
  54. {
  55. file_ptr = fopen("Hello.txt","r");
  56. if( file_ptr !=NULL);
  57. {
  58. while(1)
  59. {
  60. next = fgetc(file_ptr);
  61. if (next != EOF)
  62. {
  63. if(next == 'a' || next == 'e'|| next == 'i' || next == 'o' || next == 'u')
  64. printf( "%c", next);
  65. if(next == 'A' || next == 'E'|| next == 'I' || next == 'O' || next == 'U')
  66. printf( "%c", next);
  67. }
  68. else break;
  69.  
  70. //*ptr= fgetc(file_ptr);
  71. //if(next == 'a' || next == 'e'|| next == 'i' || next == 'o' || next == 'u')
  72. // printf( "%c", next);
  73.  
  74. }
  75. fclose( file_ptr );
  76. }
  77. }
  78. void count_a_fnc()
  79. {file_ptr = fopen("Hello.txt","r");
  80. if( file_ptr !=NULL);
  81. {
  82. while(1)
  83. {
  84. next = fgetc(file_ptr);
  85. if (next != EOF)
  86.  
  87.  
  88. ptr=str;
  89. for(i=1;i<=strlen(str);i++){
  90. count_a_fnc();
  91. ptr++;
  92. if (*ptr == a);
  93. count_a++;
  94. system ("PAUSE");
  95. }
  96.  
  97.  
  98.  
  99. }
  100. fclose( file_ptr );
  101. }
  102. }
Last edited by WaltP; Mar 3rd, 2010 at 2:09 pm. Reason: Added CODE tags -- with all the help about them, how could you miss using them????
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
homesweethome17 is offline Offline
1 posts
since Mar 2010
Mar 3rd, 2010
0
Re: Count Letter A
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.
Last edited by shah1248; Mar 3rd, 2010 at 12:25 pm.
Reputation Points: 15
Solved Threads: 7
Light Poster
shah1248 is offline Offline
41 posts
since Mar 2010
Mar 3rd, 2010
0
Re: Count Letter A
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:

  1. ptr=str;
  2. for(i=1;i<=strlen(str);i++){
  3. //count_a_fnc();
  4. ptr++; //why increment this?
  5. if (*ptr == 'a'); // added the apostrophe's
  6. count_a++;
  7. }
  8. printf( "%d", count_a); //just to see what your count is...it's not correct though
  9. system ("PAUSE"); //placed outside of loop
Reputation Points: 63
Solved Threads: 11
Junior Poster
MyrtleTurtle is offline Offline
100 posts
since Feb 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Check if ESC is pressed
Next Thread in C Forum Timeline: How do I make a variable number pyramid?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC