Reputation: 65
I have problem with extracting numbers from string.
Problem is I can not use strtol or isdigit functions as teacher requested so.
I am supposed to save numbers in new array.
For example, string input:
11-some12meh13text-14
.
So new array would be:
11,12,13,14
(NOT -14)
I came to idea to do following:
char str1[150];
int array[20];
int temporary, i=0, j=0; //i - for string, j - for array
for(i=0;i<=strlen(str1);i++) {
if(i==0) { //not needed to check if char before was number
if((str1[i]>=0) && (str1[i]<=9))temporary=str1[i];
if((str1[i+1]<=0) && (str1[i+1]>=9)) {//if next char is NOT number
array[j]=temporary;
j++;
}
}
else if(i!=0) { //needs to be checked if char before was number
if((str1[i]>=0) && (str1[i]<=9)) {
if((str1[i-1]>=0) && (str1[i-1]<=9))temporary=temporary*10+str1[i];
else temporary=str1[i];
if((str1[i+1]<=0) && (str1[i+1]>=9)) {//if next char is NOT number
array[j]=temporary;
j++;
}
}
}
}
I tried it on my PC, but program crashes and I have no idea or ability to find what I did wrong. Please help! Thank you!
Upvotes: 2
Views: 401
Reputation: 8286
If using sscanf()
is allowed, this will parse the string for integers. The %n
specifier will report the number of characters processed by the scan. This lets you move through the string.
When the scan for an integer fails ( != 1), the scanset "%*[^0-9\n]%n"
scans and discards characters that are not a digit or newline. The %n
captures the number of characters processed.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ( ) {
char str1[150] = {"0+1-=2!3*4-5/6#7&8|9^10"};
int array[20] = {0};
int offset = 0;
int each = 0;
int loop = 0;
int used = 0;
int length = 0;
length = strlen ( str1);
do {
sscanf ( str1 + offset, "%*[^0-9\n]%n", &used);
offset += used;
if ( ( sscanf ( str1 + offset, "%d%n", &array[each], &used)) == 1) {
offset += used;
each++;
if ( each >= 20) {
break;
}
}
} while ( offset < length);
for ( loop = 0; loop < each; loop++) {
printf ( "%d\n", array[loop]);
}
return 0;
}
Here is a version of your code that seems to work
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ( ) {
char str1[150] = {"-0+1-=2!3*4-text-5/6#7&8|9^10 11end"};
int array[20] = {0};
int each = 0;
int loop = 0;
int saved = 1;
int length = 0;
int temporary = 0, i=0;
length = strlen ( str1);
for ( i = 0; i < length; i++) {
if ( ( str1[i] >= '0') && ( str1[i] <= '9')) {//found a digit
temporary = ( temporary * 10) + ( str1[i] - '0');
saved = 0;//flag not saved. temporary value may change
}
else {//found NOT a digit
if ( i > 0 && !saved) {//not at first character and not saved
array[each] = temporary;//save integer
each++;
saved = 1;//flag saved
temporary = 0;//reset
if ( each >= 20) {
break;
}
}
}
}
if ( !saved && each < 19) {//end of string is an integer. save it
array[each] = temporary;
each++;
}
for ( loop = 0; loop < each; loop++) {
printf ( "%d\n", array[loop]);
}
return 0;
}
Upvotes: 1
Reputation: 30136
The integral values of characters '0'...'9'
are not 0...9
.
Hence, change every occurrence of str1[k]
to str1[k]-'0'
.
Upvotes: 2