Learntofish's Blog

A blog about math, physics and computer science

Basic Algorithm 4b: Length of an integer (part 2)

Posted by Ed on November 23, 2011

Last time we dealt with this problem:

Write a function that takes an integer and returns the length of that integer, i.e. the number of digits.
Example: 2011 has length 4.
Test your function with the following integers:
a) 31415926
b) 1000
c) 5
d) 0

I already posted a solution last time but the following crossed my mind:
We can convert the integer first to a string and then use a string method to determine the length.

Here is my solution in C++:
Here, we use a stringstream.

#include <iostream>
#include <sstream>
using namespace std;


int getLength(int x){
    // using a stringstream
    stringstream ss;
    ss << x;
    string xString = ss.str();
    return xString.length();
}

// stringstream:
// http://www.cplusplus.com/forum/beginner/1057/

// length of a string:
// http://www.cplusplus.com/reference/string/string/length/


int main(){
    int myNumber=31415926;
    int thousand=1000;
    int five=5;
    int zero=0;

    cout << "Converting to string..." << endl;
    cout << myNumber << " has length: " << getLength(myNumber) << endl;
    cout << thousand << " has length: " << getLength(thousand) << endl;
    cout << five << " has length: " << getLength(five) << endl;
    cout << zero << " has length: " << getLength(zero) << endl;

    cout << "\nEnd of program" << endl;
}

Here is my solution in Python 3:
The Python solution is very simple, we just use the string typecast str().

def getLength(x):
    # typecast to string
    xString = str(x)
    return len(xString)

# main function
def main():
    myNumber=31415926
    thousand=1000
    five=5
    zero=0

    print("Converting to string...")
    print(myNumber, "has length: ", getLength(myNumber))
    print(thousand, "has length: ", getLength(thousand))
    print(five, "has length: ", getLength(five))
    print(zero, "has length: ", getLength(zero))

    print("\nEnd of program")

main()

Here is my solution in Java:
The Java solution is almost as simple as the Python 3 solution. We use the String.valueOf(x) method here.

public class BasicAlgorithm4b_lengthInteger {
	
	int getLength(int x){
		// Converting to String
		String xString = String.valueOf(x);
		int length = xString.length();
		return length;
	}
	
	public static void main(String[] args){
		int myNumber=31415926;
		int thousand=1000;
		int five=5;
		int zero=0;
		
		BasicAlgorithm4b_lengthInteger myObj = new BasicAlgorithm4b_lengthInteger();
		
		System.out.println("Converting to String...");
		System.out.println(myNumber + " has length: " + myObj.getLength(myNumber));
		System.out.println(thousand + " has length: " + myObj.getLength(thousand));
		System.out.println(five + " has length: " + myObj.getLength(five));
		System.out.println(zero + " has length: " + myObj.getLength(zero));
		
		System.out.println("\nEnd of program");
	}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: