converting an array of integers to string

后端 未结 5 1681
闹比i
闹比i 2021-01-25 11:23

If I have an array that looks like

int digits[size] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 4}

I want to remove the leading zeros and to d

相关标签:
5条回答
  • 2021-01-25 11:55

    you can use C++11 function std::to_string() here is an example

    #include <string>
    #include <iostream>
    
    int main()
    {
       int size = 15;
       int digits[size] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 4};
       std::string result = "";
    
       for (int i = 0; i < size; i++)
       {
          if (!(digits[i] == 0 && result.size() == 0))
             result += std::to_string(digits[i]);
       }
    
       std::cout << result << std::endl;
    }
    

    you can check if a string is numeric using this function

    bool isNb(std::string str)
    {
      if (str.size() == 0)
        return false;
    
      for (int i = 0; i < str.size(); i++)
      {
        if (std::isdigit(str.at(i)) == false)
          return false;
      }
    
      return true;
    }
    
    0 讨论(0)
  • 2021-01-25 11:55

    To answer your actual question (How to remove the leading zeros?) here a solution without strings:

    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    int main() {
        std::vector<int> x = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 4};
        // ...or if you insist on the array...
        // int x[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 4};
        // std::vector<int> x = {x,x+15};
        auto it = std::find_if_not(x.begin(),x.end(),[](int i){return i==0;});
        std::vector<int> y{it,x.end()};
        for (auto i : y) std::cout << i << " ";
    }
    

    prints:

    1 2 3 0 4

    0 讨论(0)
  • 2021-01-25 11:56

    You can use a string stream to convert from any type to string:

    #include <sstream> //<-- ALLOWS USE OF std::stringstream 
    const int size = 15;
    
    int digits[size] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 4};
    
    std::stringstream ss;  //<-- DECLARE STREAM
    
    int k;
    for (k = 0; k < size; ++k)
            if (digits[k] != 0)
                    break; //FIND FIRST NON-0 INDEX
    
    for (int i = k; i < size; ++i)
                    ss << digits[i]; //ADD TO THE STREAM s
    
    std::cout<< ss.str() << std::endl; //PRINT STREAM
    

    12304

    0 讨论(0)
  • 2021-01-25 12:12

    Try the following way:

    int i = 0;
    while(digits[i] == 0) i++;
    for (; i < size; i++)
       result += to_string(digits[i]);
    
    0 讨论(0)
  • 2021-01-25 12:14

    Instead of changing digits with your for loop, add them with

     number += to_string(digits[i]);
    

    Also, you can remove the toString line you have, and just use it as I put here.

    As to your other question, just use a for loop to check each digit in the string and its ASCII value, if there is any whose ASCII value is less than 48 or greater than 57 then it's not a number.

    0 讨论(0)
提交回复
热议问题