# Find first occurance of a number in a string.

• 09-12-2011
337higgin
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)
• 09-12-2011
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; }```
• 09-12-2011
CommonTater
Quote:

Originally Posted by 337higgin
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.
• 09-12-2011
itsme86
Or you could do it without a break statement:
Code:

```char *s = str; if(!isdigit(*s)) {   while(*s && !isdigit(*++s))     ; } if(*s)   putchar(*s);```
• 09-12-2011
337higgin
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?
• 09-12-2011
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?
• 09-12-2011
Subsonics
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?
• 09-13-2011
KCfromNC
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.
• 09-13-2011
CommonTater
Quote:

Originally Posted by 337higgin
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...