Thread: elements in char array?

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    2

    elements in char array?

    Hi everyone, I'm new to C (within the past week or so) and I have a question about arrays.

    I'm attempting to write a Hex to Integer (hex to decimal) function, while checking to make sure input is correct. My problem is checking to make sure all chars entered are in the set "0123456789abcdefABCDEF".

    The error occurs when a value of, say 'A', is entered (hex value of A = 10), the for loop will continue to verify while i<max, and jump out.

    is there any way to find out how many elements were added to the array when using scanf?

    here's my code:


    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int htoi(char s[])
    {
    	int x;
    
    	sscanf(s, "%x", &x);
     	printf("%u\n", x);
    	return x;
    }
    
    int main()
    {
    	const int max= 10;
    	char s[max];
    	int i;
    	const char *legal= "0123456789abcdefABCDEF";
    
    	printf("enter hex value (max size %d): ", max);
    	scanf("%s", s);
    
    	if('O'==s[0]) //Ox or OX prefix is present, remove it.
    	{
    		for(i=0; i<max-1; i++)
    		{
    			s[i]= s[i+2];
    		}
    		i=0;
    	}
            
            for(i=0; i<max; i++)
    	{
                    if(!strchr(legal, s[i]))
                    {
                            printf("Illegal hexidecimal value.");
                            return 0;
                    }
            }
    
    	return htoi(s);
    }

  2. #2
    Novice
    Join Date
    Jul 2009
    Posts
    568
    strlen() can tell you how many characters there are in a string, excluding the terminating null-characters.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    2
    Quote Originally Posted by msh View Post
    strlen() can tell you how many characters there are in a string, excluding the terminating null-characters.
    thank you, thank you. works great now.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int n = 0;
    ...
    scanf( "%s%n", s, &n );
    n Nothing is expected; instead, the number of characters consumed
    thus far from the input is stored through the next pointer, which
    must be a pointer to int. This is not a conversion, although it
    can be suppressed with the * flag.
    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  2. The UNIX System Interface
    By rrc55 in forum C Programming
    Replies: 1
    Last Post: 10-20-2009, 05:56 PM
  3. warning: excess elements in array initializer
    By redruby147 in forum C Programming
    Replies: 6
    Last Post: 09-30-2009, 06:08 AM
  4. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  5. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM