-5

This is the program for converting the ascii sentence to character sentence. But i dont understand the num = num * 10 + (str[i] - '0'); why it us used and why it's needed?

#include <bits/stdc++.h> 
using namespace std; 
void asciiToSentence(string str, int len) 
{ 
    int num = 0; 
    for (int i = 0; i < len; i++) { 
        num = num * 10 + (str[i] - '0'); 
        if (num >= 32 && num <= 122) { 
            char ch = (char)num; 
            cout << ch; 
            num = 0; 
        } 
    } 
}
int main() 
{ 
    string str = "973265"; 
    int len = str.length(); 
    asciiToSentence(str, len); 
    return 0; 
}

2 Answers2

1

Firstly, bits/* are not standard headers, you shouldn't be using them otherwise your code may not compile with compilers like MSVC. Refer this.

Now let's come to your query : str[i] - '0' is simply a way to convert char to respective int, i.e. '0' to 0. You can find more discussion on this thread.

What does your code do?

It just keeps on extracting chars from your string (in loop), converting it to int and adding to num after multiplying it by 10. This is basically converting a part of string, say, "97" to an integer 97.

When num comes in range from 32 (' ') to 122 ('Z') (both inclusive), it cast them to char and prints it.

Explanation of Sample Output:

Your code prints a A to console. You will get this result if you can figure out the substrings. Here consider these three - "97" "32" "65". Then "97" will be converted to 97 (as int), then will be cast to char and end up being 'a' ('a' has ASCII value of 97). Similarly "32", will be a space ' ', and "65" will be 'A'.


These substrings didn't come here by magic. Let me explain one for you :

You have str = "973265", num = 0, then the loop begins :

i = 0 -> str[i] = '9' -> str[i] - '0' = 9 -> num = 0*10 + 9 = 9 -> is num in range [32, 122]? false -> go to next iteration.

i = 1 -> str[i] = '7' -> str[i] - '7' = 7 -> num = 9*10 + 7 = 97 -> is num in range [32, 122]? true -> ch = (char)num = (char)97 = 'A' -> print ch, i.e. 'A' -> num = 0 -> go to next iteration.


Pro Tip: Use your debugger, set breakpoints, watch variables and step through your code for better understanding.

Note that: When I wrote ____ - I meant ____ like this ____.

  • "convert" -- conversion -- '0' <-> 0 or "12" <-> 12
  • "cast" -- casting -- '0' <-> 48 (assuming your compiler is using ASCII).
brc-dd
  • 10,788
  • 3
  • 47
  • 67
0

In http://www.asciitable.com/, you can see that 0 has ascii value of 48, 1 is 49, 2 is 50 and so on.

So str[i] - '0' will give you 0 if str[i] == '0', 1 if str[i] == '1' and so on. This is a simple trick to convert numeric char into its mathematical value.

Let's call str[i] - '0' as x, so the line you don't understand becomes num = num * 10 + x. What this does is, take the original num (say it was 32), add a 0 behind it (so it becomes 320), then add x (if x is 7, num becomes 327).

So overall, this code is basically a function that converts a number string into integer type.

John London
  • 1,250
  • 2
  • 14
  • 32
  • Interesting fun fact: The ASCII value of `'0'` is irrelevant. `str[i] - '0'` will result in 0 no matter the encoding for any compliant C++ implementation. Unfortunately the `char ch = (char)num;` may not work if the character encoding is not sufficiently ASCII-like to encode the the alphabet with the same values. – user4581301 Oct 31 '20 at 05:31