Stack Overflow Asked by Tom Arica on December 2, 2020
I have a problem doing itterations on a string.
function input:
the function needs to create an array of the numbers.
for example if I get the input "809-2-3", my new array needs to be – [8,0,9,-2,-3].
in my code, I get the error ‘expression must have a pointer-to-object type’, and I dont understand it.
this is the code I made:
void separate_nums(char str, int str_len)
{
int new_arr[20];
int i;
for (i = 0; i <= str_len - 1; i++)//run through the string
{
if (str[i] == '-') //if the char is -, take minus of the next char. than increase index by one so youll get to the next number
{
new_arr[i] = -str[i + 1];
i++;
}
else
new_arr[i] = str[i];
}
}
Thank you!
First, an array is needed.
n
).n
numbers.Next, it's just a question of looking at the characters of the string one at a time. Any time a -
is encountered, simply use the next character, but negate it.
-
,
Of course, we need to make sure the user doesn't provide garbage. This is left to you to handle.
Answered by ikegami on December 2, 2020
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP