0

Hello, I need help with a program that reads in a string, detailing the occurrence of each variable. So far the program reads a string and shows the number of times each variable occurs (letters only). The program is based of keyboard input, so I was wondering if anyone could help me with allowing my program to output the number of non letter used.

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

int main()
{
   char string[100], ch;
   int c = 0, count[26] = {0};

   printf("Enter a string\n");
   gets(string);

   while ( string[c] != '\0' )
   {
      if ( string[c] >= 'a' && string[c] <= 'z' ) 
         count[string[c]-'a']++;

      c++;
   }

   for ( c = 0 ; c < 26 ; c++ )
   {
      if( count[c] != 0 )
         printf("%c occurs %d times in the entered string.\n",c+'a',count[c]);
   }

   return 0;
}
2
Contributors
3
Replies
31
Views
4 Years
Discussion Span
Last Post by Ancient Dragon
0

delete lines 9 and 10, then replace them with a line tht declares a FILE* variable, opens the file for reading, then calls fgets() to read the first line of the file into the string variable.

0

Sorry I'm not quite sure I know how to do that. Could you provide an example?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.