简体   繁体   中英

converting an array of integers to string

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 do so I'm attempting to convert the array of integers into a string (which is an array of chars).

My attempt looks like

string toString(int digits[], int size){
string number = " ";
for(int i = 0; i < size - 1; i++){
    number[i] = digits[i];
}
return number;
}

which came out horribly broken.

I also can't simply remove all zeros, just the leading ones.

Also if I may dogpile another question here:

how can I identify if a string is numeric?

eg

string number = "12a4"
cout << "not a number"

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;
}

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.

Try the following way:

int i = 0;
while(digits[i] == 0) i++;
for (; i < size; i++)
   result += to_string(digits[i]);

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM