simple c question

This is a discussion on simple c question within the C Programming forums, part of the General Programming Boards category; i have one set of numbers many numbers start with '1' and start with '2' and start with '3'. i ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    19

    simple c question

    i have one set of numbers many numbers start with '1' and start with '2' and start with '3'. i seperate these numbers how i identified which numbers start with '1','2' and '3'?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    Well if you read them in as strings, then you do
    Code:
    if ( num_in_a_string[0] == '1' )

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Or...
    Code:
    if( strchr( num_in_a_string, '1' ) == num_in_a_string )
    Next poster, come up with your version of it.

    Quzah.
    Last edited by quzah; 12-02-2004 at 01:16 PM. Reason: Where's my coffee?
    Hope is the first step on the road to disappointment.

  4. #4
    .
    Join Date
    Nov 2003
    Posts
    307
    Probably :
    Code:
    if( strchr( num_in_a_string, '1' ) == num_in_a_string )
    unless he wants to find SOH

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    *slaps forehead* Yeah. I should have just used the strstr version. That's what I get for not having some coffee right when I get up. Be right back.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Without using any functions prototyped in string.h!
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      int num = 123984;
      int i;
      int is_a_one = 0;
    
      for(i = 1;i < num && i > 0;i *= 10)
        if(num/i < 10)
        {
          if(num/i == 1)
            is_a_one = 1;
          break;
        }
    
      printf("It's%s a one!\n", is_a_one ? "" : " not");
    
      return 0;
    }
    Last edited by itsme86; 12-02-2004 at 01:44 PM. Reason: Oops! Needed to start loop at 1 instead of 10
    If you understand what you're doing, you're not learning anything.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Without weird nested ifs
    Code:
    #include <stdio.h>
    
    int main()
    {
      int num = 123984;
      int i;
      int last_digit;
    
      while(num > 0) {
        last_digit = num % 10;
        num /= 10;
      }
    
      printf("Number starts with a %i.\n", last_digit);
    }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    Code:
    if ( num == 1 || ( num >= 10 && num <= 19 ) || ( num >= 100 && num <= 199 ) )
    Just add the other half-dozen or so cases and you'll have all the numbers which begin with a '1'

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    16
    Code:
    int first_digit(int number)
    {
            if (number < 0)
                    number = -number;
    
            while (number >= 10)
                    number /= 10;
    
            return number;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 08:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 11:15 AM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 05:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21