1

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?

e.g

string number = "12a4"
cout << "not a number"
3
  • 1
    Start by looking at this: stackoverflow.com/questions/5590381/… It should help you convert int to string Commented Apr 13, 2017 at 19:30
  • 1
    Take a look at, stackoverflow.com/questions/5223066/… Commented Apr 13, 2017 at 19:33
  • edited my answer to match all your request. Comment my post if you have more issues Commented Apr 13, 2017 at 19:42

5 Answers 5

7

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;
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

Try the following way:

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

Comments

1

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

7 Comments

that's a bad practise and he wants to use an array of integers; not a vector
@RomMer what is bad practice about that? Do you really rather convert an array of integers to a string than to a vector of ints?
i use vectors; but op didn't if you read his post he uses an array of integer
@RomMer a vector is just an array (plus a nicer interface)
@RomMer anyhow, I added some comments in the code so it can be used starting from an array
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.