Monday, May 24, 2010

Is there a C function that will take an integer and output it as a string?

example an input of 256 will output two hundred and fifty six or something similar

Is there a C function that will take an integer and output it as a string?
try sprintf(). It's like printf, but it puts the formatted output to a string.





sprintf(stringName,"%d",intigerName);


that should work. I'm not sure what header sprintf() is defined in, i think its in one of the main ones.
Reply:No, but I found this functionality:


#include %26lt;sstream%26gt;





template %26lt;class T%26gt;


inline std::string to_string (const T%26amp; t)


{


std::stringstream ss;


ss %26lt;%26lt; t;


return ss.str();


}





Now if you are wanting to display the words for the numbers you'll have a lot more work. You have to parse the string, keep track of where you are, 10's, 100's, 1000's etc., and display the appropriate wording for each position and number. Have fun.


No comments:

Post a Comment