Thread: Isalpha Function Issue

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    27

    Isalpha Function Issue

    In the below code, I am attempting to count the number of letters and number of words in a phrase. In researching the return values of isalpha(), I noted that when a letter is present the first character isalpha() output is 1. But the second character isalpha() output is 2? The phrase that was input into this example was the following: How do you organize? Can someone help me with why I am not getting uniform output for the "H" character and the "o" character?

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void)
    	{
    	int ch;
    	float avg = 0;
    	int spaces = 0;
    	int letters = 0;
    	int test = 0;
    	int ltrcnt = 0;
    	int spccnt = 0;		
    	while((ch = getchar()) != '\n')
    		{
    		test = isalpha(ch);
    		if(isalpha(ch) == 2)
    			{
    			ltrcnt++;
    			}
    		if(isspace(ch) == 8)
    			{
    			spccnt++;
    			}
    		}
    	printf("The Number Of Words Are %d , The Number Of Letters Are %d ",(spccnt+1),ltrcnt);
    	
    	}

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by mcertini
    In researching the return values of isalpha(), I noted that when a letter is present the first character isalpha() output is 1. But the second character isalpha() output is 2? The phrase that was input into this example was the following: How do you organize? Can someone help me with why I am not getting uniform output for the "H" character and the "o" character?
    The return value of isalpha is either zero (false) or non-zero (true).

    If you want to differentiate between uppercase and lowercase letters, use isupper and islower respectively.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    27

    Answer

    laserlight,

    Thank you for your reply. As you state and what I have learned from another poster:

    isalpha returns zero if the given character is not an alphabet.
    Returns non-zero if it is alphabet.
    And may return '1' if the alphabet is of upper case, and '2' if it is lower case.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    "another poster" has misinformed you.

    There is no guarantee about what non-zero value is returned if a character is alphabetic. There is certainly no requirement to return a different result based on upper or lower case.

    Some libraries might do that, but others will not.
    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.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I should inquire as to whether you intend to use C or C++? What you have shown so far is C code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Function issue
    By Learner87 in forum C++ Programming
    Replies: 1
    Last Post: 04-09-2008, 01:31 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM