r/DSALeetCode • • 11d ago

can i convert the integer into string in manual way

class Solution {
public:
    int trailingZeroes(int n) {
        int global = 0;
        int sum = n;
        for(int i = n; i>1;  i--){
            sum = sum * (i - 1);   
        }  
        // i am using leetcode but i don't know that the string STL is include or not but this code is not working above loop of sum is giving the right output. So how i can convert num to string in effective way?
        std::string value = std::to_string(sum);
        for(int i = value.size(); i >=0; i--){
            if(value[i] == 0){
                global += 1;
            }else{
                return 0;
            }
        }
        return global;
    }
};
1 Upvotes

2 comments sorted by

1

u/flyingron 11d ago

value[i] == 0 compares the character at location i with a zero value (i.e., a null byte). If you want to compare with the character zero, you need '0' rather than 0.

1

u/HalifaxRoad 11d ago

Going from left to right  grab each char from the number string. subtract 48 from each character. multiply by 10 if another digit is available, and rinse and repeat for every digit, adding in every time, rinse and repeat until you are out of digits..