Thread: how to check if string contains only alphabet

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    41

    Post how to check if string contains only alphabet

    If we have a string like "CPROGRAMMING"
    How can we check if this string contains only
    1. Capital letters
    2. No digits
    Thanks

  2. #2
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    For starters:

    isalpha

    Are you familiar with looping through elements of an array?

    Seeing what you have done so far would be nice.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    isupper() would be handy too.
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    41
    That is what i have just coded
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    int main ( int argc , char* argv[])
    {
    char* s = "CPROGRAMMING";
    int i;
    boolean flag = true;
    for(i=0; , i< strlen(s) && flag == true; i++ )
    {
       if( isalpha(s[i]) && isupper(s[i]) )
         flag = true;
       else
         flag = false;
    }
    if (flag)
         printf("A string contains only uppercase and alphabet letters\n" );
    return 0;
    }
    The problem is at read line.isupper() and isalpha() take arguments as integer type however like you said we are passing arg like char type. That is why i am confused
    Last edited by thungmail; 11-05-2009 at 03:43 PM.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Just pass the char as an integer. I'm not sure why they use integers for those functions. It's not like they'll except -1 as an argument. But they expect a regular character in the range 0 to 127.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Not giving away too much you could then also do:

    Code:
    ...
    while (*str++)
        {
          if ((*str >= 'a') && (*str <= 'z'))
    ...
    But yes, those functions will take your passed in char data type as well.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I'm not sure why they use integers for those functions.
    >It's not like they'll except -1 as an argument.

    Well, technically... Those functions accept int to account for EOF, the idea being that any return value from getchar is an acceptable input to any of the character classification functions (ie. the range of unsigned char, plus EOF). And EOF is often implemented as -1.

    >But they expect a regular character in the range 0 to 127.
    Beware giving out specific ranges, especially when the range of a type is implementation-defined.
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    41
    @slingerland3g: >='a' and <='z' can help me.
    @King Mir : what do you mean pass char as an integer. Could you explain pls
    Thanks all

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Thungmail's code is an example. s is a char * and s[i] is therefor a char.

    Only s should really be declared as a const char pointer, as it points to a literal string which cannot be changed.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by King Mir View Post
    Just pass the char as an integer. I'm not sure why they use integers for those functions. It's not like they'll except -1 as an argument. But they expect a regular character in the range 0 to 127.
    The isXXX() functions are required to accept EOF. EOF, by definition, is outside of the range 0..255. Therefore, a char is not big enough.

    (EOF may or may not be equal to -1 -- the point is, it is not representable in a char)
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  11. #11
    Registered User
    Join Date
    Nov 2008
    Posts
    75
    Quote Originally Posted by thungmail View Post
    That is what i have just coded
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    int main ( int argc , char* argv[])
    {
    char* s = "CPROGRAMMING";
    int i;
    boolean flag = true;
    for(i=0; , i< strlen(s) && flag == true; i++ )
    {
       if( isalpha(s[i]) && isupper(s[i]) )
         flag = true;
       else
         flag = false;
    }
    if (flag)
         printf("A string contains only uppercase and alphabet letters\n" );
    return 0;
    }
    The problem is at read line.isupper() and isalpha() take arguments as integer type however like you said we are passing arg like char type. That is why i am confused
    Among the other reasons, try this code to get an hint:
    Code:
    #include<stdio.h>
    
    int main(void)
    {
    	printf("sizeof('a') = %d\n", (int)sizeof('a'));
    	return 0;
    }
    P.S.
    Notice instead that if you try to compile it as c++ code, you'll get that sizeof('a') = 1.
    Last edited by MisterIO; 11-05-2009 at 06:10 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  5. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM