Like Tree1Likes
  • 1 Post By ZuK

Character Counting Program. Crashes at the End.

This is a discussion on Character Counting Program. Crashes at the End. within the C Programming forums, part of the General Programming Boards category; So I am writing a program that counts the letters of 3 lines of input from the user. I am ...

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    11

    Character Counting Program. Crashes at the End.

    So I am writing a program that counts the letters of 3 lines of input from the user. I am using a 3 x 80 character array as the "notepad".
    Upper and lower case characters are incremented on the same counter array.

    Code:
    /*Letters in a string*/
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    void countAlphabet(char *);
    
    /*Character counting array*/
    int alphabet[26] = {0};
    
    int main(void){
    
        char paper[3][80];
        
        int counter = 0;
    
        char c;
    
        printf("Enter 3 lines of texts\n");
    
        fgets(paper[0], 80, stdin);
        fgets(paper[1], 80, stdin);
        fgets(paper[2], 80, stdin);
    
      
        for(counter = 0; counter < 3; counter++)
            countAlphabet(&paper[counter]);
    
        for(counter = 0; counter <26; counter++)
            printf("%d ", alphabet[counter]);
    
        printf("\n");
    
    
        return 0;
    }
    
    
    void countAlphabet(char *line){
    
        int i = 0;
    
        printf("Inside countAlphabet\n");
        while(i< 80){
    
            if((line[i] <= 122 && line[i] >= 97) || (line[i] >= 65 && line[i] <= 90)){
                if(isupper(line[i]))
                    ++alphabet[line[i] - 97];
                else
                    ++alphabet[line[i] - 26];
            }
            
            i++;
        }
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > if((line[i] <= 122 && line[i] >= 97) || (line[i] >= 65 && line[i] <= 90))
    There is another is... function to do this (see your use of isupper on the next line.

    > ++alphabet[line[i] - 26];
    Where did 26 come from?

    Perhaps you should use
    ++alphabet[line[i] - 'A'];
    and/or
    ++alphabet[line[i] - 'a'];
    as appropriate
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    countAlphabet(&paper[counter]);
    increase the warning level of your compiler.
    Kurt
    AndiPersti likes this.

  4. #4
    Registered User
    Join Date
    May 2013
    Posts
    11
    Responding to second >,

    I did some scratchwork where i printed the character and integer value of those statements and they come out fine

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with character counting program
    By shyam.sunder91 in forum C Programming
    Replies: 3
    Last Post: 05-08-2011, 05:20 AM
  2. Help with a character counting program!
    By Redpred in forum C Programming
    Replies: 9
    Last Post: 08-09-2006, 08:15 AM
  3. Problem with character counting program...
    By MyglyMP2 in forum C++ Programming
    Replies: 9
    Last Post: 03-25-2005, 04:12 PM
  4. counting program worked, but test file CRASHES IT.. WHY?
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 05-19-2002, 02:29 AM
  5. Character counting program
    By TankCDR in forum C++ Programming
    Replies: 5
    Last Post: 04-05-2002, 09:01 PM

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