Thread: isDigit() and isLower() for strings

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    119

    isDigit() and isLower() for strings

    If I were to write my own functions for single characters that would be easy. I was thinking more along the lines of strings, having overloaded functions with parameters char* or char[] for the strings. How could I tokenize the word by each character and analyze each one by itself to determine if these functions would return true or not?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    overloading is something you can do in c++. But there's nothing preventing you from creating a function that takes a string, called something different from the one that takes a single char, that returned whether the entire string is for example digits or lower case chars.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,337
    Like this:

    Code:
    #include <stdio.h>
    
    int strIsdigit(char * data) { 
    	int i = 0 ; 
    	while (1) { 
    		if ( data[i] == '\0') return 1 ; // true - the string is all digits 
    		if ( !isdigit(data[i])) return 0; 
    		i++ ; 
    	}
    	return 1 ; 
    }
    
    int main (int argc, const char * argv[]) {
    	char num1[] = "0123456789" ; 
    	char num2[] = "1232abc" ; 
    	char yes[] = "Yes" ; 
    	char no[] = "No" ; 
    	char * answer1, * answer2 ; 
    	answer1 = strIsdigit(num1) ? yes : no ; 
    	answer2 = strIsdigit(num2) ? yes : no ; 
        printf("&#37;s is all digits? %s\n", num1, answer1 ) ;
        printf("%s is all digits? %s\n", num2, answer2 ) ;
        return 0;
    }
    Todd
    Last edited by Dino; 12-06-2007 at 10:27 AM. Reason: incorrect comment

Popular pages Recent additions subscribe to a feed