Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a function that lets me look at the next char?

Tags:

c

function

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}

like image 918
pisfire Avatar asked Sep 28 '10 17:09

pisfire


2 Answers

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

like image 165
Carl Norum Avatar answered Sep 24 '22 18:09

Carl Norum


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;            
    }
}
like image 36
GWW Avatar answered Sep 22 '22 18:09

GWW