Thread: counting letters in string

  1. #1
    Registered User
    Join Date
    Mar 2008

    Unhappy counting letters in string

    I'm trying to create a program that reads in a block of text from the keyboard, analyze the text and display the output in the format shown below. However i have to use two arrays one is a character holder (each time a new character is found it will be placed in that array. Only one of each character may be placed in the array). the second array is a count array. i am allow to use isalpha function.

    character count
    H 3
    r 2
    u 3
    . .
    . .

    This is the code i have so far

    #include "stdafx.h"
    #include "stdio.h"
    int _tmain(int argc, _TCHAR* argv[])
    	#define ALPHABET_TOTAL 26                    /* The total number of letters in the alphabet */
    		int text[ALPHABET_TOTAL] = {0},        /* Array sized 26, acting as counters for all characters 'a' .. 'z'  (case insensitive) */     
             text_input,                        /* Reads in the text input as characters */
             ASCII_count,                       /* The ASCII numerical representation of letters in the alphabet */
             alpha;                             /* boolean "flag" which turns to 0 (false) in case of a non-alpha character */
    	/* Initialization */
          text_input = 0;
          ASCII_count = 0;
          alpha = 0;
          /* Get input */
          printf("\nPlease enter text.  Terminate program using Ctrl+Z: ");
          while( text_input != EOF )
            alpha = 1; 
            text_input = getchar();    
          /* If input is alphabetical, convert to a number 0-25 so that it can be used as an array index locator */
          if(text_input >= 'a' && text_input <= 'z')
           text_input -= 'a';
          else if(text_input >= 'A' && text_input <= 'Z')
           text_input -= 'A';
          /* Non-alphabetical character, set the alpha flag to false */
           alpha = 0;
          /* Increment the appropriate counter in the text array as long as the character is alphabetical */
         /* Display results */
        for (ASCII_count; ASCII_count <= 25; ASCII_count++)
         printf("\nTotal %c or %c: %d", ASCII_count + 'a', ASCII_count + 'A', text[ASCII_count]);
    The problem is that i don't want to display all the letters in the alphabet only the ones that appear in the text. Also I need to come up with a way to differentiate lower case from upper case.

    Any help would be greatly appreciate it.


  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    How about
    if ( text[ASCII_count] != 0 )
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 09:20 AM
  2. Help with hw problem: counting characters in a string
    By pinkfloyd4ever in forum C++ Programming
    Replies: 11
    Last Post: 11-04-2007, 11:18 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM