Like Tree1Likes
  • 1 Post By Adak

Find first occurance of a number in a string.

This is a discussion on Find first occurance of a number in a string. within the C Programming forums, part of the General Programming Boards category; In my class i've been having to use while() and if() statements to display different things from a given string. ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    23

    Find first occurance of a number in a string.

    In my class i've been having to use while() and if() statements to display different things from a given string.

    Now i have to display the first occurrence of a number in this string. I know how to display all of the numbers and stuff in a string like:
    Code:
    /*Finds the numbers in the string and displays them*/
    
    while (o < len)
    {
    	if(isdigit(str[o]))
    	{
    		printf("%c", str[o]);		
    	}
    	o++;
    }
    How do i just display the first number in the string i haven't ever had to do it before. Any tips is greatly appreciated.

    If it makes it easier, lets just say the string is: aevf4l68js5bb (the only reason the letters are so random is because the given string is way more confusing)

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    While loop would be fine to use for this:

    Code:
    int len = strlen(arrayString);
    int i = 0;
    while(i < len) {
       if(isdigit(arrayString[i])) {
          printf("Found it: %d", arrayString[i]);
          break;
       }
       ++i;
    }
    Last edited by Adak; 09-12-2011 at 10:48 PM.
    AndrewHunter likes this.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by 337higgin View Post
    In my class i've been having to use while() and if() statements to display different things from a given string.

    Now i have to display the first occurrence of a number in this string. I know how to display all of the numbers and stuff in a string like:
    Code:
    /*Finds the numbers in the string and displays them*/
    
    while (o < len)
    {
    	if(isdigit(str[o]))
    	{
    		printf("%c", str[o]);		
    	}
    	o++;
    }
    How do i just display the first number in the string i haven't ever had to do it before. Any tips is greatly appreciated.

    If it makes it easier, lets just say the string is: aevf4l68js5bb (the only reason the letters are so random is because the given string is way more confusing)
    It's pretty simple really... just exit the loop as soon as you find the first number...
    Hint: Look up the "break" keyword in your compiler's C Libarary documentation.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Or you could do it without a break statement:
    Code:
    char *s = str;
    if(!isdigit(*s))
    {
      while(*s && !isdigit(*++s))
        ;
    }
    if(*s)
      putchar(*s);
    Last edited by itsme86; 09-12-2011 at 11:03 PM.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    23
    im actually supposed to display the first number and the first non alpha character.

    i can get the first number to show, but i can't get the non alpha to show at the same time. Am i not able to use multiple breaks in a code even though they are in separate while statements?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you want them to show (be printed) at the same time, then save the values, and print them up at the same time.

    Code:
    int len = strlen(arrayString);
    int i = 0;
    int firstNumber = -99;
    while(i < len) {
       if(isdigit(arrayString[i])) {
          firstNumber = arrayString[i]);
       }
       if(your second test is true)
          variable = arrayString[i] statement
     
      ++i;
    }
    
    print up your saved variables from the scan of the array, here
    Don't print up the firstNumber, if it still equals -99, since that shows there was no digit in the string, at all.
    Like that?

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Only problem with isdigit() in this context is what you consider a number, it actually finds individual digits. In your example string: aevf4l68js5bb should 68 be interpreted as one number or two 6,8?

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    You could also set flags when the values are seen and printed :

    Code:
    int digit_seen = 0
    int other_seen = 0
    
    while (i < len)
    {
        if (!digit_seen && isdigit(array[i]))
        {
            print digit
            digit_seen = 1
        }
        if (!other_seen && iswhatever(array[i])
        {
            print other
            other_seen = 1
        }
        i += 1
    }
    You could then add something to break out of the loop if both digit_seen and other_seen are true since there's nothing else to print at that point.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by 337higgin View Post
    im actually supposed to display the first number and the first non alpha character.

    i can get the first number to show, but i can't get the non alpha to show at the same time. Am i not able to use multiple breaks in a code even though they are in separate while statements?
    So... write two loops...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help, trying to find prime number
    By vampirekid.13 in forum C++ Programming
    Replies: 19
    Last Post: 05-02-2009, 03:29 PM
  2. Best way to find maximum number
    By mutombo in forum C Programming
    Replies: 3
    Last Post: 02-27-2009, 03:36 AM
  3. Find all factors of a number
    By Spono in forum C Programming
    Replies: 5
    Last Post: 10-23-2003, 01:23 AM
  4. how do u find 2nd largest number??
    By juancardenas in forum C Programming
    Replies: 8
    Last Post: 02-14-2003, 07:28 AM
  5. find first occurance of a string in a file
    By jstn in forum C Programming
    Replies: 2
    Last Post: 05-08-2002, 08:00 PM

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