The variable not_num contains a string of three digit characters, for example "528" . It is necessary to obtain the corresponding number from this line and assign it to the variable num . Display the result of the expression num - 10. (Hint: a string is an array of characters, therefore, you can extract the characters of digits by their indices; when calculating a number, the first character of the array, converted to a number, means the number of hundreds, the second - tens, third - units.)

  • For any character c in the range 0..9 numeric value can be obtained as c - '0' . You can take advantage of this. - andy.37
  • So what's the problem? Why not just use the strtoul function? - AnT
  • @AnT, based on the clue in the question text, the problem is the lack of understanding of character encodings (however, the semester has clearly ended) - avp

3 answers 3

char not_num - maybe you still meant char*not_num ?

Look at functions like atoi or strtol .

PS The first clarification (I wonder how many more they will be? :))

 num = 0; char* s = not_num; while(*s) { num = num*10 + (*s++ - '0'); } 
  • Here is the task: The variable not_num contains a string of three digit characters, for example "528". It is necessary to obtain the corresponding number from this line and assign it to the variable num. Display the result of the expression num - 10. (Hint: a string is an array of characters, therefore, you can extract the characters of digits by their indices; when calculating a number, the first character of the array, converted to a number, means the number of hundreds, the second - tens, third - units.) - biggy
  • And there is nothing there about third-party functions - biggy
  • And what did you write in the assignment? :) Bad TK - and the result is HZ ... - Harry
  • one
    @AntonShchyrov and what is unreadable here? * s ++ is a classic imho. But the brackets can and be removed) - pavel
  • four
    @Harry I was waiting for an answer here 100*a[0]+10*a[1]+a[2]-5328 ) - pavel
 int num; num = (not_num[0] - '0') * 100 + (not_num[1] - '0') * 10 + (not_num[2] - '0') printf("%d\n", num - 10); 
     long num = strtol(not_num, NULL, 10); printf("%ld\n", num - 10); 

    That's all.