Thread: checking if a string contains a letter

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    21

    Unhappy checking if a string contains a letter

    I wrote this piece of code but I'm not satisfied at all with it. Could anyone provide me with a better and smarter code to do the trick ?
    thanks in advance
    Code:
    #include   <stdio.h>
    #include   <ctype.h>
    
    int contains_a_letter(char * address)
    {
    	int i;
    	int output;
    	output = 1;
    	for ( i=0;  address[i]!='\0'; i++ ) {
    						printf("address[i] : %d\n",address[i]);
            					if( isalpha( address[i] ) )
                       				{
                				 	printf("  There is a letter \n");                
    					 	output = 0;
                       				}
                       				else  
                       				{
                       			 	printf("  There is no letter \n");	              
            	                 		}	
                       			   }
                 return(output);
    }
     
     main() 
    {
    int result;
    char address[32]="2574A4564G54";
     
    result = contains_a_letter(address);
    printf("result : %d\n", result);
    if (result == 0) {
    	printf("address contains a letter\n");
    	        }
    else {
    	printf("address doest not contain a letter\n");
         }
    		        
    return 0; 
    }

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Instead of "output = 0;", do "return 0;"
    No point in looping through the rest of the string once you find an alpha.

    - make your parameter "const char *address", since you don't modify it
    - main() returns int, declare it (this is required in the latest C standard)
    - "char address[32]="2574A4564G54";" - don't need to declare the size since you are initializing it - but it doesn't hurt either.

    gg

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    bool ContainsLetter(const char* String)
    {
       //Abort if no string is passed
       if(String == NULL) return false;
    
       while((*String) != '\0')
       {
          if(isalpha(*String)) return true;
          String++;
       }
    
       return false;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM