tags:

views:

92

answers:

3

is there a function in c that lets me look at the next char in an array? Also where could I find this information on my own, I tried Google and looking for existing threads on this site.

I am trying to pull numbers from a line, and store those numbers. So I want to do something like

if(c = a number and c "next character" is not a number){value is = value*10+c-'0', store number}

A: 

To get a char from a char array, you just use:

array[i]

where is is an index.

If you want to read every char, you can use a loop:

for(size_t i = 0; i < sizeof(array); i++)
{
  char cur = array[i];
}

You could also increment a pointer.

Matthew Flaschen
+5  A: 

If the current character is array[i], the next character is array[i+1].

Carl Norum
+5  A: 

You could write a method to do this:

char next_char(char *array, int i, int size){
    return (++i) < size ? array[i] : '\0';
}

EDIT: After reading your question something like this may be reasonable.

if(isdigit(array[i]) && !isdigit(next_char(array,i,size)){
    ..
}

A better solution would be a for loop:

int val = 0;
for(i = 0; i < size; i++){
    if(isdigit(i)){
        val = 10 * val + array[i] - '0';
    }else{
        // Store the value
        val = 0;            
    }
}
GWW
thank you very much
pisfire