Thread: How do I read only the first capital letter from a file?

  1. #1
    Registered User
    Join Date
    Dec 2014
    Location
    New York, New York, United States
    Posts
    1

    How do I read only the first capital letter from a file?

    Hello everyone, I'm supposed to count the number of first capital letters in a string. I've written a code that counts every uppercase letter, and it turns out that this is not what the assignment is about. For example, the input "JAMES, How Are you?" should return an output of 3 (mine would return 7 in this case). Only J, H, A need to be counted.

    Code:
    #include <stdio.h>
    #include <string.h>
    #define MAX 100
    
    
       void WriteToFile() {
        FILE *f = fopen("text.txt", "w");
        char c;
        while((c = getchar()) != EOF) {
            fputc(c, f);
        }
        fclose(f);
    }
    
    
    int main()
    {
        WriteToFile();
    
    
        FILE *f;
        f = fopen("text.txt", "r");
        char letters[MAX];
    
    
        int count = 0, i;
    
    
            while(fgets(letters, MAX, f) != 0){
        for(i = 0; letters[i]; i++){
    
    
            if(letters[i] >= 'A' && letters[i] <= 'Z')
                count++;
    
    
    
    
            }
         }
                         printf("%d", count);
    
    
            return 0;
        }
    How do I fix this?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    That depends on what the count is supposed to be for inputs like "Foo,Bar" or "Foo-Bar" (note the absence of space).

    But, generally speaking you need to count the uppercase letters if they are the first character in the file, or if they immediately follow a character that is whitespace or punctuation.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Jun 2014
    Posts
    79
    Grumpy already put you on the right track. I add that you could take advantage of two functions from the C standard library, namely isupper() and isspace(), defined in the ctype.h include.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    #define MAX 100
    
    int main() {
        FILE *f = fopen( "text.txt", "r" );
        char letters[MAX] = "";
        int i, count=0;
    
        if( f == NULL ) {
            printf( "\n\nFile not opened!\n\n" );
            return 0;
        }
    
        fgets( letters, MAX, f );
    
        /*
        Let's verify each character of letters[]. Whenever a character
        is uppercase, we need to verify that the previous one is a space,
        unless it's the first one in the string. We take advantage of two
        functions from the ctype.h include here (isupper() and isspace()).
        Whenever the "i==0" or the "isspace(letters[i-1])" condition is true,
        1 is added to count (in C, true equals to 1 and false equals to 0).
        */
    
        for( i=0; letters[i]; ++i )
            if( isupper(letters[i]) )
                count += i==0 || isspace(letters[i-1]);
    
        printf( "\n\n%d\n\n", count );
    
        return 0;
    }
    I hope this is not regarded as "spoon feeding" (I think it is not, but who knows?).
    Last edited by aldo_baldo; 12-25-2014 at 04:49 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-15-2014, 12:42 AM
  2. Replies: 3
    Last Post: 11-14-2012, 07:13 AM
  3. Replies: 6
    Last Post: 02-28-2011, 03:45 PM
  4. How To Make Capital Letter To Small Capital Letter??
    By jason07 in forum C++ Programming
    Replies: 3
    Last Post: 10-10-2005, 04:37 PM
  5. Return true if character capital letter!
    By JamesMontgomery in forum C Programming
    Replies: 9
    Last Post: 11-20-2003, 01:39 PM

Tags for this Thread