Counting Numbers using Array in C

Asked

Viewed 244 times

0

How to count integers using a C array in this model:

Input > two strings with "integers"

ex: 8 15

Output > A string with range numbers, inclusive:

ex: 8 9 10 11 12 13 14 15.

Probably a basic doubt, but I’m a beginner in the area yet.

Thank you!

  • 1

    What you’ve already done?

  • You need necessarily in C or it can be C++?

  • This is with a homework face... You are with some specific difficulty?

1 answer

0


Come on, if it’s C you need then follow:

//Define a entrada dos 2 valores
char rangeStr[] = "8 15";
//Define a variavel que vai armazenar os valores convertidos
int rangeInt[2] = { 0, 0 };

//Tokeniza a string com delimitador ' '
char* splitedRange = strtok(rangeStr, " ");

//Define a variavel de contagem para garantir que apenas 2 valores sejam atribuidos
int i = 0;
//inicializa o loop para extrair os valores entre ' ' e garantir que sejam até 2
while (splitedRange && i < 2)
{
    //verifica se o valor recebido é numérico
    if (isdigit(*splitedRange))
        rangeInt[i] = atoi(splitedRange);
    splitedRange = strtok(NULL, " ");
    i++;
}
//Cria o looping em cima dos dois valores entregues
for (int start = rangeInt[0]; start <= rangeInt[1]; start++)
    printf("%d\n", start);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.