Counting/displaying number of letters per line?

This is a discussion on Counting/displaying number of letters per line? within the C Programming forums, part of the General Programming Boards category; Hello! I'm reading in a .txt file into a program and I'm trying to get it to count how many ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    31

    Counting/displaying number of letters per line?

    Hello! I'm reading in a .txt file into a program and I'm trying to get it to count how many letters are in each line, and then display them. I'm not exactly sure what to use though.

    Here's my code so far:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    
    
    int countLetters();
    
    
    void main()
    {
    
    
    countLetters();
    
    
    
    
    }
    
    
    int countLetters()
    {
            char c;
    
    
            FILE *infile = fopen("FILEIOLabDATAFILE.txt", "r");
    
    
            if(infile == NULL)
            {
                    printf("The file was not successfully open.\n");
                    return;
            }
    
    
    
    
    
    
    
    
    }

    I'm not sure if I would use
    Code:
     
    char letter;
    fscanf(infile, "%s", &letter);

    or
    Code:
    char line[80];
    infile.getline(line, 80, \n);
    fscanf(infile, "%s", letter);

    And then I'm not exactly sure what variable I would put to display it? Something like this?
    Code:
    int i;
    for(i = 0; i < strlen(line); i++)
    {
    if(line[i] == x) {  count++; }
    }

    Any help would be great! Thanks!

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    First off infile.getline is not C ... I believe it's C++
    Second fscanf() stops reading at the first invisible character (space, tab, return, etc.)

    What you want is fgets() ... look it up in your Compiler's library documentation or help files. (don't have that? Get it!)

    Finding the length of the line is simple... strlen() should give it to you.

    Printing it is just as simple with printf()


    Also... DO NOT use void main() ... it's int main (void) and it returns an integer to the operating system.
    The minimum skeleton for a C program is this...
    Code:
    int main (void)
      {
         // your code goes here
    
         return 0;
    }
    And yes it does matter. Your program may work correctly but it will be incorrectly interfaced with the operating system. Windows, Linux, Os-X, BSD etc. all expect an integer "errorlevel" returned from programs... if you use void main and do not return something the OS gets garbage which could cause strange behaviour in batch or script files as well as parent programs that use the returned values.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    31
    Sorry, still having some trouble. Alright here's what I have so far, I just put in the printf to see what it does (displays each line of the .txt file). Not sure where to go from here. Would I put in another while loop inside the first while loop??

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    
    
    int countLetters();
    
    
    int main(void)
    {
    
    
    countLetters();
    
    
    return 0;
    }
    
    
    int countLetters()
    {
            char line[500];
            int count = 0;
    
    
            FILE *infile = fopen("FILEIOLabDATAFILE.txt", "r");
    
    
            if(infile == NULL)
            {
                    printf("The file was not successfully open.\n");
                    return;
            }
    
    
            while(fgets(line, 80, infile))
            {
                    printf("%s \n", line);
    
    
                    int i;
                    for(i = 0; i < strlen(line); i++)
                    {
                            if(line[i] == 'x') {  count++; }
                    }
            }
    }
    Thanks!

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    What EXACTLY are you trying to do?

    If you want the number of characters on each line..
    Code:
    while (fgets(line,500,infile))
      { 
          printf("%s", line);
          count += strlen(line);
      }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. counting letters in arrays
    By BiggestNoobEver in forum C Programming
    Replies: 6
    Last Post: 04-28-2011, 08:26 AM
  2. Replies: 5
    Last Post: 06-05-2010, 03:04 AM
  3. Counting letters in each state
    By Kayoss in forum C++ Programming
    Replies: 6
    Last Post: 11-16-2005, 08:55 PM
  4. Re: displaying the line number
    By icc_81 in forum C++ Programming
    Replies: 4
    Last Post: 12-12-2003, 03:38 PM
  5. Help with counting letters
    By hpy_gilmore8 in forum C++ Programming
    Replies: 3
    Last Post: 10-24-2003, 03:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21